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 ***************************************************************************/
25 /* somewhat unix-specific */
30 #include <curl/curl.h>
33 * Simply download two HTTP files!
41 int still_running; /* keep number of running handles */
43 http_handle = curl_easy_init();
44 http_handle2 = curl_easy_init();
47 curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
50 curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/");
52 /* init a multi stack */
53 multi_handle = curl_multi_init();
55 /* add the individual transfers */
56 curl_multi_add_handle(multi_handle, http_handle);
57 curl_multi_add_handle(multi_handle, http_handle2);
59 /* we start some action by calling perform right away */
60 curl_multi_perform(multi_handle, &still_running);
63 struct timeval timeout;
64 int rc; /* select() return code */
65 CURLMcode mc; /* curl_multi_fdset() return code */
78 /* set a suitable timeout to play around with */
82 curl_multi_timeout(multi_handle, &curl_timeo);
84 timeout.tv_sec = curl_timeo / 1000;
85 if(timeout.tv_sec > 1)
88 timeout.tv_usec = (curl_timeo % 1000) * 1000;
91 /* get file descriptors from the transfers */
92 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
96 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
100 /* On success the value of maxfd is guaranteed to be >= -1. We call
101 select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
102 no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
103 to sleep 100ms, which is the minimum suggested value in the
104 curl_multi_fdset() doc. */
111 /* Portable sleep for platforms other than Windows. */
112 struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
113 rc = select(0, NULL, NULL, NULL, &wait);
117 /* Note that on some platforms 'timeout' may be modified by select().
118 If you need access to the original value save a copy beforehand. */
119 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
128 /* timeout or readable/writable sockets */
129 curl_multi_perform(multi_handle, &still_running);
132 } while(still_running);
134 curl_multi_cleanup(multi_handle);
136 curl_easy_cleanup(http_handle);
137 curl_easy_cleanup(http_handle2);