multi-app.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*****************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * $Id: multi-app.c,v 1.4 2003/08/28 11:21:14 bagder Exp $
  9. *
  10. * This is an example application source code 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. * Download a HTTP file and upload an FTP file simultaneously.
  21. */
  22. #define HANDLECOUNT 2 /* Number of simultaneous transfers */
  23. #define HTTP_HANDLE 0 /* Index for the HTTP transfer */
  24. #define FTP_HANDLE 1 /* Index for the FTP transfer */
  25. int main(int argc, char **argv)
  26. {
  27. CURL *handles[HANDLECOUNT];
  28. CURLM *multi_handle;
  29. int still_running; /* keep number of running handles */
  30. int i;
  31. CURLMsg *msg; /* for picking up messages with the transfer status */
  32. int msgs_left; /* how many messages are left */
  33. /* Allocate one CURL handle per transfer */
  34. for (i=0; i<HANDLECOUNT; i++)
  35. handles[i] = curl_easy_init();
  36. /* set the options (I left out a few, you'll get the point anyway) */
  37. curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "http://website.com");
  38. curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://ftpsite.com");
  39. curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, TRUE);
  40. /* init a multi stack */
  41. multi_handle = curl_multi_init();
  42. /* add the individual transfers */
  43. for (i=0; i<HANDLECOUNT; i++)
  44. curl_multi_add_handle(multi_handle, handles[i]);
  45. /* we start some action by calling perform right away */
  46. while(CURLM_CALL_MULTI_PERFORM ==
  47. curl_multi_perform(multi_handle, &still_running));
  48. while(still_running) {
  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;
  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. /* get file descriptors from the transfers */
  62. curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  63. rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  64. switch(rc) {
  65. case -1:
  66. /* select error */
  67. break;
  68. case 0:
  69. /* timeout, do something else */
  70. break;
  71. default:
  72. /* one or more of curl's file descriptors say there's data to read
  73. or write */
  74. while(CURLM_CALL_MULTI_PERFORM ==
  75. curl_multi_perform(multi_handle, &still_running));
  76. break;
  77. }
  78. }
  79. curl_multi_cleanup(multi_handle);
  80. /* See how the transfers went */
  81. while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
  82. if (msg->msg == CURLMSG_DONE) {
  83. int idx, found = 0;
  84. /* Find out which handle this message is about */
  85. for (idx=0; (!found && (idx<HANDLECOUNT)); idx++) found = (msg->easy_handle == handles[idx]);
  86. switch (idx) {
  87. case HTTP_HANDLE:
  88. printf("HTTP transfer completed with status %d\n", msg->data.result);
  89. break;
  90. case FTP_HANDLE:
  91. printf("FTP transfer completed with status %d\n", msg->data.result);
  92. break;
  93. }
  94. }
  95. }
  96. /* Free the CURL handles */
  97. for (i=0; i<HANDLECOUNT; i++)
  98. curl_easy_cleanup(handles[i]);
  99. return 0;
  100. }