1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 /* This is an example application source code using the multi interface. */
27 /* somewhat unix-specific */
32 #include <curl/curl.h>
35 * Download a HTTP file and upload an FTP file simultaneously.
38 #define HANDLECOUNT 2 /* Number of simultaneous transfers */
39 #define HTTP_HANDLE 0 /* Index for the HTTP transfer */
40 #define FTP_HANDLE 1 /* Index for the FTP transfer */
44 CURL *handles[HANDLECOUNT];
47 int still_running; /* keep number of running handles */
50 CURLMsg *msg; /* for picking up messages with the transfer status */
51 int msgs_left; /* how many messages are left */
53 /* Allocate one CURL handle per transfer */
54 for (i=0; i<HANDLECOUNT; i++)
55 handles[i] = curl_easy_init();
57 /* set the options (I left out a few, you'll get the point anyway) */
58 curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "http://example.com");
60 curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://example.com");
61 curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
63 /* init a multi stack */
64 multi_handle = curl_multi_init();
66 /* add the individual transfers */
67 for (i=0; i<HANDLECOUNT; i++)
68 curl_multi_add_handle(multi_handle, handles[i]);
70 /* we start some action by calling perform right away */
71 curl_multi_perform(multi_handle, &still_running);
74 struct timeval timeout;
75 int rc; /* select() return code */
88 /* set a suitable timeout to play around with */
92 curl_multi_timeout(multi_handle, &curl_timeo);
94 timeout.tv_sec = curl_timeo / 1000;
95 if(timeout.tv_sec > 1)
98 timeout.tv_usec = (curl_timeo % 1000) * 1000;
101 /* get file descriptors from the transfers */
102 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
104 /* In a real-world program you OF COURSE check the return code of the
105 function calls. On success, the value of maxfd is guaranteed to be
106 greater or equal than -1. We call select(maxfd + 1, ...), specially in
107 case of (maxfd == -1), we call select(0, ...), which is basically equal
110 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
116 case 0: /* timeout */
117 default: /* action */
118 curl_multi_perform(multi_handle, &still_running);
121 } while(still_running);
123 /* See how the transfers went */
124 while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
125 if (msg->msg == CURLMSG_DONE) {
128 /* Find out which handle this message is about */
129 for (idx=0; idx<HANDLECOUNT; idx++) {
130 found = (msg->easy_handle == handles[idx]);
137 printf("HTTP transfer completed with status %d\n", msg->data.result);
140 printf("FTP transfer completed with status %d\n", msg->data.result);
146 curl_multi_cleanup(multi_handle);
148 /* Free the CURL handles */
149 for (i=0; i<HANDLECOUNT; i++)
150 curl_easy_cleanup(handles[i]);