10-at-a-time.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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 https://curl.haxx.se/docs/copyright.html.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 /* <DESC>
23  * Source code using the multi interface to download many
24  * files, with a capped maximum amount of simultaneous transfers.
25  * </DESC>
26  * Written by Michael Wallner
27  */
28 
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #ifndef WIN32
33 # include <unistd.h>
34 #endif
35 #include <curl/multi.h>
36 
37 static const char *urls[] = {
38  "http://www.microsoft.com",
39  "http://www.opensource.org",
40  "http://www.google.com",
41  "http://www.yahoo.com",
42  "http://www.ibm.com",
43  "http://www.mysql.com",
44  "http://www.oracle.com",
45  "http://www.ripe.net",
46  "http://www.iana.org",
47  "http://www.amazon.com",
48  "http://www.netcraft.com",
49  "http://www.heise.de",
50  "http://www.chip.de",
51  "http://www.ca.com",
52  "http://www.cnet.com",
53  "http://www.news.com",
54  "http://www.cnn.com",
55  "http://www.wikipedia.org",
56  "http://www.dell.com",
57  "http://www.hp.com",
58  "http://www.cert.org",
59  "http://www.mit.edu",
60  "http://www.nist.gov",
61  "http://www.ebay.com",
62  "http://www.playstation.com",
63  "http://www.uefa.com",
64  "http://www.ieee.org",
65  "http://www.apple.com",
66  "http://www.symantec.com",
67  "http://www.zdnet.com",
68  "http://www.fujitsu.com",
69  "http://www.supermicro.com",
70  "http://www.hotmail.com",
71  "http://www.ecma.com",
72  "http://www.bbc.co.uk",
73  "http://news.google.com",
74  "http://www.foxnews.com",
75  "http://www.msn.com",
76  "http://www.wired.com",
77  "http://www.sky.com",
78  "http://www.usatoday.com",
79  "http://www.cbs.com",
80  "http://www.nbc.com",
81  "http://slashdot.org",
82  "http://www.bloglines.com",
83  "http://www.techweb.com",
84  "http://www.newslink.org",
85  "http://www.un.org",
86 };
87 
88 #define MAX 10 /* number of simultaneous transfers */
89 #define CNT sizeof(urls)/sizeof(char *) /* total number of transfers to do */
90 
91 static size_t cb(char *d, size_t n, size_t l, void *p)
92 {
93  /* take care of the data here, ignored in this example */
94  (void)d;
95  (void)p;
96  return n*l;
97 }
98 
99 static void init(CURLM *cm, int i)
100 {
101  CURL *eh = curl_easy_init();
102 
103  curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, cb);
104  curl_easy_setopt(eh, CURLOPT_HEADER, 0L);
105  curl_easy_setopt(eh, CURLOPT_URL, urls[i]);
106  curl_easy_setopt(eh, CURLOPT_PRIVATE, urls[i]);
107  curl_easy_setopt(eh, CURLOPT_VERBOSE, 0L);
108 
109  curl_multi_add_handle(cm, eh);
110 }
111 
112 int main(void)
113 {
114  CURLM *cm;
115  CURLMsg *msg;
116  long L;
117  unsigned int C = 0;
118  int M, Q, U = -1;
119  fd_set R, W, E;
120  struct timeval T;
121 
123 
124  cm = curl_multi_init();
125 
126  /* we can optionally limit the total amount of connections this multi handle
127  uses */
128  curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX);
129 
130  for(C = 0; C < MAX; ++C) {
131  init(cm, C);
132  }
133 
134  while(U) {
135  curl_multi_perform(cm, &U);
136 
137  if(U) {
138  FD_ZERO(&R);
139  FD_ZERO(&W);
140  FD_ZERO(&E);
141 
142  if(curl_multi_fdset(cm, &R, &W, &E, &M)) {
143  fprintf(stderr, "E: curl_multi_fdset\n");
144  return EXIT_FAILURE;
145  }
146 
147  if(curl_multi_timeout(cm, &L)) {
148  fprintf(stderr, "E: curl_multi_timeout\n");
149  return EXIT_FAILURE;
150  }
151  if(L == -1)
152  L = 100;
153 
154  if(M == -1) {
155 #ifdef WIN32
156  Sleep(L);
157 #else
158  sleep((unsigned int)L / 1000);
159 #endif
160  }
161  else {
162  T.tv_sec = L/1000;
163  T.tv_usec = (L%1000)*1000;
164 
165  if(0 > select(M + 1, &R, &W, &E, &T)) {
166  fprintf(stderr, "E: select(%i,,,,%li): %i: %s\n",
167  M + 1, L, errno, strerror(errno));
168  return EXIT_FAILURE;
169  }
170  }
171  }
172 
173  while((msg = curl_multi_info_read(cm, &Q))) {
174  if(msg->msg == CURLMSG_DONE) {
175  char *url;
176  CURL *e = msg->easy_handle;
178  fprintf(stderr, "R: %d - %s <%s>\n",
179  msg->data.result, curl_easy_strerror(msg->data.result), url);
182  }
183  else {
184  fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
185  }
186  if(C < CNT) {
187  init(cm, C++);
188  U++; /* just to prevent it from remaining at 0 if there are more
189  URLs to get */
190  }
191  }
192  }
193 
194  curl_multi_cleanup(cm);
196 
197  return EXIT_SUCCESS;
198 }
CURL_EXTERN CURLMcode curl_multi_fdset(CURLM *multi_handle, fd_set *read_fd_set, fd_set *write_fd_set, fd_set *exc_fd_set, int *max_fd)
CURL_EXTERN CURLMcode curl_multi_add_handle(CURLM *multi_handle, CURL *curl_handle)
int main(void)
Definition: 10-at-a-time.c:112
CURL * easy_handle
Definition: multi.h:95
CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle, int *running_handles)
CURL_EXTERN CURLMcode curl_multi_timeout(CURLM *multi_handle, long *milliseconds)
Definition: multi.h:93
static void init(CURLM *cm, int i)
Definition: 10-at-a-time.c:99
#define curl_easy_setopt(handle, option, value)
Definition: typecheck-gcc.h:41
const char ** p
Definition: unit1394.c:76
static CURL * eh[NUM_HANDLES]
Definition: lib540.c:49
unsigned int i
Definition: unit1303.c:79
CURL_EXTERN CURLM * curl_multi_init(void)
Definition: multi.c:355
#define curl_multi_setopt(handle, opt, param)
#define curl_easy_getinfo(handle, info, arg)
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
CURLMSG msg
Definition: multi.h:94
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
CURL_EXTERN CURLMcode curl_multi_remove_handle(CURLM *multi_handle, CURL *curl_handle)
CURL_EXTERN CURLcode curl_global_init(long flags)
curl_global_init() globally initializes curl given a bitwise set of the different features of what to...
Definition: easy.c:271
static const char * urls[]
Definition: 10-at-a-time.c:37
void CURLM
Definition: multi.h:58
static size_t cb(char *d, size_t n, size_t l, void *p)
Definition: 10-at-a-time.c:91
void CURL
Definition: curl.h:102
CURL_EXTERN CURLMsg * curl_multi_info_read(CURLM *multi_handle, int *msgs_in_queue)
#define CNT
Definition: 10-at-a-time.c:89
union CURLMsg::@6 data
#define fprintf
Definition: curl_printf.h:41
CURL_EXTERN void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
#define CURL_GLOBAL_ALL
Definition: curl.h:2519
CURLcode result
Definition: multi.h:98
#define MAX
Definition: 10-at-a-time.c:88
CURL_EXTERN const char * curl_easy_strerror(CURLcode)
Definition: strerror.c:57
CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle)


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:08