multi-single.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
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 https://curl.haxx.se/docs/copyright.html.
13  *
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.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 /* <DESC>
23  * using the multi interface to do a single download
24  * </DESC>
25  */
26 
27 #include <stdio.h>
28 #include <string.h>
29 
30 /* somewhat unix-specific */
31 #include <sys/time.h>
32 #include <unistd.h>
33 
34 /* curl stuff */
35 #include <curl/curl.h>
36 
37 #ifdef _WIN32
38 #define WAITMS(x) Sleep(x)
39 #else
40 /* Portable sleep for platforms other than Windows. */
41 #define WAITMS(x) \
42  struct timeval wait = { 0, (x) * 1000 }; \
43  (void)select(0, NULL, NULL, NULL, &wait);
44 #endif
45 
46 /*
47  * Simply download a HTTP file.
48  */
49 int main(void)
50 {
51  CURL *http_handle;
53 
54  int still_running; /* keep number of running handles */
55  int repeats = 0;
56 
58 
59  http_handle = curl_easy_init();
60 
61  /* set the options (I left out a few, you'll get the point anyway) */
62  curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
63 
64  /* init a multi stack */
65  multi_handle = curl_multi_init();
66 
67  /* add the individual transfers */
68  curl_multi_add_handle(multi_handle, http_handle);
69 
70  /* we start some action by calling perform right away */
71  curl_multi_perform(multi_handle, &still_running);
72 
73  do {
74  CURLMcode mc; /* curl_multi_wait() return code */
75  int numfds;
76 
77  /* wait for activity, timeout or "nothing" */
78  mc = curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds);
79 
80  if(mc != CURLM_OK) {
81  fprintf(stderr, "curl_multi_wait() failed, code %d.\n", mc);
82  break;
83  }
84 
85  /* 'numfds' being zero means either a timeout or no file descriptors to
86  wait for. Try timeout on first occurrence, then assume no file
87  descriptors and no file descriptors to wait for means wait for 100
88  milliseconds. */
89 
90  if(!numfds) {
91  repeats++; /* count number of repeated zero numfds */
92  if(repeats > 1) {
93  WAITMS(100); /* sleep 100 milliseconds */
94  }
95  }
96  else
97  repeats = 0;
98 
99  curl_multi_perform(multi_handle, &still_running);
100  } while(still_running);
101 
102  curl_multi_remove_handle(multi_handle, http_handle);
103 
104  curl_easy_cleanup(http_handle);
105 
106  curl_multi_cleanup(multi_handle);
107 
109 
110  return 0;
111 }
CURL_EXTERN CURLMcode curl_multi_add_handle(CURLM *multi_handle, CURL *curl_handle)
#define WAITMS(x)
Definition: multi-single.c:41
CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle, int *running_handles)
#define curl_easy_setopt(handle, option, value)
Definition: typecheck-gcc.h:41
CURL_EXTERN CURLM * curl_multi_init(void)
Definition: multi.c:355
static CURLM * multi_handle
Definition: fopen.c:91
#define CURL_GLOBAL_DEFAULT
Definition: curl.h:2521
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
int main(void)
Definition: multi-single.c:49
CURL_EXTERN CURLMcode curl_multi_remove_handle(CURLM *multi_handle, CURL *curl_handle)
CURLMcode
Definition: multi.h:61
CURL_EXTERN CURLcode curl_global_init(long flags)
curl_global_init() globally initializes curl given a bitwise set of the different features of what to...
Definition: easy.c:271
void CURLM
Definition: multi.h:58
CURL_EXTERN CURLMcode curl_multi_wait(CURLM *multi_handle, struct curl_waitfd extra_fds[], unsigned int extra_nfds, int timeout_ms, int *ret)
void CURL
Definition: curl.h:102
Definition: multi.h:64
#define fprintf
Definition: curl_printf.h:41
CURL_EXTERN void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle)


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:16