xref: /curl/docs/examples/multi-double.c (revision 53b4dfe4)
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 /* curl stuff */
32 #include <curl/curl.h>
33 
34 /*
35  * Simply download two HTTP files!
36  */
main(void)37 int main(void)
38 {
39   CURL *http_handle;
40   CURL *http_handle2;
41   CURLM *multi_handle;
42 
43   int still_running = 1; /* keep number of running handles */
44 
45   http_handle = curl_easy_init();
46   http_handle2 = curl_easy_init();
47 
48   /* set options */
49   curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/");
50 
51   /* set options */
52   curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/");
53 
54   /* init a multi stack */
55   multi_handle = curl_multi_init();
56 
57   /* add the individual transfers */
58   curl_multi_add_handle(multi_handle, http_handle);
59   curl_multi_add_handle(multi_handle, http_handle2);
60 
61   while(still_running) {
62     CURLMsg *msg;
63     int queued;
64     CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
65 
66     if(still_running)
67       /* wait for activity, timeout or "nothing" */
68       mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
69 
70     if(mc)
71       break;
72 
73     do {
74       msg = curl_multi_info_read(multi_handle, &queued);
75       if(msg) {
76         if(msg->msg == CURLMSG_DONE) {
77           /* a transfer ended */
78           fprintf(stderr, "Transfer completed\n");
79         }
80       }
81     } while(msg);
82   }
83 
84   curl_multi_remove_handle(multi_handle, http_handle);
85   curl_multi_remove_handle(multi_handle, http_handle2);
86 
87   curl_multi_cleanup(multi_handle);
88 
89   curl_easy_cleanup(http_handle);
90   curl_easy_cleanup(http_handle2);
91 
92   return 0;
93 }
94