multi-single.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. http_handle = curl_easy_init();
  39. /* set the options (I left out a few, you'll get the point anyway) */
  40. curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
  41. /* init a multi stack */
  42. multi_handle = curl_multi_init();
  43. /* add the individual transfers */
  44. curl_multi_add_handle(multi_handle, http_handle);
  45. /* we start some action by calling perform right away */
  46. curl_multi_perform(multi_handle, &still_running);
  47. while(still_running) {
  48. struct timeval timeout;
  49. int rc; /* select() return code */
  50. fd_set fdread;
  51. fd_set fdwrite;
  52. fd_set fdexcep;
  53. int maxfd = -1;
  54. long curl_timeo = -1;
  55. FD_ZERO(&fdread);
  56. FD_ZERO(&fdwrite);
  57. FD_ZERO(&fdexcep);
  58. /* set a suitable timeout to play around with */
  59. timeout.tv_sec = 1;
  60. timeout.tv_usec = 0;
  61. curl_multi_timeout(multi_handle, &curl_timeo);
  62. if(curl_timeo >= 0) {
  63. timeout.tv_sec = curl_timeo / 1000;
  64. if(timeout.tv_sec > 1)
  65. timeout.tv_sec = 1;
  66. else
  67. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  68. }
  69. /* get file descriptors from the transfers */
  70. curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  71. /* In a real-world program you OF COURSE check the return code of the
  72. function calls. On success, the value of maxfd is guaranteed to be
  73. greater or equal than -1. We call select(maxfd + 1, ...), specially in
  74. case of (maxfd == -1), we call select(0, ...), which is basically equal
  75. to sleep. */
  76. rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  77. switch(rc) {
  78. case -1:
  79. /* select error */
  80. still_running = 0;
  81. printf("select() returns error, this is badness\n");
  82. break;
  83. case 0:
  84. default:
  85. /* timeout or readable/writable sockets */
  86. curl_multi_perform(multi_handle, &still_running);
  87. break;
  88. }
  89. }
  90. curl_multi_cleanup(multi_handle);
  91. curl_easy_cleanup(http_handle);
  92. return 0;
  93. }