xref: /curl/docs/libcurl/curl_easy_recv.md (revision e3fe0200)
1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Title: curl_easy_recv
5Section: 3
6Source: libcurl
7See-also:
8  - curl_easy_getinfo (3)
9  - curl_easy_perform (3)
10  - curl_easy_send (3)
11  - curl_easy_setopt (3)
12Protocol:
13  - All
14---
15
16# NAME
17
18curl_easy_recv - receives raw data on an "easy" connection
19
20# SYNOPSIS
21
22~~~c
23#include <curl/curl.h>
24
25CURLcode curl_easy_recv(CURL *curl, void *buffer, size_t buflen, size_t *n);
26~~~
27
28# DESCRIPTION
29
30This function receives raw data from the established connection. You may use
31it together with curl_easy_send(3) to implement custom protocols using
32libcurl. This functionality can be particularly useful if you use proxies
33and/or SSL encryption: libcurl takes care of proxy negotiation and connection
34setup.
35
36**buffer** is a pointer to your buffer memory that gets populated by the
37received data. **buflen** is the maximum amount of data you can get in that
38buffer. The variable **n** points to receives the number of received bytes.
39
40To establish the connection, set CURLOPT_CONNECT_ONLY(3) option before
41calling curl_easy_perform(3) or curl_multi_perform(3). Note that
42curl_easy_recv(3) does not work on connections that were created without
43this option.
44
45The call returns **CURLE_AGAIN** if there is no data to read - the socket is
46used in non-blocking mode internally. When **CURLE_AGAIN** is returned, use
47your operating system facilities like *select(2)* to wait for data. The
48socket may be obtained using curl_easy_getinfo(3) with
49CURLINFO_ACTIVESOCKET(3).
50
51Wait on the socket only if curl_easy_recv(3) returns **CURLE_AGAIN**.
52The reason for this is libcurl or the SSL library may internally cache some
53data, therefore you should call curl_easy_recv(3) until all data is
54read which would include any cached data.
55
56Furthermore if you wait on the socket and it tells you there is data to read,
57curl_easy_recv(3) may return **CURLE_AGAIN** if the only data that was
58read was for internal SSL processing, and no other data is available.
59
60# EXAMPLE
61
62~~~c
63int main(void)
64{
65  CURL *curl = curl_easy_init();
66  if(curl) {
67    CURLcode res;
68    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
69    /* Do not do the transfer - only connect to host */
70    curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
71    res = curl_easy_perform(curl);
72
73    if(res == CURLE_OK) {
74      char buf[256];
75      size_t nread;
76      long sockfd;
77
78      /* Extract the socket from the curl handle - we need it for waiting. */
79      res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd);
80
81      /* read data */
82      res = curl_easy_recv(curl, buf, sizeof(buf), &nread);
83    }
84  }
85}
86~~~
87
88# AVAILABILITY
89
90Added in 7.18.2.
91
92# RETURN VALUE
93
94On success, returns **CURLE_OK**, stores the received data into
95**buffer**, and the number of bytes it actually read into ***n**.
96
97On failure, returns the appropriate error code.
98
99The function may return **CURLE_AGAIN**. In this case, use your operating
100system facilities to wait until data can be read, and retry.
101
102Reading exactly 0 bytes indicates a closed connection.
103
104If there is no socket available to use from the previous transfer, this function
105returns **CURLE_UNSUPPORTED_PROTOCOL**.
106