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