1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at http://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
22 /* This is a very simple example using the multi interface. */
27 /* somewhat unix-specific */
32 #include <curl/curl.h>
35 * Simply download a HTTP file.
42 int still_running; /* keep number of running handles */
44 curl_global_init(CURL_GLOBAL_DEFAULT);
46 http_handle = curl_easy_init();
48 /* set the options (I left out a few, you'll get the point anyway) */
49 curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
51 /* init a multi stack */
52 multi_handle = curl_multi_init();
54 /* add the individual transfers */
55 curl_multi_add_handle(multi_handle, http_handle);
57 /* we start some action by calling perform right away */
58 curl_multi_perform(multi_handle, &still_running);
61 struct timeval timeout;
62 int rc; /* select() return code */
75 /* set a suitable timeout to play around with */
79 curl_multi_timeout(multi_handle, &curl_timeo);
81 timeout.tv_sec = curl_timeo / 1000;
82 if(timeout.tv_sec > 1)
85 timeout.tv_usec = (curl_timeo % 1000) * 1000;
88 /* get file descriptors from the transfers */
89 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
91 /* In a real-world program you OF COURSE check the return code of the
92 function calls. On success, the value of maxfd is guaranteed to be
93 greater or equal than -1. We call select(maxfd + 1, ...), specially in
94 case of (maxfd == -1), we call select(0, ...), which is basically equal
97 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
103 printf("select() returns error, this is badness\n");
107 /* timeout or readable/writable sockets */
108 curl_multi_perform(multi_handle, &still_running);
111 } while(still_running);
113 curl_multi_remove_handle(multi_handle, http_handle);
115 curl_easy_cleanup(http_handle);
117 curl_multi_cleanup(multi_handle);
119 curl_global_cleanup();