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