sendrecv.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 /* <DESC>
23  * An example of curl_easy_send() and curl_easy_recv() usage.
24  * </DESC>
25  */
26 
27 #include <stdio.h>
28 #include <string.h>
29 #include <curl/curl.h>
30 
31 /* Auxiliary function that waits on the socket. */
32 static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
33 {
34  struct timeval tv;
35  fd_set infd, outfd, errfd;
36  int res;
37 
38  tv.tv_sec = timeout_ms / 1000;
39  tv.tv_usec = (timeout_ms % 1000) * 1000;
40 
41  FD_ZERO(&infd);
42  FD_ZERO(&outfd);
43  FD_ZERO(&errfd);
44 
45  FD_SET(sockfd, &errfd); /* always check for error */
46 
47  if(for_recv) {
48  FD_SET(sockfd, &infd);
49  }
50  else {
51  FD_SET(sockfd, &outfd);
52  }
53 
54  /* select() returns the number of signalled sockets or -1 */
55  res = select((int)sockfd + 1, &infd, &outfd, &errfd, &tv);
56  return res;
57 }
58 
59 int main(void)
60 {
61  CURL *curl;
62  CURLcode res;
63  /* Minimalistic http request */
64  const char *request = "GET / HTTP/1.0\r\nHost: example.com\r\n\r\n";
65  size_t request_len = strlen(request);
66  curl_socket_t sockfd;
67  size_t nsent_total = 0;
68 
69  /* A general note of caution here: if you're using curl_easy_recv() or
70  curl_easy_send() to implement HTTP or _any_ other protocol libcurl
71  supports "natively", you're doing it wrong and you should stop.
72 
73  This example uses HTTP only to show how to use this API, it does not
74  suggest that writing an application doing this is sensible.
75  */
76 
77  curl = curl_easy_init();
78  if(curl) {
79  curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
80  /* Do not do the transfer - only connect to host */
81  curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
82  res = curl_easy_perform(curl);
83 
84  if(res != CURLE_OK) {
85  printf("Error: %s\n", curl_easy_strerror(res));
86  return 1;
87  }
88 
89  /* Extract the socket from the curl handle - we'll need it for waiting. */
90  res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd);
91 
92  if(res != CURLE_OK) {
93  printf("Error: %s\n", curl_easy_strerror(res));
94  return 1;
95  }
96 
97  printf("Sending request.\n");
98 
99  do {
100  /* Warning: This example program may loop indefinitely.
101  * A production-quality program must define a timeout and exit this loop
102  * as soon as the timeout has expired. */
103  size_t nsent;
104  do {
105  nsent = 0;
106  res = curl_easy_send(curl, request + nsent_total,
107  request_len - nsent_total, &nsent);
108  nsent_total += nsent;
109 
110  if(res == CURLE_AGAIN && !wait_on_socket(sockfd, 0, 60000L)) {
111  printf("Error: timeout.\n");
112  return 1;
113  }
114  } while(res == CURLE_AGAIN);
115 
116  if(res != CURLE_OK) {
117  printf("Error: %s\n", curl_easy_strerror(res));
118  return 1;
119  }
120 
121  printf("Sent %" CURL_FORMAT_CURL_OFF_T " bytes.\n",
122  (curl_off_t)nsent);
123 
124  } while(nsent_total < request_len);
125 
126  printf("Reading response.\n");
127 
128  for(;;) {
129  /* Warning: This example program may loop indefinitely (see above). */
130  char buf[1024];
131  size_t nread;
132  do {
133  nread = 0;
134  res = curl_easy_recv(curl, buf, sizeof(buf), &nread);
135 
136  if(res == CURLE_AGAIN && !wait_on_socket(sockfd, 1, 60000L)) {
137  printf("Error: timeout.\n");
138  return 1;
139  }
140  } while(res == CURLE_AGAIN);
141 
142  if(res != CURLE_OK) {
143  printf("Error: %s\n", curl_easy_strerror(res));
144  break;
145  }
146 
147  if(nread == 0) {
148  /* end of the response */
149  break;
150  }
151 
152  printf("Received %" CURL_FORMAT_CURL_OFF_T " bytes.\n",
153  (curl_off_t)nread);
154  }
155 
156  /* always cleanup */
157  curl_easy_cleanup(curl);
158  }
159  return 0;
160 }
static int wait_on_socket(curl_socket_t sockfd, int for_recv, long timeout_ms)
Definition: sendrecv.c:32
CURL_EXTERN CURLcode curl_easy_send(CURL *curl, const void *buffer, size_t buflen, size_t *n)
CURL_EXTERN CURLcode curl_easy_recv(CURL *curl, void *buffer, size_t buflen, size_t *n)
CURLcode
Definition: curl.h:454
static int res
#define curl_easy_setopt(handle, option, value)
Definition: typecheck-gcc.h:41
#define curl_easy_getinfo(handle, info, arg)
#define printf
Definition: curl_printf.h:40
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
CURL_TYPEOF_CURL_OFF_T curl_off_t
Definition: system.h:420
Definition: curl.h:455
char buf[3]
Definition: unit1398.c:32
void CURL
Definition: curl.h:102
int main(void)
Definition: sendrecv.c:59
int curl_socket_t
Definition: curl.h:130
static CURL * curl
Definition: sessioninfo.c:35
CURL_EXTERN const char * curl_easy_strerror(CURLcode)
Definition: strerror.c:57
CURL_EXTERN CURLcode curl_easy_perform(CURL *curl)
#define CURL_FORMAT_CURL_OFF_T
Definition: system.h:373


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:16