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