Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / docs / examples / multi-single.c
blob0fe6ecc58276ac481fd8831b18e2d21662c7cbc0
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * $Id: multi-single.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
10 * This is a very simple example using the multi interface.
13 #include <stdio.h>
14 #include <string.h>
16 /* somewhat unix-specific */
17 #include <sys/time.h>
18 #include <unistd.h>
20 /* curl stuff */
21 #include <curl/curl.h>
24 * Simply download a HTTP file.
26 int main(int argc, char **argv)
28 CURL *http_handle;
29 CURLM *multi_handle;
31 int still_running; /* keep number of running handles */
33 http_handle = curl_easy_init();
35 /* set the options (I left out a few, you'll get the point anyway) */
36 curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.haxx.se/");
38 /* init a multi stack */
39 multi_handle = curl_multi_init();
41 /* add the individual transfers */
42 curl_multi_add_handle(multi_handle, http_handle);
44 /* we start some action by calling perform right away */
45 while(CURLM_CALL_MULTI_PERFORM ==
46 curl_multi_perform(multi_handle, &still_running));
48 while(still_running) {
49 struct timeval timeout;
50 int rc; /* select() return code */
52 fd_set fdread;
53 fd_set fdwrite;
54 fd_set fdexcep;
55 int maxfd;
57 FD_ZERO(&fdread);
58 FD_ZERO(&fdwrite);
59 FD_ZERO(&fdexcep);
61 /* set a suitable timeout to play around with */
62 timeout.tv_sec = 1;
63 timeout.tv_usec = 0;
65 /* get file descriptors from the transfers */
66 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
68 /* In a real-world program you OF COURSE check the return code of the
69 function calls, *and* you make sure that maxfd is bigger than -1 so
70 that the call to select() below makes sense! */
72 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
74 switch(rc) {
75 case -1:
76 /* select error */
77 still_running = 0;
78 printf("select() returns error, this is badness\n");
79 break;
80 case 0:
81 default:
82 /* timeout or readable/writable sockets */
83 while(CURLM_CALL_MULTI_PERFORM ==
84 curl_multi_perform(multi_handle, &still_running));
85 break;
89 curl_multi_cleanup(multi_handle);
91 curl_easy_cleanup(http_handle);
93 return 0;