1/*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 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 https://curl.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 * SPDX-License-Identifier: curl 22 * 23 ***************************************************************************/ 24/* <DESC> 25 * multi interface code doing two parallel HTTP transfers 26 * </DESC> 27 */ 28#include <stdio.h> 29#include <string.h> 30 31/* somewhat unix-specific */ 32#include <sys/time.h> 33#include <unistd.h> 34 35/* curl stuff */ 36#include <curl/curl.h> 37 38/* 39 * Simply download two HTTP files! 40 */ 41int main(void) 42{ 43 CURL *http_handle; 44 CURL *http_handle2; 45 CURLM *multi_handle; 46 47 int still_running = 1; /* keep number of running handles */ 48 49 http_handle = curl_easy_init(); 50 http_handle2 = curl_easy_init(); 51 52 /* set options */ 53 curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/"); 54 55 /* set options */ 56 curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/"); 57 58 /* init a multi stack */ 59 multi_handle = curl_multi_init(); 60 61 /* add the individual transfers */ 62 curl_multi_add_handle(multi_handle, http_handle); 63 curl_multi_add_handle(multi_handle, http_handle2); 64 65 while(still_running) { 66 CURLMsg *msg; 67 int queued; 68 CURLMcode mc = curl_multi_perform(multi_handle, &still_running); 69 70 if(still_running) 71 /* wait for activity, timeout or "nothing" */ 72 mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL); 73 74 if(mc) 75 break; 76 77 do { 78 msg = curl_multi_info_read(multi_handle, &queued); 79 if(msg) { 80 if(msg->msg == CURLMSG_DONE) { 81 /* a transfer ended */ 82 fprintf(stderr, "Transfer completed\n"); 83 } 84 } 85 } while(msg); 86 } 87 88 curl_multi_remove_handle(multi_handle, http_handle); 89 curl_multi_remove_handle(multi_handle, http_handle2); 90 91 curl_multi_cleanup(multi_handle); 92 93 curl_easy_cleanup(http_handle); 94 curl_easy_cleanup(http_handle2); 95 96 return 0; 97} 98