curl_easy_recv.3 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. .\" generated by cd2nroff 0.1 from curl_easy_recv.md
  2. .TH curl_easy_recv 3 "2025-01-17" libcurl
  3. .SH NAME
  4. curl_easy_recv \- receives raw data on an "easy" connection
  5. .SH SYNOPSIS
  6. .nf
  7. #include <curl/curl.h>
  8. CURLcode curl_easy_recv(CURL *curl, void *buffer, size_t buflen, size_t *n);
  9. .fi
  10. .SH DESCRIPTION
  11. This function receives raw data from the established connection. You may use
  12. it together with \fIcurl_easy_send(3)\fP to implement custom protocols using
  13. libcurl. This functionality can be particularly useful if you use proxies
  14. and/or SSL encryption: libcurl takes care of proxy negotiation and connection
  15. setup.
  16. \fBbuffer\fP is a pointer to your buffer memory that gets populated by the
  17. received data. \fBbuflen\fP is the maximum amount of data you can get in that
  18. buffer. The variable \fBn\fP points to receives the number of received bytes.
  19. To establish the connection, set \fICURLOPT_CONNECT_ONLY(3)\fP option before
  20. calling \fIcurl_easy_perform(3)\fP or \fIcurl_multi_perform(3)\fP. Note that
  21. \fIcurl_easy_recv(3)\fP does not work on connections that were created without
  22. this option.
  23. The call returns \fBCURLE_AGAIN\fP if there is no data to read \- the socket is
  24. used in non\-blocking mode internally. When \fBCURLE_AGAIN\fP is returned, use
  25. your operating system facilities like \fIselect(2)\fP to wait for data. The
  26. socket may be obtained using \fIcurl_easy_getinfo(3)\fP with
  27. \fICURLINFO_ACTIVESOCKET(3)\fP.
  28. Wait on the socket only if \fIcurl_easy_recv(3)\fP returns \fBCURLE_AGAIN\fP.
  29. The reason for this is libcurl or the SSL library may internally cache some
  30. data, therefore you should call \fIcurl_easy_recv(3)\fP until all data is
  31. read which would include any cached data.
  32. Furthermore if you wait on the socket and it tells you there is data to read,
  33. \fIcurl_easy_recv(3)\fP may return \fBCURLE_AGAIN\fP if the only data that was
  34. read was for internal SSL processing, and no other data is available.
  35. .SH PROTOCOLS
  36. This functionality affects all supported protocols
  37. .SH EXAMPLE
  38. .nf
  39. int main(void)
  40. {
  41. CURL *curl = curl_easy_init();
  42. if(curl) {
  43. CURLcode res;
  44. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
  45. /* Do not do the transfer - only connect to host */
  46. curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
  47. res = curl_easy_perform(curl);
  48. if(res == CURLE_OK) {
  49. char buf[256];
  50. size_t nread;
  51. long sockfd;
  52. /* Extract the socket from the curl handle - we need it for waiting. */
  53. res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd);
  54. /* read data */
  55. res = curl_easy_recv(curl, buf, sizeof(buf), &nread);
  56. }
  57. }
  58. }
  59. .fi
  60. .SH AVAILABILITY
  61. Added in curl 7.18.2
  62. .SH RETURN VALUE
  63. On success, returns \fBCURLE_OK\fP, stores the received data into
  64. \fBbuffer\fP, and the number of bytes it actually read into \fB*n\fP.
  65. On failure, returns the appropriate error code.
  66. The function may return \fBCURLE_AGAIN\fP. In this case, use your operating
  67. system facilities to wait until data can be read, and retry.
  68. Reading exactly 0 bytes indicates a closed connection.
  69. If there is no socket available to use from the previous transfer, this function
  70. returns \fBCURLE_UNSUPPORTED_PROTOCOL\fP.
  71. .SH SEE ALSO
  72. .BR curl_easy_getinfo (3),
  73. .BR curl_easy_perform (3),
  74. .BR curl_easy_send (3),
  75. .BR curl_easy_setopt (3)