Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdio.h>
00029 #include <string.h>
00030
00031
00032 #include <sys/time.h>
00033 #include <unistd.h>
00034
00035
00036 #include <curl/curl.h>
00037
00038
00039
00040
00041
00042 #define HANDLECOUNT 2
00043 #define HTTP_HANDLE 0
00044 #define FTP_HANDLE 1
00045
00046 int main(void)
00047 {
00048 CURL *handles[HANDLECOUNT];
00049 CURLM *multi_handle;
00050
00051 int still_running;
00052 int i;
00053
00054 CURLMsg *msg;
00055 int msgs_left;
00056
00057
00058 for(i=0; i<HANDLECOUNT; i++)
00059 handles[i] = curl_easy_init();
00060
00061
00062 curl_easy_setopt(handles[HTTP_HANDLE], CURLOPT_URL, "http://example.com");
00063
00064 curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_URL, "ftp://example.com");
00065 curl_easy_setopt(handles[FTP_HANDLE], CURLOPT_UPLOAD, 1L);
00066
00067
00068 multi_handle = curl_multi_init();
00069
00070
00071 for(i=0; i<HANDLECOUNT; i++)
00072 curl_multi_add_handle(multi_handle, handles[i]);
00073
00074
00075 curl_multi_perform(multi_handle, &still_running);
00076
00077 do {
00078 struct timeval timeout;
00079 int rc;
00080 CURLMcode mc;
00081
00082 fd_set fdread;
00083 fd_set fdwrite;
00084 fd_set fdexcep;
00085 int maxfd = -1;
00086
00087 long curl_timeo = -1;
00088
00089 FD_ZERO(&fdread);
00090 FD_ZERO(&fdwrite);
00091 FD_ZERO(&fdexcep);
00092
00093
00094 timeout.tv_sec = 1;
00095 timeout.tv_usec = 0;
00096
00097 curl_multi_timeout(multi_handle, &curl_timeo);
00098 if(curl_timeo >= 0) {
00099 timeout.tv_sec = curl_timeo / 1000;
00100 if(timeout.tv_sec > 1)
00101 timeout.tv_sec = 1;
00102 else
00103 timeout.tv_usec = (curl_timeo % 1000) * 1000;
00104 }
00105
00106
00107 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
00108
00109 if(mc != CURLM_OK) {
00110 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
00111 break;
00112 }
00113
00114
00115
00116
00117
00118
00119
00120 if(maxfd == -1) {
00121 #ifdef _WIN32
00122 Sleep(100);
00123 rc = 0;
00124 #else
00125
00126 struct timeval wait = { 0, 100 * 1000 };
00127 rc = select(0, NULL, NULL, NULL, &wait);
00128 #endif
00129 }
00130 else {
00131
00132
00133 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
00134 }
00135
00136 switch(rc) {
00137 case -1:
00138
00139 break;
00140 case 0:
00141 default:
00142 curl_multi_perform(multi_handle, &still_running);
00143 break;
00144 }
00145 } while(still_running);
00146
00147
00148 while((msg = curl_multi_info_read(multi_handle, &msgs_left))) {
00149 if(msg->msg == CURLMSG_DONE) {
00150 int idx, found = 0;
00151
00152
00153 for(idx=0; idx<HANDLECOUNT; idx++) {
00154 found = (msg->easy_handle == handles[idx]);
00155 if(found)
00156 break;
00157 }
00158
00159 switch(idx) {
00160 case HTTP_HANDLE:
00161 printf("HTTP transfer completed with status %d\n", msg->data.result);
00162 break;
00163 case FTP_HANDLE:
00164 printf("FTP transfer completed with status %d\n", msg->data.result);
00165 break;
00166 }
00167 }
00168 }
00169
00170 curl_multi_cleanup(multi_handle);
00171
00172
00173 for(i=0; i<HANDLECOUNT; i++)
00174 curl_easy_cleanup(handles[i]);
00175
00176 return 0;
00177 }