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 */
76 CURLMcode mc; /* curl_multi_fdset() return code */
89 /* set a suitable timeout to play around with */
93 curl_multi_timeout(multi_handle, &curl_timeo);
95 timeout.tv_sec = curl_timeo / 1000;
96 if(timeout.tv_sec > 1)
99 timeout.tv_usec = (curl_timeo % 1000) * 1000;
102 /* get file descriptors from the transfers */
103 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
107 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
111 /* On success the value of maxfd is guaranteed to be >= -1. We call
112 select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
113 no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
114 to sleep 100ms, which is the minimum suggested value in the
115 curl_multi_fdset() doc. */
122 /* Portable sleep for platforms other than Windows. */
123 struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
124 rc = select(0, NULL, NULL, NULL, &wait);
128 /* Note that on some platforms 'timeout' may be modified by select().
129 If you need access to the original value save a copy beforehand. */
130 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
137 case 0: /* timeout */
138 default: /* action */
139 curl_multi_perform(multi_handle, &still_running);
142 } while(still_running);
144 /* See how the transfers went */
145 while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
146 if (msg->msg == CURLMSG_DONE) {
149 /* Find out which handle this message is about */
150 for (idx=0; idx<HANDLECOUNT; idx++) {
151 found = (msg->easy_handle == handles[idx]);
158 printf("HTTP transfer completed with status %d\n", msg->data.result);
161 printf("FTP transfer completed with status %d\n", msg->data.result);
167 curl_multi_cleanup(multi_handle);
169 /* Free the CURL handles */
170 for (i=0; i<HANDLECOUNT; i++)
171 curl_easy_cleanup(handles[i]);