#include <errno.h> #include <stdlib.h> #include <string.h> #ifndef win32#include <unistd.h># Endif#include <curl/multi.h> Static const char *urls[] = {"http://www.microsoft.com", "http://www.opensource.org "," http://www.google.com "," http://www.yahoo.com "," http://www.ibm.com "," http://www.mysql.com ","/HTTP/ Www.oracle.com "," http://www.ripe.net "," http://www.iana.org "," http://www.amazon.com "," http://www.netcraft.com ", "Http://www.heise.de", "http://www.chip.de", "http://www.ca.com", "http://www.cnet.com", "http://www.news.com", " Http://www.cnn.com "," http://www.wikipedia.org "," http://www.dell.com "," http://www.hp.com "," http://www.cert.org " , "http://www.mit.edu", "http://www.nist.gov"}; #define MAX #define CNT sizeof (URLs)/sizeof (char*) static size_t CB (char *d, size_t N, size_t l, void *p) {/* Take Care of the data here, ignored in this example */(void) D; (void) P; return n*l;} int nindex=0;static void init (CURLM *cm, INT i) {CURL *eh = Curl_easy_init (); Curl_easy_setopt (Eh, curlopt_writefunction, CB);//All downloads Call this callback function curl_easy_setopt (Eh, Curlopt_header, 0L); The output content does not contain the message header curl_easy_setopt (Eh, Curlopt_url, urls[i]); Requested URL curl_easy_setopt (Eh, curlopt_private, urls[i]); In this the URL of the call is saved, back Curl_easy_getinfo (Msg->easy_handle, Curlinfo_private, &url); query out curl_easy_setopt (Eh, Curlopt_verbose, 0L); If you want curl to report every unexpected thing, set this option to a non-0 value curl_easy_setopt (Eh, curlopt_writedata, (void *) &nindex); Set the fourth parameter curl_easy_setopt (Eh, curlopt_useragent, "lavf/55.13.102") to pass the CB function when calling the CB function; Set useragent curl_multi_add_handle (cm, eh);} int main (void) {CURLM *cm; Curlmsg *msg; Long L; unsigned int c=0; int M, Q, U =-1; Fd_set R, W, E; struct Timeval T; Curl_global_init (Curl_global_all); CM = Curl_multi_init (); /* We can optionally limit the total amount of connections this multi handle uses */curl_multi_setopt (cm, Curlmopt_ Maxconnects, (long) MAX); for (C = 0; C < MAX; ++c) {init (cm, C); } while (U) {curl_multi_perform (cm, &u); Perform concurrent requests, non-blocking, and immediately return if (U) {Fd_zero (&r); Fd_zero (&W); Fd_zero (&e); Gets the set of file descriptors that the CM needs to listen to, if the returned m equals-1, need to wait a while and then call Curl_multi_perform again, how long? Recommended at least 100 milliseconds, but you may want to test it under your own specific conditions and find a suitable value if (curl_multi_fdset (cm, &r, &w, &e, &m)) {fprintf (Stder R, "e:curl_multi_fdset\n"); return exit_failure; } if (Curl_multi_timeout (cm, &l)) {fprintf (stderr, "e:curl_multi_timeout\n"); return exit_failure; } if (L = =-1) L = 100; if (M = =-1) {#ifdef WIN32 sleep (L), #else sleep ((unsigned int) l/1000), #endif} else {t.tv _sec = L/1000; T.tv_usec = (l%1000) *1000; /* Determine the status of one or more sockets and query its readability, writeable, and error status information. Return value: The Select () call returns the total number of descriptive words that are in the ready state and are already contained in the FD_SET structure, or 0 if the timeout is exceeded, otherwise the socket_error (-1) error is returned, and the application is available through WSAGetLastError () Gets the appropriate error code. */if (0 > select (m+1, &r, &w, &e, &t)) {fprintf (stderr, "E:select (%i,,,,%li):%i:%s\n", M+1, L, errno, Strerror (errno)); return exit_failure; }}}/* gets the relevant transport information for the currently resolved Curl query whether a batch handle has a message or information returned in a separate transport thread. The message may contain a report such as an error code returned from a separate transport thread, or just a transmission line threads not completed. Call this function repeatedly, it returns a new result each time, until no more information is returned, FALSE is returned as a signal. The integer returned by msgs_in_queue indicates that the number of messages remaining will be included when this function is called. Warning returns a resource that points to a data call that does not exist after Curl_multi_remove_handle (). */while (msg = curl_multi_info_read (cm, &q)) {if (msg->msg = = curlmsg_done) {char *url; CURL *e = msg->easy_handle; Curl_easy_getinfo (Msg->easy_handle, Curlinfo_private, &url); fprintf (stderr, "R:%d-%s <%s>\n", Msg->data.result, Curl_easy_strerror (msg->data.result), ur L); Curl_multi_remove_handle (cm, E); Curl_easy_cleanup (e); } else {fprintf (stderr, "e:curlmsg (%d) \ n", msg->msg); } if (C < CNT) {init (cm, C + +); u++; /* Just to prevent it fromRemaining at 0 if there is more URLs to get */}}} curl_multi_cleanup (cm); Curl_global_cleanup (); return exit_success;}
Libcurl downloading multiple files simultaneously