multi-app.c 3.5 KB

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