multi-double.c 2.6 KB

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