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://website.com");
48 curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://ftpsite.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 while(CURLM_CALL_MULTI_PERFORM ==
60 curl_multi_perform(multi_handle, &still_running));
62 while(still_running) {
63 struct timeval timeout;
64 int rc; /* select() return code */
75 /* set a suitable timeout to play around with */
79 /* get file descriptors from the transfers */
80 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
82 /* In a real-world program you OF COURSE check the return code of the
83 function calls, *and* you make sure that maxfd is bigger than -1 so
84 that the call to select() below makes sense! */
86 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
93 /* timeout, do something else */
96 /* one or more of curl's file descriptors say there's data to read
98 while(CURLM_CALL_MULTI_PERFORM ==
99 curl_multi_perform(multi_handle, &still_running));
104 /* See how the transfers went */
105 while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
106 if (msg->msg == CURLMSG_DONE) {
109 /* Find out which handle this message is about */
110 for (idx=0; idx<HANDLECOUNT; idx++) {
111 found = (msg->easy_handle == handles[idx]);
118 printf("HTTP transfer completed with status %d\n", msg->data.result);
121 printf("FTP transfer completed with status %d\n", msg->data.result);
127 curl_multi_cleanup(multi_handle);
129 /* Free the CURL handles */
130 for (i=0; i<HANDLECOUNT; i++)
131 curl_easy_cleanup(handles[i]);