1/*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 1998 - 2011, 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 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 */ 35int 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 while(still_running) { 63 struct timeval timeout; 64 int rc; /* select() return code */ 65 66 fd_set fdread; 67 fd_set fdwrite; 68 fd_set fdexcep; 69 int maxfd = -1; 70 71 long curl_timeo = -1; 72 73 FD_ZERO(&fdread); 74 FD_ZERO(&fdwrite); 75 FD_ZERO(&fdexcep); 76 77 /* set a suitable timeout to play around with */ 78 timeout.tv_sec = 1; 79 timeout.tv_usec = 0; 80 81 curl_multi_timeout(multi_handle, &curl_timeo); 82 if(curl_timeo >= 0) { 83 timeout.tv_sec = curl_timeo / 1000; 84 if(timeout.tv_sec > 1) 85 timeout.tv_sec = 1; 86 else 87 timeout.tv_usec = (curl_timeo % 1000) * 1000; 88 } 89 90 /* get file descriptors from the transfers */ 91 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); 92 93 /* In a real-world program you OF COURSE check the return code of the 94 function calls. On success, the value of maxfd is guaranteed to be 95 greater or equal than -1. We call select(maxfd + 1, ...), specially in 96 case of (maxfd == -1), we call select(0, ...), which is basically equal 97 to sleep. */ 98 99 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); 100 101 switch(rc) { 102 case -1: 103 /* select error */ 104 break; 105 case 0: 106 default: 107 /* timeout or readable/writable sockets */ 108 curl_multi_perform(multi_handle, &still_running); 109 break; 110 } 111 } 112 113 curl_multi_cleanup(multi_handle); 114 115 curl_easy_cleanup(http_handle); 116 curl_easy_cleanup(http_handle2); 117 118 return 0; 119} 120