1
/*****************************************************************************
3
* Project ___| | | | _ \| |
5
* | (__| |_| | _ <| |___
6
* \___|\___/|_| \_\_____|
8
* $Id: 10-at-a-time.c,v 1.2 2006-10-13 14:01:19 bagder 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
17
#include <curl/multi.h>
19
static const char *urls[] = {
20
"http://www.microsoft.com",
21
"http://www.opensource.org",
22
"http://www.google.com",
23
"http://www.yahoo.com",
25
"http://www.mysql.com",
26
"http://www.oracle.com",
27
"http://www.ripe.net",
28
"http://www.iana.org",
29
"http://www.amazon.com",
30
"http://www.netcraft.com",
31
"http://www.heise.de",
34
"http://www.cnet.com",
35
"http://www.news.com",
37
"http://www.wikipedia.org",
38
"http://www.dell.com",
40
"http://www.cert.org",
42
"http://www.nist.gov",
43
"http://www.ebay.com",
44
"http://www.playstation.com",
45
"http://www.uefa.com",
46
"http://www.ieee.org",
47
"http://www.apple.com",
48
"http://www.sony.com",
49
"http://www.symantec.com",
50
"http://www.zdnet.com",
51
"http://www.fujitsu.com",
52
"http://www.supermicro.com",
53
"http://www.hotmail.com",
54
"http://www.ecma.com",
55
"http://www.bbc.co.uk",
56
"http://news.google.com",
57
"http://www.foxnews.com",
59
"http://www.wired.com",
61
"http://www.usatoday.com",
64
"http://slashdot.org",
65
"http://www.bloglines.com",
66
"http://www.techweb.com",
67
"http://www.newslink.org",
71
#define MAX 10 /* number of simultaneous transfers */
72
#define CNT sizeof(urls)/sizeof(char*) /* total number of transfers to do */
74
static int cb(char *d, size_t n, size_t l, void *p)
76
/* take care of the data here, ignored in this example */
82
static void init(CURLM *cm, int i)
84
CURL *eh = curl_easy_init();
86
curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, cb);
87
curl_easy_setopt(eh, CURLOPT_HEADER, 0);
88
curl_easy_setopt(eh, CURLOPT_URL, urls[i]);
89
curl_easy_setopt(eh, CURLOPT_PRIVATE, urls[i]);
90
curl_easy_setopt(eh, CURLOPT_VERBOSE, 0);
92
curl_multi_add_handle(cm, eh);
105
curl_global_init(CURL_GLOBAL_ALL);
107
cm = curl_multi_init();
109
for (C = 0; C < MAX; ++C) {
114
while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(cm, &U));
121
if (curl_multi_fdset(cm, &R, &W, &E, &M)) {
122
fprintf(stderr, "E: curl_multi_fdset\n");
126
/* In a real-world program you OF COURSE check the return that maxfd is
127
bigger than -1 so that the call to select() below makes sense! */
129
if (curl_multi_timeout(cm, &L)) {
130
fprintf(stderr, "E: curl_multi_timeout\n");
135
T.tv_usec = (L%1000)*1000;
137
if (0 > select(M+1, &R, &W, &E, &T)) {
138
fprintf(stderr, "E: select\n");
143
while ((msg = curl_multi_info_read(cm, &Q))) {
144
if (msg->msg == CURLMSG_DONE) {
146
CURL *e = msg->easy_handle;
147
curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url);
148
fprintf(stderr, "R: %d - %s <%s>\n",
149
msg->data.result, curl_easy_strerror(msg->data.result), url);
150
curl_multi_remove_handle(cm, e);
151
curl_easy_cleanup(e);
154
fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
162
curl_multi_cleanup(cm);
163
curl_global_cleanup();