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