10-at-a-time.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, 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 http://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. /* Example application source code using the multi interface to download many
  23. * files, but with a capped maximum amount of simultaneous transfers.
  24. *
  25. * Written by Michael Wallner
  26. */
  27. #include <errno.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #ifndef WIN32
  31. # include <unistd.h>
  32. #endif
  33. #include <curl/multi.h>
  34. static const char *urls[] = {
  35. "http://www.microsoft.com",
  36. "http://www.opensource.org",
  37. "http://www.google.com",
  38. "http://www.yahoo.com",
  39. "http://www.ibm.com",
  40. "http://www.mysql.com",
  41. "http://www.oracle.com",
  42. "http://www.ripe.net",
  43. "http://www.iana.org",
  44. "http://www.amazon.com",
  45. "http://www.netcraft.com",
  46. "http://www.heise.de",
  47. "http://www.chip.de",
  48. "http://www.ca.com",
  49. "http://www.cnet.com",
  50. "http://www.news.com",
  51. "http://www.cnn.com",
  52. "http://www.wikipedia.org",
  53. "http://www.dell.com",
  54. "http://www.hp.com",
  55. "http://www.cert.org",
  56. "http://www.mit.edu",
  57. "http://www.nist.gov",
  58. "http://www.ebay.com",
  59. "http://www.playstation.com",
  60. "http://www.uefa.com",
  61. "http://www.ieee.org",
  62. "http://www.apple.com",
  63. "http://www.sony.com",
  64. "http://www.symantec.com",
  65. "http://www.zdnet.com",
  66. "http://www.fujitsu.com",
  67. "http://www.supermicro.com",
  68. "http://www.hotmail.com",
  69. "http://www.ecma.com",
  70. "http://www.bbc.co.uk",
  71. "http://news.google.com",
  72. "http://www.foxnews.com",
  73. "http://www.msn.com",
  74. "http://www.wired.com",
  75. "http://www.sky.com",
  76. "http://www.usatoday.com",
  77. "http://www.cbs.com",
  78. "http://www.nbc.com",
  79. "http://slashdot.org",
  80. "http://www.bloglines.com",
  81. "http://www.techweb.com",
  82. "http://www.newslink.org",
  83. "http://www.un.org",
  84. };
  85. #define MAX 10 /* number of simultaneous transfers */
  86. #define CNT sizeof(urls)/sizeof(char*) /* total number of transfers to do */
  87. static size_t cb(char *d, size_t n, size_t l, void *p)
  88. {
  89. /* take care of the data here, ignored in this example */
  90. (void)d;
  91. (void)p;
  92. return n*l;
  93. }
  94. static void init(CURLM *cm, int i)
  95. {
  96. CURL *eh = curl_easy_init();
  97. curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, cb);
  98. curl_easy_setopt(eh, CURLOPT_HEADER, 0L);
  99. curl_easy_setopt(eh, CURLOPT_URL, urls[i]);
  100. curl_easy_setopt(eh, CURLOPT_PRIVATE, urls[i]);
  101. curl_easy_setopt(eh, CURLOPT_VERBOSE, 0L);
  102. curl_multi_add_handle(cm, eh);
  103. }
  104. int main(void)
  105. {
  106. CURLM *cm;
  107. CURLMsg *msg;
  108. long L;
  109. unsigned int C=0;
  110. int M, Q, U = -1;
  111. fd_set R, W, E;
  112. struct timeval T;
  113. curl_global_init(CURL_GLOBAL_ALL);
  114. cm = curl_multi_init();
  115. /* we can optionally limit the total amount of connections this multi handle
  116. uses */
  117. curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX);
  118. for (C = 0; C < MAX; ++C) {
  119. init(cm, C);
  120. }
  121. while (U) {
  122. curl_multi_perform(cm, &U);
  123. if (U) {
  124. FD_ZERO(&R);
  125. FD_ZERO(&W);
  126. FD_ZERO(&E);
  127. if (curl_multi_fdset(cm, &R, &W, &E, &M)) {
  128. fprintf(stderr, "E: curl_multi_fdset\n");
  129. return EXIT_FAILURE;
  130. }
  131. if (curl_multi_timeout(cm, &L)) {
  132. fprintf(stderr, "E: curl_multi_timeout\n");
  133. return EXIT_FAILURE;
  134. }
  135. if (L == -1)
  136. L = 100;
  137. if (M == -1) {
  138. #ifdef WIN32
  139. Sleep(L);
  140. #else
  141. sleep(L / 1000);
  142. #endif
  143. } else {
  144. T.tv_sec = L/1000;
  145. T.tv_usec = (L%1000)*1000;
  146. if (0 > select(M+1, &R, &W, &E, &T)) {
  147. fprintf(stderr, "E: select(%i,,,,%li): %i: %s\n",
  148. M+1, L, errno, strerror(errno));
  149. return EXIT_FAILURE;
  150. }
  151. }
  152. }
  153. while ((msg = curl_multi_info_read(cm, &Q))) {
  154. if (msg->msg == CURLMSG_DONE) {
  155. char *url;
  156. CURL *e = msg->easy_handle;
  157. curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url);
  158. fprintf(stderr, "R: %d - %s <%s>\n",
  159. msg->data.result, curl_easy_strerror(msg->data.result), url);
  160. curl_multi_remove_handle(cm, e);
  161. curl_easy_cleanup(e);
  162. }
  163. else {
  164. fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
  165. }
  166. if (C < CNT) {
  167. init(cm, C++);
  168. U++; /* just to prevent it from remaining at 0 if there are more
  169. URLs to get */
  170. }
  171. }
  172. }
  173. curl_multi_cleanup(cm);
  174. curl_global_cleanup();
  175. return EXIT_SUCCESS;
  176. }