Home | History | Annotate | Download | only in examples
      1 /***************************************************************************
      2  *                                  _   _ ____  _
      3  *  Project                     ___| | | |  _ \| |
      4  *                             / __| | | | |_) | |
      5  *                            | (__| |_| |  _ <| |___
      6  *                             \___|\___/|_| \_\_____|
      7  *
      8  * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel (at) 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 http://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 #include <stdio.h>
     23 #include <string.h>
     24 
     25 /* somewhat unix-specific */
     26 #include <sys/time.h>
     27 #include <unistd.h>
     28 
     29 /* curl stuff */
     30 #include <curl/curl.h>
     31 
     32 /*
     33  * Simply download two HTTP files!
     34  */
     35 int main(void)
     36 {
     37   CURL *http_handle;
     38   CURL *http_handle2;
     39   CURLM *multi_handle;
     40 
     41   int still_running; /* keep number of running handles */
     42 
     43   http_handle = curl_easy_init();
     44   http_handle2 = curl_easy_init();
     45 
     46   /* set options */
     47   curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
     48 
     49   /* set options */
     50   curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/");
     51 
     52   /* init a multi stack */
     53   multi_handle = curl_multi_init();
     54 
     55   /* add the individual transfers */
     56   curl_multi_add_handle(multi_handle, http_handle);
     57   curl_multi_add_handle(multi_handle, http_handle2);
     58 
     59   /* we start some action by calling perform right away */
     60   curl_multi_perform(multi_handle, &still_running);
     61 
     62   do {
     63     struct timeval timeout;
     64     int rc; /* select() return code */
     65     CURLMcode mc; /* curl_multi_fdset() return code */
     66 
     67     fd_set fdread;
     68     fd_set fdwrite;
     69     fd_set fdexcep;
     70     int maxfd = -1;
     71 
     72     long curl_timeo = -1;
     73 
     74     FD_ZERO(&fdread);
     75     FD_ZERO(&fdwrite);
     76     FD_ZERO(&fdexcep);
     77 
     78     /* set a suitable timeout to play around with */
     79     timeout.tv_sec = 1;
     80     timeout.tv_usec = 0;
     81 
     82     curl_multi_timeout(multi_handle, &curl_timeo);
     83     if(curl_timeo >= 0) {
     84       timeout.tv_sec = curl_timeo / 1000;
     85       if(timeout.tv_sec > 1)
     86         timeout.tv_sec = 1;
     87       else
     88         timeout.tv_usec = (curl_timeo % 1000) * 1000;
     89     }
     90 
     91     /* get file descriptors from the transfers */
     92     mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
     93 
     94     if(mc != CURLM_OK)
     95     {
     96       fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
     97       break;
     98     }
     99 
    100     /* On success the value of maxfd is guaranteed to be >= -1. We call
    101        select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
    102        no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
    103        to sleep 100ms, which is the minimum suggested value in the
    104        curl_multi_fdset() doc. */
    105 
    106     if(maxfd == -1) {
    107 #ifdef _WIN32
    108       Sleep(100);
    109       rc = 0;
    110 #else
    111       /* Portable sleep for platforms other than Windows. */
    112       struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
    113       rc = select(0, NULL, NULL, NULL, &wait);
    114 #endif
    115     }
    116     else {
    117       /* Note that on some platforms 'timeout' may be modified by select().
    118          If you need access to the original value save a copy beforehand. */
    119       rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
    120     }
    121 
    122     switch(rc) {
    123     case -1:
    124       /* select error */
    125       break;
    126     case 0:
    127     default:
    128       /* timeout or readable/writable sockets */
    129       curl_multi_perform(multi_handle, &still_running);
    130       break;
    131     }
    132   } while(still_running);
    133 
    134   curl_multi_cleanup(multi_handle);
    135 
    136   curl_easy_cleanup(http_handle);
    137   curl_easy_cleanup(http_handle2);
    138 
    139   return 0;
    140 }
    141