1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
10 * Example application source code using the multi interface to download many
11 * files, but with a capped maximum amount of simultaneous transfers.
13 * Written by Michael Wallner
20 #include <curl/multi.h>
25 static const char *urls[] = {
26 "http://www.microsoft.com",
27 "http://www.opensource.org",
28 "http://www.google.com",
29 "http://www.yahoo.com",
31 "http://www.mysql.com",
32 "http://www.oracle.com",
33 "http://www.ripe.net",
34 "http://www.iana.org",
35 "http://www.amazon.com",
36 "http://www.netcraft.com",
37 "http://www.heise.de",
40 "http://www.cnet.com",
41 "http://www.news.com",
43 "http://www.wikipedia.org",
44 "http://www.dell.com",
46 "http://www.cert.org",
48 "http://www.nist.gov",
49 "http://www.ebay.com",
50 "http://www.playstation.com",
51 "http://www.uefa.com",
52 "http://www.ieee.org",
53 "http://www.apple.com",
54 "http://www.sony.com",
55 "http://www.symantec.com",
56 "http://www.zdnet.com",
57 "http://www.fujitsu.com",
58 "http://www.supermicro.com",
59 "http://www.hotmail.com",
60 "http://www.ecma.com",
61 "http://www.bbc.co.uk",
62 "http://news.google.com",
63 "http://www.foxnews.com",
65 "http://www.wired.com",
67 "http://www.usatoday.com",
70 "http://slashdot.org",
71 "http://www.bloglines.com",
72 "http://www.techweb.com",
73 "http://www.newslink.org",
77 #define MAX 10 /* number of simultaneous transfers */
78 #define CNT sizeof(urls)/sizeof(char*) /* total number of transfers to do */
80 static size_t cb(char *d, size_t n, size_t l, void *p)
82 /* take care of the data here, ignored in this example */
88 static void init(CURLM *cm, int i)
90 CURL *eh = curl_easy_init();
92 curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, cb);
93 curl_easy_setopt(eh, CURLOPT_HEADER, 0);
94 curl_easy_setopt(eh, CURLOPT_URL, urls[i]);
95 curl_easy_setopt(eh, CURLOPT_PRIVATE, urls[i]);
96 curl_easy_setopt(eh, CURLOPT_VERBOSE, 0);
98 curl_multi_add_handle(cm, eh);
111 curl_global_init(CURL_GLOBAL_ALL);
113 cm = curl_multi_init();
115 /* we can optionally limit the total amount of connections this multi handle
117 curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, MAX);
119 for (C = 0; C < MAX; ++C) {
124 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(cm, &U));
131 if (curl_multi_fdset(cm, &R, &W, &E, &M)) {
132 fprintf(stderr, "E: curl_multi_fdset\n");
136 if (curl_multi_timeout(cm, &L)) {
137 fprintf(stderr, "E: curl_multi_timeout\n");
151 T.tv_usec = (L%1000)*1000;
153 if (0 > select(M+1, &R, &W, &E, &T)) {
154 fprintf(stderr, "E: select(%i,,,,%li): %i: %s\n",
155 M+1, L, errno, strerror(errno));
161 while ((msg = curl_multi_info_read(cm, &Q))) {
162 if (msg->msg == CURLMSG_DONE) {
164 CURL *e = msg->easy_handle;
165 curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url);
166 fprintf(stderr, "R: %d - %s <%s>\n",
167 msg->data.result, curl_easy_strerror(msg->data.result), url);
168 curl_multi_remove_handle(cm, e);
169 curl_easy_cleanup(e);
172 fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
176 U++; /* just to prevent it from remaining at 0 if there are more
182 curl_multi_cleanup(cm);
183 curl_global_cleanup();