multi-double.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id$
  9. *
  10. * This is a very simple example using the multi interface.
  11. */
  12. #include <stdio.h>
  13. #include <string.h>
  14. /* somewhat unix-specific */
  15. #include <sys/time.h>
  16. #include <unistd.h>
  17. /* curl stuff */
  18. #include <curl/curl.h>
  19. /*
  20. * Simply download two HTTP files!
  21. */
  22. int main(int argc, char **argv)
  23. {
  24. CURL *http_handle;
  25. CURL *http_handle2;
  26. CURLM *multi_handle;
  27. int still_running; /* keep number of running handles */
  28. http_handle = curl_easy_init();
  29. http_handle2 = curl_easy_init();
  30. /* set options */
  31. curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.haxx.se/");
  32. /* set options */
  33. curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/");
  34. /* init a multi stack */
  35. multi_handle = curl_multi_init();
  36. /* add the individual transfers */
  37. curl_multi_add_handle(multi_handle, http_handle);
  38. curl_multi_add_handle(multi_handle, http_handle2);
  39. /* we start some action by calling perform right away */
  40. while(CURLM_CALL_MULTI_PERFORM ==
  41. curl_multi_perform(multi_handle, &still_running));
  42. while(still_running) {
  43. struct timeval timeout;
  44. int rc; /* select() return code */
  45. fd_set fdread;
  46. fd_set fdwrite;
  47. fd_set fdexcep;
  48. int maxfd;
  49. FD_ZERO(&fdread);
  50. FD_ZERO(&fdwrite);
  51. FD_ZERO(&fdexcep);
  52. /* set a suitable timeout to play around with */
  53. timeout.tv_sec = 1;
  54. timeout.tv_usec = 0;
  55. /* get file descriptors from the transfers */
  56. curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  57. /* In a real-world program you OF COURSE check the return code of the
  58. function calls, *and* you make sure that maxfd is bigger than -1 so
  59. that the call to select() below makes sense! */
  60. rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  61. switch(rc) {
  62. case -1:
  63. /* select error */
  64. break;
  65. case 0:
  66. default:
  67. /* timeout or readable/writable sockets */
  68. while(CURLM_CALL_MULTI_PERFORM ==
  69. curl_multi_perform(multi_handle, &still_running));
  70. break;
  71. }
  72. }
  73. curl_multi_cleanup(multi_handle);
  74. curl_easy_cleanup(http_handle);
  75. curl_easy_cleanup(http_handle2);
  76. return 0;
  77. }