• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * A basic application source code using the multi interface doing two
24  * transfers in parallel.
25  * </DESC>
26  */
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  * Download a HTTP file and upload an FTP file simultaneously.
40  */
41 
42 #define HANDLECOUNT 2   /* Number of simultaneous transfers */
43 #define HTTP_HANDLE 0   /* Index for the HTTP transfer */
44 #define FTP_HANDLE 1    /* Index for the FTP transfer */
45 
main(void)46 int main(void)
47 {
48   CURL *handles[HANDLECOUNT];
49   CURLM *multi_handle;
50 
51   int still_running = 1; /* keep number of running handles */
52   int i;
53 
54   CURLMsg *msg; /* for picking up messages with the transfer status */
55   int msgs_left; /* how many messages are left */
56 
57   /* Allocate one CURL handle per transfer */
58   for(i = 0; i<HANDLECOUNT; i++)
59     handles[i] = curl_easy_init();
60 
61   /* set the options (I left out a few, you'll get the point anyway) */
62   curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "https://example.com");
63 
64   curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://example.com");
65   curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
66 
67   /* init a multi stack */
68   multi_handle = curl_multi_init();
69 
70   /* add the individual transfers */
71   for(i = 0; i<HANDLECOUNT; i++)
72     curl_multi_add_handle(multi_handle, handles[i]);
73 
74   while(still_running) {
75     CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
76 
77     if(still_running)
78       /* wait for activity, timeout or "nothing" */
79       mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
80 
81     if(mc)
82       break;
83   }
84   /* See how the transfers went */
85   while((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
86     if(msg->msg == CURLMSG_DONE) {
87       int idx;
88 
89       /* Find out which handle this message is about */
90       for(idx = 0; idx<HANDLECOUNT; idx++) {
91         int found = (msg->easy_handle == handles[idx]);
92         if(found)
93           break;
94       }
95 
96       switch(idx) {
97       case HTTP_HANDLE:
98         printf("HTTP transfer completed with status %d\n", msg->data.result);
99         break;
100       case FTP_HANDLE:
101         printf("FTP transfer completed with status %d\n", msg->data.result);
102         break;
103       }
104     }
105   }
106 
107   curl_multi_cleanup(multi_handle);
108 
109   /* Free the CURL handles */
110   for(i = 0; i<HANDLECOUNT; i++)
111     curl_easy_cleanup(handles[i]);
112 
113   return 0;
114 }
115