X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=docs%2Fexamples%2Fsendrecv.c;h=41e283cdce92c127be6e5b0d1d2ca8b1a884ae73;hb=88d478478059f53e6de0d1267c3a1e6b02c4273f;hp=662323487da2d639ef71de0a777e9f7ddcfc8aef;hpb=e9bdad71c8277e20607fa1eaf0027d53a0dc1f37;p=platform%2Fupstream%2Fcurl.git diff --git a/docs/examples/sendrecv.c b/docs/examples/sendrecv.c index 6623234..41e283c 100644 --- a/docs/examples/sendrecv.c +++ b/docs/examples/sendrecv.c @@ -62,9 +62,10 @@ int main(void) CURLcode res; /* Minimalistic http request */ const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n"; - size_t request_len = strlen(request); - curl_socket_t sockfd; - size_t nsent_total = 0; + curl_socket_t sockfd; /* socket */ + long sockextr; + size_t iolen; + curl_off_t nread; /* A general note of caution here: if you're using curl_easy_recv() or curl_easy_send() to implement HTTP or _any_ other protocol libcurl @@ -81,76 +82,54 @@ int main(void) curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L); res = curl_easy_perform(curl); - if(res != CURLE_OK) { - printf("Error: %s\n", curl_easy_strerror(res)); + if(CURLE_OK != res) { + printf("Error: %s\n", strerror(res)); return 1; } - /* Extract the socket from the curl handle - we'll need it for waiting. */ - res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd); + /* Extract the socket from the curl handle - we'll need it for waiting. + * Note that this API takes a pointer to a 'long' while we use + * curl_socket_t for sockets otherwise. + */ + res = curl_easy_getinfo(curl, CURLINFO_LASTSOCKET, &sockextr); - if(res != CURLE_OK) { + if(CURLE_OK != res) { printf("Error: %s\n", curl_easy_strerror(res)); return 1; } - printf("Sending request.\n"); - - do { - /* Warning: This example program may loop indefinitely. - * A production-quality program must define a timeout and exit this loop - * as soon as the timeout has expired. */ - size_t nsent; - do { - nsent = 0; - res = curl_easy_send(curl, request + nsent_total, - request_len - nsent_total, &nsent); - nsent_total += nsent; - - if(res == CURLE_AGAIN && !wait_on_socket(sockfd, 0, 60000L)) { - printf("Error: timeout.\n"); - return 1; - } - } while(res == CURLE_AGAIN); + sockfd = (curl_socket_t)sockextr; - if(res != CURLE_OK) { - printf("Error: %s\n", curl_easy_strerror(res)); - return 1; - } - - printf("Sent %" CURL_FORMAT_CURL_OFF_T " bytes.\n", - (curl_off_t)nsent); + /* wait for the socket to become ready for sending */ + if(!wait_on_socket(sockfd, 0, 60000L)) { + printf("Error: timeout.\n"); + return 1; + } - } while(nsent_total < request_len); + puts("Sending request."); + /* Send the request. Real applications should check the iolen + * to see if all the request has been sent */ + res = curl_easy_send(curl, request, strlen(request), &iolen); - printf("Reading response.\n"); + if(CURLE_OK != res) { + printf("Error: %s\n", curl_easy_strerror(res)); + return 1; + } + puts("Reading response."); + /* read the response */ for(;;) { - /* Warning: This example program may loop indefinitely (see above). */ char buf[1024]; - size_t nread; - do { - nread = 0; - res = curl_easy_recv(curl, buf, sizeof(buf), &nread); - - if(res == CURLE_AGAIN && !wait_on_socket(sockfd, 1, 60000L)) { - printf("Error: timeout.\n"); - return 1; - } - } while(res == CURLE_AGAIN); - - if(res != CURLE_OK) { - printf("Error: %s\n", curl_easy_strerror(res)); - break; - } - if(nread == 0) { - /* end of the response */ + wait_on_socket(sockfd, 1, 60000L); + res = curl_easy_recv(curl, buf, 1024, &iolen); + + if(CURLE_OK != res) break; - } - printf("Received %" CURL_FORMAT_CURL_OFF_T " bytes.\n", - (curl_off_t)nread); + nread = (curl_off_t)iolen; + + printf("Received %" CURL_FORMAT_CURL_OFF_T " bytes.\n", nread); } /* always cleanup */