Fixed a surprising number of example programs that were passing int arguments
[platform/upstream/curl.git] / docs / examples / multi-app.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * $Id$
9  *
10  * This is an example application source code using the multi interface.
11  */
12
13 #include <stdio.h>
14 #include <string.h>
15
16 /* somewhat unix-specific */
17 #include <sys/time.h>
18 #include <unistd.h>
19
20 /* curl stuff */
21 #include <curl/curl.h>
22
23 /*
24  * Download a HTTP file and upload an FTP file simultaneously.
25  */
26
27 #define HANDLECOUNT 2   /* Number of simultaneous transfers */
28 #define HTTP_HANDLE 0   /* Index for the HTTP transfer */
29 #define FTP_HANDLE 1    /* Index for the FTP transfer */
30
31 int main(int argc, char **argv)
32 {
33   CURL *handles[HANDLECOUNT];
34   CURLM *multi_handle;
35
36   int still_running; /* keep number of running handles */
37   int i;
38
39   CURLMsg *msg; /* for picking up messages with the transfer status */
40   int msgs_left; /* how many messages are left */
41
42   /* Allocate one CURL handle per transfer */
43   for (i=0; i<HANDLECOUNT; i++)
44       handles[i] = curl_easy_init();
45
46   /* set the options (I left out a few, you'll get the point anyway) */
47   curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "http://website.com");
48
49   curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://ftpsite.com");
50   curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
51
52   /* init a multi stack */
53   multi_handle = curl_multi_init();
54
55   /* add the individual transfers */
56   for (i=0; i<HANDLECOUNT; i++)
57       curl_multi_add_handle(multi_handle, handles[i]);
58
59   /* we start some action by calling perform right away */
60   while(CURLM_CALL_MULTI_PERFORM ==
61         curl_multi_perform(multi_handle, &still_running));
62
63   while(still_running) {
64     struct timeval timeout;
65     int rc; /* select() return code */
66
67     fd_set fdread;
68     fd_set fdwrite;
69     fd_set fdexcep;
70     int maxfd;
71
72     FD_ZERO(&fdread);
73     FD_ZERO(&fdwrite);
74     FD_ZERO(&fdexcep);
75
76     /* set a suitable timeout to play around with */
77     timeout.tv_sec = 1;
78     timeout.tv_usec = 0;
79
80     /* get file descriptors from the transfers */
81     curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
82
83     /* In a real-world program you OF COURSE check the return code of the
84        function calls, *and* you make sure that maxfd is bigger than -1 so
85        that the call to select() below makes sense! */
86
87     rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
88
89     switch(rc) {
90     case -1:
91       /* select error */
92       break;
93     case 0:
94       /* timeout, do something else */
95       break;
96     default:
97       /* one or more of curl's file descriptors say there's data to read
98          or write */
99       while(CURLM_CALL_MULTI_PERFORM ==
100             curl_multi_perform(multi_handle, &still_running));
101       break;
102     }
103   }
104
105   /* See how the transfers went */
106   while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
107     if (msg->msg == CURLMSG_DONE) {
108
109        int idx, found = 0;
110
111        /* Find out which handle this message is about */
112        for (idx=0; (!found && (idx<HANDLECOUNT)); idx++) found = (msg->easy_handle == handles[idx]);
113
114        switch (idx) {
115          case HTTP_HANDLE:
116            printf("HTTP transfer completed with status %d\n", msg->data.result);
117            break;
118          case FTP_HANDLE:
119            printf("FTP transfer completed with status %d\n", msg->data.result);
120            break;
121        }
122     }
123   }
124
125   curl_multi_cleanup(multi_handle);
126
127   /* Free the CURL handles */
128   for (i=0; i<HANDLECOUNT; i++)
129       curl_easy_cleanup(handles[i]);
130
131   return 0;
132 }