multi-single.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. * using the multi interface to do a single download
  26. * </DESC>
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. /* curl stuff */
  31. #include <curl/curl.h>
  32. /*
  33. * Simply download an HTTP file.
  34. */
  35. int main(void)
  36. {
  37. CURL *http_handle;
  38. CURLM *multi_handle;
  39. int still_running = 1; /* keep number of running handles */
  40. curl_global_init(CURL_GLOBAL_DEFAULT);
  41. http_handle = curl_easy_init();
  42. /* set the options (I left out a few, you get the point anyway) */
  43. curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/");
  44. /* init a multi stack */
  45. multi_handle = curl_multi_init();
  46. /* add the individual transfers */
  47. curl_multi_add_handle(multi_handle, http_handle);
  48. do {
  49. CURLMcode mc = curl_multi_perform(multi_handle, &still_running);
  50. if(!mc)
  51. /* wait for activity, timeout or "nothing" */
  52. mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL);
  53. if(mc) {
  54. fprintf(stderr, "curl_multi_poll() failed, code %d.\n", (int)mc);
  55. break;
  56. }
  57. } while(still_running);
  58. curl_multi_remove_handle(multi_handle, http_handle);
  59. curl_easy_cleanup(http_handle);
  60. curl_multi_cleanup(multi_handle);
  61. curl_global_cleanup();
  62. return 0;
  63. }