2
0

multi-double.c 2.6 KB

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