multi-app.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 an example application source code 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. * Download a HTTP file and upload an FTP file simultaneously.
  32. */
  33. #define HANDLECOUNT 2 /* Number of simultaneous transfers */
  34. #define HTTP_HANDLE 0 /* Index for the HTTP transfer */
  35. #define FTP_HANDLE 1 /* Index for the FTP transfer */
  36. int main(void)
  37. {
  38. CURL *handles[HANDLECOUNT];
  39. CURLM *multi_handle;
  40. int still_running; /* keep number of running handles */
  41. int i;
  42. CURLMsg *msg; /* for picking up messages with the transfer status */
  43. int msgs_left; /* how many messages are left */
  44. /* Allocate one CURL handle per transfer */
  45. for (i=0; i<HANDLECOUNT; i++)
  46. handles[i] = curl_easy_init();
  47. /* set the options (I left out a few, you'll get the point anyway) */
  48. curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "http://example.com");
  49. curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://example.com");
  50. curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
  51. /* init a multi stack */
  52. multi_handle = curl_multi_init();
  53. /* add the individual transfers */
  54. for (i=0; i<HANDLECOUNT; i++)
  55. curl_multi_add_handle(multi_handle, handles[i]);
  56. /* we start some action by calling perform right away */
  57. curl_multi_perform(multi_handle, &still_running);
  58. do {
  59. struct timeval timeout;
  60. int rc; /* select() return code */
  61. fd_set fdread;
  62. fd_set fdwrite;
  63. fd_set fdexcep;
  64. int maxfd = -1;
  65. long curl_timeo = -1;
  66. FD_ZERO(&fdread);
  67. FD_ZERO(&fdwrite);
  68. FD_ZERO(&fdexcep);
  69. /* set a suitable timeout to play around with */
  70. timeout.tv_sec = 1;
  71. timeout.tv_usec = 0;
  72. curl_multi_timeout(multi_handle, &curl_timeo);
  73. if(curl_timeo >= 0) {
  74. timeout.tv_sec = curl_timeo / 1000;
  75. if(timeout.tv_sec > 1)
  76. timeout.tv_sec = 1;
  77. else
  78. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  79. }
  80. /* get file descriptors from the transfers */
  81. curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
  82. /* In a real-world program you OF COURSE check the return code of the
  83. function calls. On success, the value of maxfd is guaranteed to be
  84. greater or equal than -1. We call select(maxfd + 1, ...), specially in
  85. case of (maxfd == -1), we call select(0, ...), which is basically equal
  86. to sleep. */
  87. rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
  88. switch(rc) {
  89. case -1:
  90. /* select error */
  91. break;
  92. case 0: /* timeout */
  93. default: /* action */
  94. curl_multi_perform(multi_handle, &still_running);
  95. break;
  96. }
  97. } while(still_running);
  98. /* See how the transfers went */
  99. while ((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
  100. if (msg->msg == CURLMSG_DONE) {
  101. int idx, found = 0;
  102. /* Find out which handle this message is about */
  103. for (idx=0; idx<HANDLECOUNT; idx++) {
  104. found = (msg->easy_handle == handles[idx]);
  105. if(found)
  106. break;
  107. }
  108. switch (idx) {
  109. case HTTP_HANDLE:
  110. printf("HTTP transfer completed with status %d\n", msg->data.result);
  111. break;
  112. case FTP_HANDLE:
  113. printf("FTP transfer completed with status %d\n", msg->data.result);
  114. break;
  115. }
  116. }
  117. }
  118. curl_multi_cleanup(multi_handle);
  119. /* Free the CURL handles */
  120. for (i=0; i<HANDLECOUNT; i++)
  121. curl_easy_cleanup(handles[i]);
  122. return 0;
  123. }