Git init
[external/curl.git] / docs / examples / multi-double.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  *
9  * This is a very simple example using the multi interface.
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14
15 /* somewhat unix-specific */
16 #include <sys/time.h>
17 #include <unistd.h>
18
19 /* curl stuff */
20 #include <curl/curl.h>
21
22 /*
23  * Simply download two HTTP files!
24  */
25 int main(int argc, char **argv)
26 {
27   CURL *http_handle;
28   CURL *http_handle2;
29   CURLM *multi_handle;
30
31   int still_running; /* keep number of running handles */
32
33   http_handle = curl_easy_init();
34   http_handle2 = curl_easy_init();
35
36   /* set options */
37   curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
38
39   /* set options */
40   curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/");
41
42   /* init a multi stack */
43   multi_handle = curl_multi_init();
44
45   /* add the individual transfers */
46   curl_multi_add_handle(multi_handle, http_handle);
47   curl_multi_add_handle(multi_handle, http_handle2);
48
49   /* we start some action by calling perform right away */
50   curl_multi_perform(multi_handle, &still_running);
51
52   while(still_running) {
53     struct timeval timeout;
54     int rc; /* select() return code */
55
56     fd_set fdread;
57     fd_set fdwrite;
58     fd_set fdexcep;
59     int maxfd = -1;
60
61     long curl_timeo = -1;
62
63     FD_ZERO(&fdread);
64     FD_ZERO(&fdwrite);
65     FD_ZERO(&fdexcep);
66
67     /* set a suitable timeout to play around with */
68     timeout.tv_sec = 1;
69     timeout.tv_usec = 0;
70
71     curl_multi_timeout(multi_handle, &curl_timeo);
72     if(curl_timeo >= 0) {
73       timeout.tv_sec = curl_timeo / 1000;
74       if(timeout.tv_sec > 1)
75         timeout.tv_sec = 1;
76       else
77         timeout.tv_usec = (curl_timeo % 1000) * 1000;
78     }
79
80     /* get file descriptors from the transfers */
81     curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
82
83     /* In a real-world program you OF COURSE check the return code of the
84        function calls.  On success, the value of maxfd is guaranteed to be
85        greater or equal than -1.  We call select(maxfd + 1, ...), specially in
86        case of (maxfd == -1), we call select(0, ...), which is basically equal
87        to sleep. */
88
89     rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
90
91     switch(rc) {
92     case -1:
93       /* select error */
94       break;
95     case 0:
96     default:
97       /* timeout or readable/writable sockets */
98       curl_multi_perform(multi_handle, &still_running);
99       break;
100     }
101   }
102
103   curl_multi_cleanup(multi_handle);
104
105   curl_easy_cleanup(http_handle);
106   curl_easy_cleanup(http_handle2);
107
108   return 0;
109 }