2
0

multi-app.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #include <stdio.h>
  30. #include <string.h>
  31. /* curl stuff */
  32. #include <curl/curl.h>
  33. /*
  34. * Download an HTTP file and upload an FTP file simultaneously.
  35. */
  36. #define HANDLECOUNT 2 /* Number of simultaneous transfers */
  37. #define HTTP_HANDLE 0 /* Index for the HTTP transfer */
  38. #define FTP_HANDLE 1 /* Index for the FTP transfer */
  39. int main(void)
  40. {
  41. CURL *handles[HANDLECOUNT];
  42. CURLM *multi_handle;
  43. int still_running = 1; /* keep number of running handles */
  44. int i;
  45. CURLMsg *msg; /* for picking up messages with the transfer status */
  46. int msgs_left; /* how many messages are left */
  47. /* Allocate one CURL handle per transfer */
  48. for(i = 0; i<HANDLECOUNT; i++)
  49. handles[i] = curl_easy_init();
  50. /* set the options (I left out a few, you get the point anyway) */
  51. curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "https://example.com");
  52. curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://example.com");
  53. curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
  54. /* init a multi stack */
  55. multi_handle = curl_multi_init();
  56. /* add the individual transfers */
  57. for(i = 0; i<HANDLECOUNT; i++)
  58. curl_multi_add_handle(multi_handle, handles[i]);
  59. while(still_running) {
  60. CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
  61. if(still_running)
  62. /* wait for activity, timeout or "nothing" */
  63. mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
  64. if(mc)
  65. break;
  66. }
  67. /* See how the transfers went */
  68. /* !checksrc! disable EQUALSNULL 1 */
  69. while((msg = curl_multi_info_read(multi_handle, &msgs_left)) != NULL) {
  70. if(msg->msg == CURLMSG_DONE) {
  71. int idx;
  72. /* Find out which handle this message is about */
  73. for(idx = 0; idx<HANDLECOUNT; idx++) {
  74. int found = (msg->easy_handle == handles[idx]);
  75. if(found)
  76. break;
  77. }
  78. switch(idx) {
  79. case HTTP_HANDLE:
  80. printf("HTTP transfer completed with status %d\n", msg->data.result);
  81. break;
  82. case FTP_HANDLE:
  83. printf("FTP transfer completed with status %d\n", msg->data.result);
  84. break;
  85. }
  86. }
  87. }
  88. /* remove the transfers and cleanup the handles */
  89. for(i = 0; i<HANDLECOUNT; i++) {
  90. curl_multi_remove_handle(multi_handle, handles[i]);
  91. curl_easy_cleanup(handles[i]);
  92. }
  93. curl_multi_cleanup(multi_handle);
  94. return 0;
  95. }