multi interface using examples
[platform/upstream/curl.git] / docs / examples / multi-double.c
1 /*
2  * This is a simple example using the multi interface.
3  */
4
5 #include <stdio.h>
6 #include <string.h>
7
8 /* somewhat unix-specific */
9 #include <sys/time.h>
10 #include <unistd.h>
11
12 /* To start with, we include the header from the lib directory. This should
13    later of course be moved to the proper include dir. */
14 #include "../lib/multi.h"
15
16 /*
17  * Simply download two HTTP files!
18  */
19 int main(int argc, char **argv)
20 {
21   CURL *http_handle;
22   CURL *http_handle2;
23   CURLM *multi_handle;
24
25   int still_running; /* keep number of running handles */
26
27   http_handle = curl_easy_init();
28   http_handle2 = curl_easy_init();
29
30   /* set options */
31   curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.haxx.se/");
32
33   /* set options */
34   curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/");
35
36   /* init a multi stack */
37   multi_handle = curl_multi_init();
38
39   /* add the individual transfers */
40   curl_multi_add_handle(multi_handle, http_handle);
41   curl_multi_add_handle(multi_handle, http_handle2);
42
43   /* we start some action by calling perform right away */
44   while(CURLM_CALL_MULTI_PERFORM ==
45         curl_multi_perform(multi_handle, &still_running));
46
47   while(still_running) {
48     struct timeval timeout;
49     int rc; /* select() return code */
50
51     fd_set fdread;
52     fd_set fdwrite;
53     fd_set fdexcep;
54     int maxfd;
55
56     FD_ZERO(&fdread);
57     FD_ZERO(&fdwrite);
58     FD_ZERO(&fdexcep);
59
60     /* set a suitable timeout to play around with */
61     timeout.tv_sec = 1;
62     timeout.tv_usec = 0;
63
64     /* get file descriptors from the transfers */
65     curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
66
67     rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
68
69     switch(rc) {
70     case -1:
71       /* select error */
72       break;
73     case 0:
74     default:
75       /* timeout or readable/writable sockets */
76       curl_multi_perform(multi_handle, &still_running);
77       break;
78     }
79   }
80
81   curl_multi_cleanup(multi_handle);
82
83   curl_easy_cleanup(http_handle);
84   curl_easy_cleanup(http_handle2);
85
86   return 0;
87 }