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