10-at-a-time.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2019, 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. * Download many files in parallel, in the same thread.
  24. * </DESC>
  25. */
  26. #include <errno.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #ifndef WIN32
  30. # include <unistd.h>
  31. #endif
  32. #include <curl/curl.h>
  33. static const char *urls[] = {
  34. "https://www.microsoft.com",
  35. "https://opensource.org",
  36. "https://www.google.com",
  37. "https://www.yahoo.com",
  38. "https://www.ibm.com",
  39. "https://www.mysql.com",
  40. "https://www.oracle.com",
  41. "https://www.ripe.net",
  42. "https://www.iana.org",
  43. "https://www.amazon.com",
  44. "https://www.netcraft.com",
  45. "https://www.heise.de",
  46. "https://www.chip.de",
  47. "https://www.ca.com",
  48. "https://www.cnet.com",
  49. "https://www.mozilla.org",
  50. "https://www.cnn.com",
  51. "https://www.wikipedia.org",
  52. "https://www.dell.com",
  53. "https://www.hp.com",
  54. "https://www.cert.org",
  55. "https://www.mit.edu",
  56. "https://www.nist.gov",
  57. "https://www.ebay.com",
  58. "https://www.playstation.com",
  59. "https://www.uefa.com",
  60. "https://www.ieee.org",
  61. "https://www.apple.com",
  62. "https://www.symantec.com",
  63. "https://www.zdnet.com",
  64. "https://www.fujitsu.com/global/",
  65. "https://www.supermicro.com",
  66. "https://www.hotmail.com",
  67. "https://www.ietf.org",
  68. "https://www.bbc.co.uk",
  69. "https://news.google.com",
  70. "https://www.foxnews.com",
  71. "https://www.msn.com",
  72. "https://www.wired.com",
  73. "https://www.sky.com",
  74. "https://www.usatoday.com",
  75. "https://www.cbs.com",
  76. "https://www.nbc.com/",
  77. "https://slashdot.org",
  78. "https://www.informationweek.com",
  79. "https://apache.org",
  80. "https://www.un.org",
  81. };
  82. #define MAX_PARALLEL 10 /* number of simultaneous transfers */
  83. #define NUM_URLS sizeof(urls)/sizeof(char *)
  84. static size_t write_cb(char *data, size_t n, size_t l, void *userp)
  85. {
  86. /* take care of the data here, ignored in this example */
  87. (void)data;
  88. (void)userp;
  89. return n*l;
  90. }
  91. static void add_transfer(CURLM *cm, int i)
  92. {
  93. CURL *eh = curl_easy_init();
  94. curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, write_cb);
  95. curl_easy_setopt(eh, CURLOPT_URL, urls[i]);
  96. curl_easy_setopt(eh, CURLOPT_PRIVATE, urls[i]);
  97. curl_multi_add_handle(cm, eh);
  98. }
  99. int main(void)
  100. {
  101. CURLM *cm;
  102. CURLMsg *msg;
  103. unsigned int transfers = 0;
  104. int msgs_left = -1;
  105. int still_alive = 1;
  106. curl_global_init(CURL_GLOBAL_ALL);
  107. cm = curl_multi_init();
  108. /* Limit the amount of simultaneous connections curl should allow: */
  109. curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX_PARALLEL);
  110. for(transfers = 0; transfers < MAX_PARALLEL; transfers++)
  111. add_transfer(cm, transfers);
  112. do {
  113. curl_multi_perform(cm, &still_alive);
  114. while((msg = curl_multi_info_read(cm, &msgs_left))) {
  115. if(msg->msg == CURLMSG_DONE) {
  116. char *url;
  117. CURL *e = msg->easy_handle;
  118. curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url);
  119. fprintf(stderr, "R: %d - %s <%s>\n",
  120. msg->data.result, curl_easy_strerror(msg->data.result), url);
  121. curl_multi_remove_handle(cm, e);
  122. curl_easy_cleanup(e);
  123. }
  124. else {
  125. fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
  126. }
  127. if(transfers < NUM_URLS)
  128. add_transfer(cm, transfers++);
  129. }
  130. if(still_alive)
  131. curl_multi_wait(cm, NULL, 0, 1000, NULL);
  132. } while(still_alive || (transfers < NUM_URLS));
  133. curl_multi_cleanup(cm);
  134. curl_global_cleanup();
  135. return EXIT_SUCCESS;
  136. }