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/* This is a very simple example using the multi interface. */ 23 24#include <stdio.h> 25#include <string.h> 26 27/* somewhat unix-specific */ 28#include <sys/time.h> 29#include <unistd.h> 30 31/* curl stuff */ 32#include <curl/curl.h> 33 34/* 35 * Simply download a HTTP file. 36 */ 37int main(void) 38{ 39 CURL *http_handle; 40 CURLM *multi_handle; 41 42 int still_running; /* keep number of running handles */ 43 44 http_handle = curl_easy_init(); 45 46 /* set the options (I left out a few, you'll get the point anyway) */ 47 curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/"); 48 49 /* init a multi stack */ 50 multi_handle = curl_multi_init(); 51 52 /* add the individual transfers */ 53 curl_multi_add_handle(multi_handle, http_handle); 54 55 /* we start some action by calling perform right away */ 56 curl_multi_perform(multi_handle, &still_running); 57 58 while(still_running) { 59 struct timeval timeout; 60 int rc; /* select() return code */ 61 62 fd_set fdread; 63 fd_set fdwrite; 64 fd_set fdexcep; 65 int maxfd = -1; 66 67 long curl_timeo = -1; 68 69 FD_ZERO(&fdread); 70 FD_ZERO(&fdwrite); 71 FD_ZERO(&fdexcep); 72 73 /* set a suitable timeout to play around with */ 74 timeout.tv_sec = 1; 75 timeout.tv_usec = 0; 76 77 curl_multi_timeout(multi_handle, &curl_timeo); 78 if(curl_timeo >= 0) { 79 timeout.tv_sec = curl_timeo / 1000; 80 if(timeout.tv_sec > 1) 81 timeout.tv_sec = 1; 82 else 83 timeout.tv_usec = (curl_timeo % 1000) * 1000; 84 } 85 86 /* get file descriptors from the transfers */ 87 curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); 88 89 /* In a real-world program you OF COURSE check the return code of the 90 function calls. On success, the value of maxfd is guaranteed to be 91 greater or equal than -1. We call select(maxfd + 1, ...), specially in 92 case of (maxfd == -1), we call select(0, ...), which is basically equal 93 to sleep. */ 94 95 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout); 96 97 switch(rc) { 98 case -1: 99 /* select error */ 100 still_running = 0; 101 printf("select() returns error, this is badness\n"); 102 break; 103 case 0: 104 default: 105 /* timeout or readable/writable sockets */ 106 curl_multi_perform(multi_handle, &still_running); 107 break; 108 } 109 } 110 111 curl_multi_cleanup(multi_handle); 112 113 curl_easy_cleanup(http_handle); 114 115 return 0; 116} 117