10-at-a-time.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * Source code using the multi interface to download many
00024  * files, with a capped maximum amount of simultaneous transfers.
00025  * </DESC>
00026  * Written by Michael Wallner
00027  */
00028 
00029 #include <errno.h>
00030 #include <stdlib.h>
00031 #include <string.h>
00032 #ifndef WIN32
00033 #  include <unistd.h>
00034 #endif
00035 #include <curl/multi.h>
00036 
00037 static const char *urls[] = {
00038   "http://www.microsoft.com",
00039   "http://www.opensource.org",
00040   "http://www.google.com",
00041   "http://www.yahoo.com",
00042   "http://www.ibm.com",
00043   "http://www.mysql.com",
00044   "http://www.oracle.com",
00045   "http://www.ripe.net",
00046   "http://www.iana.org",
00047   "http://www.amazon.com",
00048   "http://www.netcraft.com",
00049   "http://www.heise.de",
00050   "http://www.chip.de",
00051   "http://www.ca.com",
00052   "http://www.cnet.com",
00053   "http://www.news.com",
00054   "http://www.cnn.com",
00055   "http://www.wikipedia.org",
00056   "http://www.dell.com",
00057   "http://www.hp.com",
00058   "http://www.cert.org",
00059   "http://www.mit.edu",
00060   "http://www.nist.gov",
00061   "http://www.ebay.com",
00062   "http://www.playstation.com",
00063   "http://www.uefa.com",
00064   "http://www.ieee.org",
00065   "http://www.apple.com",
00066   "http://www.symantec.com",
00067   "http://www.zdnet.com",
00068   "http://www.fujitsu.com",
00069   "http://www.supermicro.com",
00070   "http://www.hotmail.com",
00071   "http://www.ecma.com",
00072   "http://www.bbc.co.uk",
00073   "http://news.google.com",
00074   "http://www.foxnews.com",
00075   "http://www.msn.com",
00076   "http://www.wired.com",
00077   "http://www.sky.com",
00078   "http://www.usatoday.com",
00079   "http://www.cbs.com",
00080   "http://www.nbc.com",
00081   "http://slashdot.org",
00082   "http://www.bloglines.com",
00083   "http://www.techweb.com",
00084   "http://www.newslink.org",
00085   "http://www.un.org",
00086 };
00087 
00088 #define MAX 10 /* number of simultaneous transfers */
00089 #define CNT sizeof(urls)/sizeof(char *) /* total number of transfers to do */
00090 
00091 static size_t cb(char *d, size_t n, size_t l, void *p)
00092 {
00093   /* take care of the data here, ignored in this example */
00094   (void)d;
00095   (void)p;
00096   return n*l;
00097 }
00098 
00099 static void init(CURLM *cm, int i)
00100 {
00101   CURL *eh = curl_easy_init();
00102 
00103   curl_easy_setopt(eh, CURLOPT_WRITEFUNCTION, cb);
00104   curl_easy_setopt(eh, CURLOPT_HEADER, 0L);
00105   curl_easy_setopt(eh, CURLOPT_URL, urls[i]);
00106   curl_easy_setopt(eh, CURLOPT_PRIVATE, urls[i]);
00107   curl_easy_setopt(eh, CURLOPT_VERBOSE, 0L);
00108 
00109   curl_multi_add_handle(cm, eh);
00110 }
00111 
00112 int main(void)
00113 {
00114   CURLM *cm;
00115   CURLMsg *msg;
00116   long L;
00117   unsigned int C=0;
00118   int M, Q, U = -1;
00119   fd_set R, W, E;
00120   struct timeval T;
00121 
00122   curl_global_init(CURL_GLOBAL_ALL);
00123 
00124   cm = curl_multi_init();
00125 
00126   /* we can optionally limit the total amount of connections this multi handle
00127      uses */
00128   curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX);
00129 
00130   for(C = 0; C < MAX; ++C) {
00131     init(cm, C);
00132   }
00133 
00134   while(U) {
00135     curl_multi_perform(cm, &U);
00136 
00137     if(U) {
00138       FD_ZERO(&R);
00139       FD_ZERO(&W);
00140       FD_ZERO(&E);
00141 
00142       if(curl_multi_fdset(cm, &R, &W, &E, &M)) {
00143         fprintf(stderr, "E: curl_multi_fdset\n");
00144         return EXIT_FAILURE;
00145       }
00146 
00147       if(curl_multi_timeout(cm, &L)) {
00148         fprintf(stderr, "E: curl_multi_timeout\n");
00149         return EXIT_FAILURE;
00150       }
00151       if(L == -1)
00152         L = 100;
00153 
00154       if(M == -1) {
00155 #ifdef WIN32
00156         Sleep(L);
00157 #else
00158         sleep((unsigned int)L / 1000);
00159 #endif
00160       }
00161       else {
00162         T.tv_sec = L/1000;
00163         T.tv_usec = (L%1000)*1000;
00164 
00165         if(0 > select(M+1, &R, &W, &E, &T)) {
00166           fprintf(stderr, "E: select(%i,,,,%li): %i: %s\n",
00167               M+1, L, errno, strerror(errno));
00168           return EXIT_FAILURE;
00169         }
00170       }
00171     }
00172 
00173     while((msg = curl_multi_info_read(cm, &Q))) {
00174       if(msg->msg == CURLMSG_DONE) {
00175         char *url;
00176         CURL *e = msg->easy_handle;
00177         curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url);
00178         fprintf(stderr, "R: %d - %s <%s>\n",
00179                 msg->data.result, curl_easy_strerror(msg->data.result), url);
00180         curl_multi_remove_handle(cm, e);
00181         curl_easy_cleanup(e);
00182       }
00183       else {
00184         fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg);
00185       }
00186       if(C < CNT) {
00187         init(cm, C++);
00188         U++; /* just to prevent it from remaining at 0 if there are more
00189                 URLs to get */
00190       }
00191     }
00192   }
00193 
00194   curl_multi_cleanup(cm);
00195   curl_global_cleanup();
00196 
00197   return EXIT_SUCCESS;
00198 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:01