multi-double.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. * multi interface code doing two parallel HTTP transfers
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. /* curl stuff */
  31. #include <curl/curl.h>
  32. /*
  33. * Simply download two HTTP files!
  34. */
  35. int main(void)
  36. {
  37. CURL *http_handle;
  38. CURL *http_handle2;
  39. CURLM *multi_handle;
  40. int still_running = 1; /* keep number of running handles */
  41. http_handle = curl_easy_init();
  42. http_handle2 = curl_easy_init();
  43. /* set options */
  44. curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/");
  45. /* set options */
  46. curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/");
  47. /* init a multi stack */
  48. multi_handle = curl_multi_init();
  49. /* add the individual transfers */
  50. curl_multi_add_handle(multi_handle, http_handle);
  51. curl_multi_add_handle(multi_handle, http_handle2);
  52. while(still_running) {
  53. CURLMsg *msg;
  54. int queued;
  55. CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
  56. if(still_running)
  57. /* wait for activity, timeout or "nothing" */
  58. mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
  59. if(mc)
  60. break;
  61. do {
  62. msg = curl_multi_info_read(multi_handle, &queued);
  63. if(msg) {
  64. if(msg->msg == CURLMSG_DONE) {
  65. /* a transfer ended */
  66. fprintf(stderr, "Transfer completed\n");
  67. }
  68. }
  69. } while(msg);
  70. }
  71. curl_multi_remove_handle(multi_handle, http_handle);
  72. curl_multi_remove_handle(multi_handle, http_handle2);
  73. curl_multi_cleanup(multi_handle);
  74. curl_easy_cleanup(http_handle);
  75. curl_easy_cleanup(http_handle2);
  76. return 0;
  77. }