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 #include <stdio.h>
00028 #include <string.h>
00029 #include <curl/curl.h>
00030
00031
00032 static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
00033 {
00034 struct timeval tv;
00035 fd_set infd, outfd, errfd;
00036 int res;
00037
00038 tv.tv_sec = timeout_ms / 1000;
00039 tv.tv_usec= (timeout_ms % 1000) * 1000;
00040
00041 FD_ZERO(&infd);
00042 FD_ZERO(&outfd);
00043 FD_ZERO(&errfd);
00044
00045 FD_SET(sockfd, &errfd);
00046
00047 if(for_recv) {
00048 FD_SET(sockfd, &infd);
00049 }
00050 else {
00051 FD_SET(sockfd, &outfd);
00052 }
00053
00054
00055 res = select(sockfd + 1, &infd, &outfd, &errfd, &tv);
00056 return res;
00057 }
00058
00059 int main(void)
00060 {
00061 CURL *curl;
00062 CURLcode res;
00063
00064 const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
00065 size_t request_len = strlen(request);
00066 curl_socket_t sockfd;
00067 size_t nsent_total = 0;
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 curl = curl_easy_init();
00078 if(curl) {
00079 curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
00080
00081 curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
00082 res = curl_easy_perform(curl);
00083
00084 if(res != CURLE_OK) {
00085 printf("Error: %s\n", curl_easy_strerror(res));
00086 return 1;
00087 }
00088
00089
00090 res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd);
00091
00092 if(res != CURLE_OK) {
00093 printf("Error: %s\n", curl_easy_strerror(res));
00094 return 1;
00095 }
00096
00097 printf("Sending request.\n");
00098
00099 do {
00100
00101
00102
00103 size_t nsent;
00104 do {
00105 nsent = 0;
00106 res = curl_easy_send(curl, request + nsent_total,
00107 request_len - nsent_total, &nsent);
00108 nsent_total += nsent;
00109
00110 if(res == CURLE_AGAIN && !wait_on_socket(sockfd, 0, 60000L)) {
00111 printf("Error: timeout.\n");
00112 return 1;
00113 }
00114 } while(res == CURLE_AGAIN);
00115
00116 if(res != CURLE_OK) {
00117 printf("Error: %s\n", curl_easy_strerror(res));
00118 return 1;
00119 }
00120
00121 printf("Sent %" CURL_FORMAT_CURL_OFF_T " bytes.\n",
00122 (curl_off_t)nsent);
00123
00124 } while(nsent_total < request_len);
00125
00126 printf("Reading response.\n");
00127
00128 for(;;) {
00129
00130 char buf[1024];
00131 size_t nread;
00132 do {
00133 nread = 0;
00134 res = curl_easy_recv(curl, buf, sizeof(buf), &nread);
00135
00136 if(res == CURLE_AGAIN && !wait_on_socket(sockfd, 1, 60000L)) {
00137 printf("Error: timeout.\n");
00138 return 1;
00139 }
00140 } while(res == CURLE_AGAIN);
00141
00142 if(res != CURLE_OK) {
00143 printf("Error: %s\n", curl_easy_strerror(res));
00144 break;
00145 }
00146
00147 if(nread == 0) {
00148
00149 break;
00150 }
00151
00152 printf("Received %" CURL_FORMAT_CURL_OFF_T " bytes.\n",
00153 (curl_off_t)nread);
00154 }
00155
00156
00157 curl_easy_cleanup(curl);
00158 }
00159 return 0;
00160 }