multi-single.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * using the multi interface to do a single download
00024  * </DESC>
00025  */
00026 
00027 #include <stdio.h>
00028 #include <string.h>
00029 
00030 /* somewhat unix-specific */
00031 #include <sys/time.h>
00032 #include <unistd.h>
00033 
00034 /* curl stuff */
00035 #include <curl/curl.h>
00036 
00037 #ifdef _WIN32
00038 #define WAITMS(x) Sleep(x)
00039 #else
00040 /* Portable sleep for platforms other than Windows. */
00041 #define WAITMS(x)                               \
00042   struct timeval wait = { 0, (x) * 1000 };      \
00043   (void)select(0, NULL, NULL, NULL, &wait);
00044 #endif
00045 
00046 /*
00047  * Simply download a HTTP file.
00048  */
00049 int main(void)
00050 {
00051   CURL *http_handle;
00052   CURLM *multi_handle;
00053 
00054   int still_running; /* keep number of running handles */
00055   int repeats = 0;
00056 
00057   curl_global_init(CURL_GLOBAL_DEFAULT);
00058 
00059   http_handle = curl_easy_init();
00060 
00061   /* set the options (I left out a few, you'll get the point anyway) */
00062   curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
00063 
00064   /* init a multi stack */
00065   multi_handle = curl_multi_init();
00066 
00067   /* add the individual transfers */
00068   curl_multi_add_handle(multi_handle, http_handle);
00069 
00070   /* we start some action by calling perform right away */
00071   curl_multi_perform(multi_handle, &still_running);
00072 
00073   do {
00074     CURLMcode mc; /* curl_multi_wait() return code */
00075     int numfds;
00076 
00077     /* wait for activity, timeout or "nothing" */
00078     mc = curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds);
00079 
00080     if(mc != CURLM_OK) {
00081       fprintf(stderr, "curl_multi_wait() failed, code %d.\n", mc);
00082       break;
00083     }
00084 
00085     /* 'numfds' being zero means either a timeout or no file descriptors to
00086        wait for. Try timeout on first occurrence, then assume no file
00087        descriptors and no file descriptors to wait for means wait for 100
00088        milliseconds. */
00089 
00090     if(!numfds) {
00091       repeats++; /* count number of repeated zero numfds */
00092       if(repeats > 1) {
00093         WAITMS(100); /* sleep 100 milliseconds */
00094       }
00095     }
00096     else
00097       repeats = 0;
00098 
00099     curl_multi_perform(multi_handle, &still_running);
00100   } while(still_running);
00101 
00102   curl_multi_remove_handle(multi_handle, http_handle);
00103 
00104   curl_easy_cleanup(http_handle);
00105 
00106   curl_multi_cleanup(multi_handle);
00107 
00108   curl_global_cleanup();
00109 
00110   return 0;
00111 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:05