1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
9 * This is an example application source code using the multi interface.
15 /* somewhat unix-specific */
20 #include <curl/curl.h>
23 * Download a HTTP file and upload an FTP file simultaneously.
26 #define HANDLECOUNT 2 /* Number of simultaneous transfers */
27 #define HTTP_HANDLE 0 /* Index for the HTTP transfer */
28 #define FTP_HANDLE 1 /* Index for the FTP transfer */
30 int main(int argc, char **argv)
32 CURL *handles[HANDLECOUNT];
35 int still_running; /* keep number of running handles */
38 CURLMsg *msg; /* for picking up messages with the transfer status */
39 int msgs_left; /* how many messages are left */
41 /* Allocate one CURL handle per transfer */
42 for (i=0; i<HANDLECOUNT; i++)
43 handles[i] = curl_easy_init();
45 /* set the options (I left out a few, you'll get the point anyway) */
46 curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "http://example.com");
48 curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://example.com");
49 curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
51 /* init a multi stack */
52 multi_handle = curl_multi_init();
54 /* add the individual transfers */
55 for (i=0; i<HANDLECOUNT; i++)
56 curl_multi_add_handle(multi_handle, handles[i]);
58 /* we start some action by calling perform right away */
59 curl_multi_perform(multi_handle, &still_running);
61 while(still_running) {
62 struct timeval timeout;
63 int rc; /* select() return code */
76 /* set a suitable timeout to play around with */
80 curl_multi_timeout(multi_handle, &curl_timeo);
82 timeout.tv_sec = curl_timeo / 1000;
83 if(timeout.tv_sec > 1)
86 timeout.tv_usec = (curl_timeo % 1000) * 1000;
89 /* get file descriptors from the transfers */
90 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
92 /* In a real-world program you OF COURSE check the return code of the
93 function calls. On success, the value of maxfd is guaranteed to be
94 greater or equal than -1. We call select(maxfd + 1, ...), specially in
95 case of (maxfd == -1), we call select(0, ...), which is basically equal
98 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
104 case 0: /* timeout */
105 default: /* action */
106 curl_multi_perform(multi_handle, &still_running);
111 /* See how the transfers went */
112 while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
113 if (msg->msg == CURLMSG_DONE) {
116 /* Find out which handle this message is about */
117 for (idx=0; idx<HANDLECOUNT; idx++) {
118 found = (msg->easy_handle == handles[idx]);
125 printf("HTTP transfer completed with status %d\n", msg->data.result);
128 printf("FTP transfer completed with status %d\n", msg->data.result);
134 curl_multi_cleanup(multi_handle);
136 /* Free the CURL handles */
137 for (i=0; i<HANDLECOUNT; i++)
138 curl_easy_cleanup(handles[i]);