1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
10 * This is an example application source code using the multi interface.
16 /* somewhat unix-specific */
21 #include <curl/curl.h>
24 * Download a HTTP file and upload an FTP file simultaneously.
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 */
31 int main(int argc, char **argv)
33 CURL *handles[HANDLECOUNT];
36 int still_running; /* keep number of running handles */
39 CURLMsg *msg; /* for picking up messages with the transfer status */
40 int msgs_left; /* how many messages are left */
42 /* Allocate one CURL handle per transfer */
43 for (i=0; i<HANDLECOUNT; i++)
44 handles[i] = curl_easy_init();
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");
49 curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://ftpsite.com");
50 curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
52 /* init a multi stack */
53 multi_handle = curl_multi_init();
55 /* add the individual transfers */
56 for (i=0; i<HANDLECOUNT; i++)
57 curl_multi_add_handle(multi_handle, handles[i]);
59 /* we start some action by calling perform right away */
60 while(CURLM_CALL_MULTI_PERFORM ==
61 curl_multi_perform(multi_handle, &still_running));
63 while(still_running) {
64 struct timeval timeout;
65 int rc; /* select() return code */
76 /* set a suitable timeout to play around with */
80 /* get file descriptors from the transfers */
81 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
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! */
87 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
94 /* timeout, do something else */
97 /* one or more of curl's file descriptors say there's data to read
99 while(CURLM_CALL_MULTI_PERFORM ==
100 curl_multi_perform(multi_handle, &still_running));
105 /* See how the transfers went */
106 while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
107 if (msg->msg == CURLMSG_DONE) {
111 /* Find out which handle this message is about */
112 for (idx=0; (!found && (idx<HANDLECOUNT)); idx++) found = (msg->easy_handle == handles[idx]);
116 printf("HTTP transfer completed with status %d\n", msg->data.result);
119 printf("FTP transfer completed with status %d\n", msg->data.result);
125 curl_multi_cleanup(multi_handle);
127 /* Free the CURL handles */
128 for (i=0; i<HANDLECOUNT; i++)
129 curl_easy_cleanup(handles[i]);