1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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 #define WAITMS(x) Sleep(x)
37 /* Portable sleep for platforms other than Windows. */
39 struct timeval wait = { 0, (x) * 1000 }; \
40 (void)select(0, NULL, NULL, NULL, &wait);
44 * Simply download a HTTP file.
51 int still_running; /* keep number of running handles */
54 curl_global_init(CURL_GLOBAL_DEFAULT);
56 http_handle = curl_easy_init();
58 /* set the options (I left out a few, you'll get the point anyway) */
59 curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
61 /* init a multi stack */
62 multi_handle = curl_multi_init();
64 /* add the individual transfers */
65 curl_multi_add_handle(multi_handle, http_handle);
67 /* we start some action by calling perform right away */
68 curl_multi_perform(multi_handle, &still_running);
71 CURLMcode mc; /* curl_multi_wait() return code */
74 /* wait for activity, timeout or "nothing" */
75 mc = curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds);
79 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
83 /* 'numfds' being zero means either a timeout or no file descriptors to
84 wait for. Try timeout on first occurance, then assume no file
85 descriptors and no file descriptors to wait for means wait for 100
89 repeats++; /* count number of repeated zero numfds */
91 WAITMS(100); /* sleep 100 milliseconds */
97 curl_multi_perform(multi_handle, &still_running);
98 } while(still_running);
100 curl_multi_remove_handle(multi_handle, http_handle);
102 curl_easy_cleanup(http_handle);
104 curl_multi_cleanup(multi_handle);
106 curl_global_cleanup();