multi-single.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 a HTTP file.
  21. */
  22. int main(int argc, char **argv)
  23. {
  24. CURL *http_handle;
  25. CURLM *multi_handle;
  26. int still_running; /* keep number of running handles */
  27. http_handle = curl_easy_init();
  28. /* set the options (I left out a few, you'll get the point anyway) */
  29. curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.haxx.se/");
  30. /* init a multi stack */
  31. multi_handle = curl_multi_init();
  32. /* add the individual transfers */
  33. curl_multi_add_handle(multi_handle, http_handle);
  34. /* we start some action by calling perform right away */
  35. while(CURLM_CALL_MULTI_PERFORM ==
  36. curl_multi_perform(multi_handle, &still_running));
  37. while(still_running) {
  38. struct timeval timeout;
  39. int rc; /* select() return code */
  40. fd_set fdread;
  41. fd_set fdwrite;
  42. fd_set fdexcep;
  43. int maxfd;
  44. FD_ZERO(&fdread);
  45. FD_ZERO(&fdwrite);
  46. FD_ZERO(&fdexcep);
  47. /* set a suitable timeout to play around with */
  48. timeout.tv_sec = 1;
  49. timeout.tv_usec = 0;
  50. /* get file descriptors from the transfers */
  51. curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  52. /* In a real-world program you OF COURSE check the return code of the
  53. function calls, *and* you make sure that maxfd is bigger than -1 so
  54. that the call to select() below makes sense! */
  55. rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  56. switch(rc) {
  57. case -1:
  58. /* select error */
  59. still_running = 0;
  60. printf("select() returns error, this is badness\n");
  61. break;
  62. case 0:
  63. default:
  64. /* timeout or readable/writable sockets */
  65. while(CURLM_CALL_MULTI_PERFORM ==
  66. curl_multi_perform(multi_handle, &still_running));
  67. break;
  68. }
  69. }
  70. curl_multi_cleanup(multi_handle);
  71. curl_easy_cleanup(http_handle);
  72. return 0;
  73. }