1 /*****************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: 10-at-a-time.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
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
22 #include <curl/multi.h>
24 static const char *urls
[] = {
25 "http://www.microsoft.com",
26 "http://www.opensource.org",
27 "http://www.google.com",
28 "http://www.yahoo.com",
30 "http://www.mysql.com",
31 "http://www.oracle.com",
32 "http://www.ripe.net",
33 "http://www.iana.org",
34 "http://www.amazon.com",
35 "http://www.netcraft.com",
36 "http://www.heise.de",
39 "http://www.cnet.com",
40 "http://www.news.com",
42 "http://www.wikipedia.org",
43 "http://www.dell.com",
45 "http://www.cert.org",
47 "http://www.nist.gov",
48 "http://www.ebay.com",
49 "http://www.playstation.com",
50 "http://www.uefa.com",
51 "http://www.ieee.org",
52 "http://www.apple.com",
53 "http://www.sony.com",
54 "http://www.symantec.com",
55 "http://www.zdnet.com",
56 "http://www.fujitsu.com",
57 "http://www.supermicro.com",
58 "http://www.hotmail.com",
59 "http://www.ecma.com",
60 "http://www.bbc.co.uk",
61 "http://news.google.com",
62 "http://www.foxnews.com",
64 "http://www.wired.com",
66 "http://www.usatoday.com",
69 "http://slashdot.org",
70 "http://www.bloglines.com",
71 "http://www.techweb.com",
72 "http://www.newslink.org",
76 #define MAX 10 /* number of simultaneous transfers */
77 #define CNT sizeof(urls)/sizeof(char*) /* total number of transfers to do */
79 static size_t cb(char *d
, size_t n
, size_t l
, void *p
)
81 /* take care of the data here, ignored in this example */
87 static void init(CURLM
*cm
, int i
)
89 CURL
*eh
= curl_easy_init();
91 curl_easy_setopt(eh
, CURLOPT_WRITEFUNCTION
, cb
);
92 curl_easy_setopt(eh
, CURLOPT_HEADER
, 0L);
93 curl_easy_setopt(eh
, CURLOPT_URL
, urls
[i
]);
94 curl_easy_setopt(eh
, CURLOPT_PRIVATE
, urls
[i
]);
95 curl_easy_setopt(eh
, CURLOPT_VERBOSE
, 0L);
97 curl_multi_add_handle(cm
, eh
);
110 curl_global_init(CURL_GLOBAL_ALL
);
112 cm
= curl_multi_init();
114 /* we can optionally limit the total amount of connections this multi handle
116 curl_multi_setopt(cm
, CURLMOPT_MAXCONNECTS
, MAX
);
118 for (C
= 0; C
< MAX
; ++C
) {
123 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(cm
, &U
));
130 if (curl_multi_fdset(cm
, &R
, &W
, &E
, &M
)) {
131 fprintf(stderr
, "E: curl_multi_fdset\n");
135 if (curl_multi_timeout(cm
, &L
)) {
136 fprintf(stderr
, "E: curl_multi_timeout\n");
150 T
.tv_usec
= (L
%1000)*1000;
152 if (0 > select(M
+1, &R
, &W
, &E
, &T
)) {
153 fprintf(stderr
, "E: select(%i,,,,%li): %i: %s\n",
154 M
+1, L
, errno
, strerror(errno
));
160 while ((msg
= curl_multi_info_read(cm
, &Q
))) {
161 if (msg
->msg
== CURLMSG_DONE
) {
163 CURL
*e
= msg
->easy_handle
;
164 curl_easy_getinfo(msg
->easy_handle
, CURLINFO_PRIVATE
, &url
);
165 fprintf(stderr
, "R: %d - %s <%s>\n",
166 msg
->data
.result
, curl_easy_strerror(msg
->data
.result
), url
);
167 curl_multi_remove_handle(cm
, e
);
168 curl_easy_cleanup(e
);
171 fprintf(stderr
, "E: CURLMsg (%d)\n", msg
->msg
);
175 U
++; /* just to prevent it from remaining at 0 if there are more
181 curl_multi_cleanup(cm
);
182 curl_global_cleanup();