multi-double.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  * multi interface code doing two parallel HTTP transfers
00024  * </DESC>
00025  */
00026 #include <stdio.h>
00027 #include <string.h>
00028 
00029 /* somewhat unix-specific */
00030 #include <sys/time.h>
00031 #include <unistd.h>
00032 
00033 /* curl stuff */
00034 #include <curl/curl.h>
00035 
00036 /*
00037  * Simply download two HTTP files!
00038  */
00039 int main(void)
00040 {
00041   CURL *http_handle;
00042   CURL *http_handle2;
00043   CURLM *multi_handle;
00044 
00045   int still_running; /* keep number of running handles */
00046 
00047   http_handle = curl_easy_init();
00048   http_handle2 = curl_easy_init();
00049 
00050   /* set options */
00051   curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
00052 
00053   /* set options */
00054   curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/");
00055 
00056   /* init a multi stack */
00057   multi_handle = curl_multi_init();
00058 
00059   /* add the individual transfers */
00060   curl_multi_add_handle(multi_handle, http_handle);
00061   curl_multi_add_handle(multi_handle, http_handle2);
00062 
00063   /* we start some action by calling perform right away */
00064   curl_multi_perform(multi_handle, &still_running);
00065 
00066   do {
00067     struct timeval timeout;
00068     int rc; /* select() return code */
00069     CURLMcode mc; /* curl_multi_fdset() return code */
00070 
00071     fd_set fdread;
00072     fd_set fdwrite;
00073     fd_set fdexcep;
00074     int maxfd = -1;
00075 
00076     long curl_timeo = -1;
00077 
00078     FD_ZERO(&fdread);
00079     FD_ZERO(&fdwrite);
00080     FD_ZERO(&fdexcep);
00081 
00082     /* set a suitable timeout to play around with */
00083     timeout.tv_sec = 1;
00084     timeout.tv_usec = 0;
00085 
00086     curl_multi_timeout(multi_handle, &curl_timeo);
00087     if(curl_timeo >= 0) {
00088       timeout.tv_sec = curl_timeo / 1000;
00089       if(timeout.tv_sec > 1)
00090         timeout.tv_sec = 1;
00091       else
00092         timeout.tv_usec = (curl_timeo % 1000) * 1000;
00093     }
00094 
00095     /* get file descriptors from the transfers */
00096     mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
00097 
00098     if(mc != CURLM_OK) {
00099       fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
00100       break;
00101     }
00102 
00103     /* On success the value of maxfd is guaranteed to be >= -1. We call
00104        select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
00105        no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
00106        to sleep 100ms, which is the minimum suggested value in the
00107        curl_multi_fdset() doc. */
00108 
00109     if(maxfd == -1) {
00110 #ifdef _WIN32
00111       Sleep(100);
00112       rc = 0;
00113 #else
00114       /* Portable sleep for platforms other than Windows. */
00115       struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
00116       rc = select(0, NULL, NULL, NULL, &wait);
00117 #endif
00118     }
00119     else {
00120       /* Note that on some platforms 'timeout' may be modified by select().
00121          If you need access to the original value save a copy beforehand. */
00122       rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
00123     }
00124 
00125     switch(rc) {
00126     case -1:
00127       /* select error */
00128       break;
00129     case 0:
00130     default:
00131       /* timeout or readable/writable sockets */
00132       curl_multi_perform(multi_handle, &still_running);
00133       break;
00134     }
00135   } while(still_running);
00136 
00137   curl_multi_cleanup(multi_handle);
00138 
00139   curl_easy_cleanup(http_handle);
00140   curl_easy_cleanup(http_handle2);
00141 
00142   return 0;
00143 }


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