curl_easy_recv.md (3231B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Title: curl_easy_recv 5 Section: 3 6 Source: libcurl 7 See-also: 8 - curl_easy_getinfo (3) 9 - curl_easy_perform (3) 10 - curl_easy_send (3) 11 - curl_easy_setopt (3) 12 Protocol: 13 - All 14 Added-in: 7.18.2 15 --- 16 17 # NAME 18 19 curl_easy_recv - receives raw data on an "easy" connection 20 21 # SYNOPSIS 22 23 ~~~c 24 #include <curl/curl.h> 25 26 CURLcode curl_easy_recv(CURL *curl, void *buffer, size_t buflen, size_t *n); 27 ~~~ 28 29 # DESCRIPTION 30 31 This function receives raw data from the established connection. You may use 32 it together with curl_easy_send(3) to implement custom protocols using 33 libcurl. This functionality can be particularly useful if you use proxies 34 and/or SSL encryption: libcurl takes care of proxy negotiation and connection 35 setup. 36 37 **buffer** is a pointer to your buffer memory that gets populated by the 38 received data. **buflen** is the maximum amount of data you can get in that 39 buffer. The variable **n** points to receives the number of received bytes. 40 41 To establish the connection, set CURLOPT_CONNECT_ONLY(3) option before 42 calling curl_easy_perform(3) or curl_multi_perform(3). Note that 43 curl_easy_recv(3) does not work on connections that were created without 44 this option. 45 46 The call returns **CURLE_AGAIN** if there is no data to read - the socket is 47 used in non-blocking mode internally. When **CURLE_AGAIN** is returned, use 48 your operating system facilities like *select(2)* to wait for data. The 49 socket may be obtained using curl_easy_getinfo(3) with 50 CURLINFO_ACTIVESOCKET(3). 51 52 Wait on the socket only if curl_easy_recv(3) returns **CURLE_AGAIN**. 53 The reason for this is libcurl or the SSL library may internally cache some 54 data, therefore you should call curl_easy_recv(3) until all data is 55 read which would include any cached data. 56 57 Furthermore if you wait on the socket and it tells you there is data to read, 58 curl_easy_recv(3) may return **CURLE_AGAIN** if the only data that was 59 read was for internal SSL processing, and no other data is available. 60 61 # %PROTOCOLS% 62 63 # EXAMPLE 64 65 ~~~c 66 int 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 curl_socket_t 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 95 On success, returns **CURLE_OK**, stores the received data into 96 **buffer**, and the number of bytes it actually read into ***n**. 97 98 On failure, returns the appropriate error code. 99 100 The function may return **CURLE_AGAIN**. In this case, use your operating 101 system facilities to wait until data can be read, and retry. 102 103 Reading exactly 0 bytes indicates a closed connection. 104 105 If there is no socket available to use from the previous transfer, this 106 function returns **CURLE_UNSUPPORTED_PROTOCOL**.