1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, 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 ***************************************************************************/
23 * multi interface code doing two parallel HTTP transfers
29 /* somewhat unix-specific */
34 #include <curl/curl.h>
37 * Simply download two HTTP files!
45 int still_running; /* keep number of running handles */
47 http_handle = curl_easy_init();
48 http_handle2 = curl_easy_init();
51 curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
54 curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/");
56 /* init a multi stack */
57 multi_handle = curl_multi_init();
59 /* add the individual transfers */
60 curl_multi_add_handle(multi_handle, http_handle);
61 curl_multi_add_handle(multi_handle, http_handle2);
63 /* we start some action by calling perform right away */
64 curl_multi_perform(multi_handle, &still_running);
67 struct timeval timeout;
68 int rc; /* select() return code */
69 CURLMcode mc; /* curl_multi_fdset() return code */
82 /* set a suitable timeout to play around with */
86 curl_multi_timeout(multi_handle, &curl_timeo);
88 timeout.tv_sec = curl_timeo / 1000;
89 if(timeout.tv_sec > 1)
92 timeout.tv_usec = (curl_timeo % 1000) * 1000;
95 /* get file descriptors from the transfers */
96 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
100 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
104 /* On success the value of maxfd is guaranteed to be >= -1. We call
105 select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
106 no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
107 to sleep 100ms, which is the minimum suggested value in the
108 curl_multi_fdset() doc. */
115 /* Portable sleep for platforms other than Windows. */
116 struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
117 rc = select(0, NULL, NULL, NULL, &wait);
121 /* Note that on some platforms 'timeout' may be modified by select().
122 If you need access to the original value save a copy beforehand. */
123 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
132 /* timeout or readable/writable sockets */
133 curl_multi_perform(multi_handle, &still_running);
136 } while(still_running);
138 curl_multi_cleanup(multi_handle);
140 curl_easy_cleanup(http_handle);
141 curl_easy_cleanup(http_handle2);