multi-single.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, Daniel Stenberg, <[email protected]>, 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. * using the multi interface to do a single download
  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. #ifdef _WIN32
  34. #define WAITMS(x) Sleep(x)
  35. #else
  36. /* Portable sleep for platforms other than Windows. */
  37. #define WAITMS(x) \
  38. struct timeval wait = { 0, (x) * 1000 }; \
  39. (void)select(0, NULL, NULL, NULL, &wait)
  40. #endif
  41. /*
  42. * Simply download a HTTP file.
  43. */
  44. int main(void)
  45. {
  46. CURL *http_handle;
  47. CURLM *multi_handle;
  48. int still_running = 1; /* keep number of running handles */
  49. curl_global_init(CURL_GLOBAL_DEFAULT);
  50. http_handle = curl_easy_init();
  51. /* set the options (I left out a few, you will get the point anyway) */
  52. curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/");
  53. /* init a multi stack */
  54. multi_handle = curl_multi_init();
  55. /* add the individual transfers */
  56. curl_multi_add_handle(multi_handle, http_handle);
  57. do {
  58. CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
  59. if(!mc)
  60. /* wait for activity, timeout or "nothing" */
  61. mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
  62. if(mc) {
  63. fprintf(stderr, "curl_multi_poll() failed, code %d.\n", (int)mc);
  64. break;
  65. }
  66. } while(still_running);
  67. curl_multi_remove_handle(multi_handle, http_handle);
  68. curl_easy_cleanup(http_handle);
  69. curl_multi_cleanup(multi_handle);
  70. curl_global_cleanup();
  71. return 0;
  72. }