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 #include <stdio.h>
00027 #include <string.h>
00028
00029
00030 #include <sys/time.h>
00031 #include <unistd.h>
00032
00033
00034 #include <curl/curl.h>
00035
00036
00037
00038
00039 int main(void)
00040 {
00041 CURL *http_handle;
00042 CURL *http_handle2;
00043 CURLM *multi_handle;
00044
00045 int still_running;
00046
00047 http_handle = curl_easy_init();
00048 http_handle2 = curl_easy_init();
00049
00050
00051 curl_easy_setopt(http_handle, CURLOPT_URL, "http://www.example.com/");
00052
00053
00054 curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/");
00055
00056
00057 multi_handle = curl_multi_init();
00058
00059
00060 curl_multi_add_handle(multi_handle, http_handle);
00061 curl_multi_add_handle(multi_handle, http_handle2);
00062
00063
00064 curl_multi_perform(multi_handle, &still_running);
00065
00066 do {
00067 struct timeval timeout;
00068 int rc;
00069 CURLMcode mc;
00070
00071 fd_set fdread;
00072 fd_set fdwrite;
00073 fd_set fdexcep;
00074 int maxfd = -1;
00075
00076 long curl_timeo = -1;
00077
00078 FD_ZERO(&fdread);
00079 FD_ZERO(&fdwrite);
00080 FD_ZERO(&fdexcep);
00081
00082
00083 timeout.tv_sec = 1;
00084 timeout.tv_usec = 0;
00085
00086 curl_multi_timeout(multi_handle, &curl_timeo);
00087 if(curl_timeo >= 0) {
00088 timeout.tv_sec = curl_timeo / 1000;
00089 if(timeout.tv_sec > 1)
00090 timeout.tv_sec = 1;
00091 else
00092 timeout.tv_usec = (curl_timeo % 1000) * 1000;
00093 }
00094
00095
00096 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
00097
00098 if(mc != CURLM_OK) {
00099 fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
00100 break;
00101 }
00102
00103
00104
00105
00106
00107
00108
00109 if(maxfd == -1) {
00110 #ifdef _WIN32
00111 Sleep(100);
00112 rc = 0;
00113 #else
00114
00115 struct timeval wait = { 0, 100 * 1000 };
00116 rc = select(0, NULL, NULL, NULL, &wait);
00117 #endif
00118 }
00119 else {
00120
00121
00122 rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &timeout);
00123 }
00124
00125 switch(rc) {
00126 case -1:
00127
00128 break;
00129 case 0:
00130 default:
00131
00132 curl_multi_perform(multi_handle, &still_running);
00133 break;
00134 }
00135 } while(still_running);
00136
00137 curl_multi_cleanup(multi_handle);
00138
00139 curl_easy_cleanup(http_handle);
00140 curl_easy_cleanup(http_handle2);
00141
00142 return 0;
00143 }