00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00089 #define CNT sizeof(urls)/sizeof(char *)
00090
00091 static size_t cb(char *d, size_t n, size_t l, void *p)
00092 {
00093
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
00127
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++;
00189
00190 }
00191 }
00192 }
00193
00194 curl_multi_cleanup(cm);
00195 curl_global_cleanup();
00196
00197 return EXIT_SUCCESS;
00198 }