multi-double.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. #include <stdio.h>
  23. #include <string.h>
  24. /* somewhat unix-specific */
  25. #include <sys/time.h>
  26. #include <unistd.h>
  27. /* curl stuff */
  28. #include <curl/curl.h>
  29. /*
  30. * Simply download two HTTP files!
  31. */
  32. int main(void)
  33. {
  34. CURL *http_handle;
  35. CURL *http_handle2;
  36. CURLM *multi_handle;
  37. int still_running; /* keep number of running handles */
  38. http_handle = curl_easy_init();
  39. http_handle2 = curl_easy_init();
  40. /* set options */
  41. curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
  42. /* set options */
  43. curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/");
  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. curl_multi_add_handle(multi_handle, http_handle2);
  49. /* we start some action by calling perform right away */
  50. curl_multi_perform(multi_handle, &still_running);
  51. while(still_running) {
  52. struct timeval timeout;
  53. int rc; /* select() return code */
  54. fd_set fdread;
  55. fd_set fdwrite;
  56. fd_set fdexcep;
  57. int maxfd = -1;
  58. long curl_timeo = -1;
  59. FD_ZERO(&fdread);
  60. FD_ZERO(&fdwrite);
  61. FD_ZERO(&fdexcep);
  62. /* set a suitable timeout to play around with */
  63. timeout.tv_sec = 1;
  64. timeout.tv_usec = 0;
  65. curl_multi_timeout(multi_handle, &curl_timeo);
  66. if(curl_timeo >= 0) {
  67. timeout.tv_sec = curl_timeo / 1000;
  68. if(timeout.tv_sec > 1)
  69. timeout.tv_sec = 1;
  70. else
  71. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  72. }
  73. /* get file descriptors from the transfers */
  74. curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  75. /* In a real-world program you OF COURSE check the return code of the
  76. function calls. On success, the value of maxfd is guaranteed to be
  77. greater or equal than -1. We call select(maxfd + 1, ...), specially in
  78. case of (maxfd == -1), we call select(0, ...), which is basically equal
  79. to sleep. */
  80. rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  81. switch(rc) {
  82. case -1:
  83. /* select error */
  84. break;
  85. case 0:
  86. default:
  87. /* timeout or readable/writable sockets */
  88. curl_multi_perform(multi_handle, &still_running);
  89. break;
  90. }
  91. }
  92. curl_multi_cleanup(multi_handle);
  93. curl_easy_cleanup(http_handle);
  94. curl_easy_cleanup(http_handle2);
  95. return 0;
  96. }