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