multi-single.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, 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 http://curl.haxx.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. /* This is a very simple example using the multi interface. */
  23. #include <stdio.h>
  24. #include <string.h>
  25. /* somewhat unix-specific */
  26. #include <sys/time.h>
  27. #include <unistd.h>
  28. /* curl stuff */
  29. #include <curl/curl.h>
  30. /*
  31. * Simply download a HTTP file.
  32. */
  33. int main(void)
  34. {
  35. CURL *http_handle;
  36. CURLM *multi_handle;
  37. int still_running; /* keep number of running handles */
  38. curl_global_init(CURL_GLOBAL_DEFAULT);
  39. http_handle = curl_easy_init();
  40. /* set the options (I left out a few, you'll get the point anyway) */
  41. curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
  42. /* init a multi stack */
  43. multi_handle = curl_multi_init();
  44. /* add the individual transfers */
  45. curl_multi_add_handle(multi_handle, http_handle);
  46. /* we start some action by calling perform right away */
  47. curl_multi_perform(multi_handle, &still_running);
  48. do {
  49. struct timeval timeout;
  50. int rc; /* select() return code */
  51. fd_set fdread;
  52. fd_set fdwrite;
  53. fd_set fdexcep;
  54. int maxfd = -1;
  55. long curl_timeo = -1;
  56. FD_ZERO(&fdread);
  57. FD_ZERO(&fdwrite);
  58. FD_ZERO(&fdexcep);
  59. /* set a suitable timeout to play around with */
  60. timeout.tv_sec = 1;
  61. timeout.tv_usec = 0;
  62. curl_multi_timeout(multi_handle, &curl_timeo);
  63. if(curl_timeo >= 0) {
  64. timeout.tv_sec = curl_timeo / 1000;
  65. if(timeout.tv_sec > 1)
  66. timeout.tv_sec = 1;
  67. else
  68. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  69. }
  70. /* get file descriptors from the transfers */
  71. curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  72. /* In a real-world program you OF COURSE check the return code of the
  73. function calls. On success, the value of maxfd is guaranteed to be
  74. greater or equal than -1. We call select(maxfd + 1, ...), specially in
  75. case of (maxfd == -1), we call select(0, ...), which is basically equal
  76. to sleep. */
  77. rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  78. switch(rc) {
  79. case -1:
  80. /* select error */
  81. still_running = 0;
  82. printf("select() returns error, this is badness\n");
  83. break;
  84. case 0:
  85. default:
  86. /* timeout or readable/writable sockets */
  87. curl_multi_perform(multi_handle, &still_running);
  88. break;
  89. }
  90. } while(still_running);
  91. curl_multi_remove_handle(multi_handle, http_handle);
  92. curl_easy_cleanup(http_handle);
  93. curl_multi_cleanup(multi_handle);
  94. curl_global_cleanup();
  95. return 0;
  96. }