sendrecv.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 /* <DESC>
00023  * An example of curl_easy_send() and curl_easy_recv() usage.
00024  * </DESC>
00025  */
00026 
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include <curl/curl.h>
00030 
00031 /* Auxiliary function that waits on the socket. */
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); /* always check for error */
00046 
00047   if(for_recv) {
00048     FD_SET(sockfd, &infd);
00049   }
00050   else {
00051     FD_SET(sockfd, &outfd);
00052   }
00053 
00054   /* select() returns the number of signalled sockets or -1 */
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   /* Minimalistic http request */
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   /* A general note of caution here: if you're using curl_easy_recv() or
00070      curl_easy_send() to implement HTTP or _any_ other protocol libcurl
00071      supports "natively", you're doing it wrong and you should stop.
00072 
00073      This example uses HTTP only to show how to use this API, it does not
00074      suggest that writing an application doing this is sensible.
00075   */
00076 
00077   curl = curl_easy_init();
00078   if(curl) {
00079     curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
00080     /* Do not do the transfer - only connect to host */
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     /* Extract the socket from the curl handle - we'll need it for waiting. */
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       /* Warning: This example program may loop indefinitely.
00101        * A production-quality program must define a timeout and exit this loop
00102        * as soon as the timeout has expired. */
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       /* Warning: This example program may loop indefinitely (see above). */
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         /* end of the response */
00149         break;
00150       }
00151 
00152       printf("Received %" CURL_FORMAT_CURL_OFF_T " bytes.\n",
00153         (curl_off_t)nread);
00154     }
00155 
00156     /* always cleanup */
00157     curl_easy_cleanup(curl);
00158   }
00159   return 0;
00160 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06