1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, 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 https://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 ***************************************************************************/
23 * A basic application source code using the multi interface doing two
24 * transfers in parallel.
31 /* somewhat unix-specific */
36 #include <curl/curl.h>
39 * Download a HTTP file and upload an FTP file simultaneously.
42 #define HANDLECOUNT 2 /* Number of simultaneous transfers */
43 #define HTTP_HANDLE 0 /* Index for the HTTP transfer */
44 #define FTP_HANDLE 1 /* Index for the FTP transfer */
48 CURL *handles[HANDLECOUNT];
51 int still_running; /* keep number of running handles */
54 CURLMsg *msg; /* for picking up messages with the transfer status */
55 int msgs_left; /* how many messages are left */
57 /* Allocate one CURL handle per transfer */
58 for(i=0; i<HANDLECOUNT; i++)
59 handles[i] = curl_easy_init();
61 /* set the options (I left out a few, you'll get the point anyway) */
62 curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "http://example.com");
64 curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://example.com");
65 curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
67 /* init a multi stack */
68 multi_handle = curl_multi_init();
70 /* add the individual transfers */
71 for(i=0; i<HANDLECOUNT; i++)
72 curl_multi_add_handle(multi_handle, handles[i]);
74 /* we start some action by calling perform right away */
75 curl_multi_perform(multi_handle, &still_running);
78 struct timeval timeout;
79 int rc; /* select() return code */
80 CURLMcode mc; /* curl_multi_fdset() return code */
93 /* set a suitable timeout to play around with */
97 curl_multi_timeout(multi_handle, &curl_timeo);
99 timeout.tv_sec = curl_timeo / 1000;
100 if(timeout.tv_sec > 1)
103 timeout.tv_usec = (curl_timeo % 1000) * 1000;
106 /* get file descriptors from the transfers */
107 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
110 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
114 /* On success the value of maxfd is guaranteed to be >= -1. We call
115 select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
116 no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
117 to sleep 100ms, which is the minimum suggested value in the
118 curl_multi_fdset() doc. */
125 /* Portable sleep for platforms other than Windows. */
126 struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
127 rc = select(0, NULL, NULL, NULL, &wait);
131 /* Note that on some platforms 'timeout' may be modified by select().
132 If you need access to the original value save a copy beforehand. */
133 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
140 case 0: /* timeout */
141 default: /* action */
142 curl_multi_perform(multi_handle, &still_running);
145 } while(still_running);
147 /* See how the transfers went */
148 while((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
149 if(msg->msg == CURLMSG_DONE) {
152 /* Find out which handle this message is about */
153 for(idx=0; idx<HANDLECOUNT; idx++) {
154 found = (msg->easy_handle == handles[idx]);
161 printf("HTTP transfer completed with status %d\n", msg->data.result);
164 printf("FTP transfer completed with status %d\n", msg->data.result);
170 curl_multi_cleanup(multi_handle);
172 /* Free the CURL handles */
173 for(i=0; i<HANDLECOUNT; i++)
174 curl_easy_cleanup(handles[i]);