curl_easy_recv.3 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. .\" **************************************************************************
  2. .\" * _ _ ____ _
  3. .\" * Project ___| | | | _ \| |
  4. .\" * / __| | | | |_) | |
  5. .\" * | (__| |_| | _ <| |___
  6. .\" * \___|\___/|_| \_\_____|
  7. .\" *
  8. .\" * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. .\" *
  10. .\" * This software is licensed as described in the file COPYING, which
  11. .\" * you should have received as part of this distribution. The terms
  12. .\" * are also available at https://curl.se/docs/copyright.html.
  13. .\" *
  14. .\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. .\" * copies of the Software, and permit persons to whom the Software is
  16. .\" * furnished to do so, under the terms of the COPYING file.
  17. .\" *
  18. .\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. .\" * KIND, either express or implied.
  20. .\" *
  21. .\" * SPDX-License-Identifier: curl
  22. .\" *
  23. .\" **************************************************************************
  24. .\"
  25. .TH curl_easy_recv 3 "29 April 2008" "libcurl" "libcurl"
  26. .SH NAME
  27. curl_easy_recv - receives raw data on an "easy" connection
  28. .SH SYNOPSIS
  29. .nf
  30. #include <curl/easy.h>
  31. CURLcode curl_easy_recv(CURL *curl, void *buffer, size_t buflen, size_t *n);
  32. .fi
  33. .SH DESCRIPTION
  34. This function receives raw data from the established connection. You may use
  35. it together with \fIcurl_easy_send(3)\fP to implement custom protocols using
  36. libcurl. This functionality can be particularly useful if you use proxies
  37. and/or SSL encryption: libcurl will take care of proxy negotiation and
  38. connection setup.
  39. \fBbuffer\fP is a pointer to your buffer that will get the received
  40. data. \fBbuflen\fP is the maximum amount of data you can get in that
  41. buffer. The variable \fBn\fP points to will receive the number of received
  42. bytes.
  43. To establish the connection, set \fICURLOPT_CONNECT_ONLY(3)\fP option before
  44. calling \fIcurl_easy_perform(3)\fP or \fIcurl_multi_perform(3)\fP. Note that
  45. \fIcurl_easy_recv(3)\fP does not work on connections that were created without
  46. this option.
  47. The call will return \fBCURLE_AGAIN\fP if there is no data to read - the
  48. socket is used in non-blocking mode internally. When \fBCURLE_AGAIN\fP is
  49. returned, use your operating system facilities like \fIselect(2)\fP to wait
  50. for data. The socket may be obtained using \fIcurl_easy_getinfo(3)\fP with
  51. \fICURLINFO_ACTIVESOCKET(3)\fP.
  52. Wait on the socket only if \fIcurl_easy_recv(3)\fP returns \fBCURLE_AGAIN\fP.
  53. The reason for this is libcurl or the SSL library may internally cache some
  54. data, therefore you should call \fIcurl_easy_recv(3)\fP until all data is
  55. read which would include any cached data.
  56. Furthermore if you wait on the socket and it tells you there is data to read,
  57. \fIcurl_easy_recv(3)\fP may return \fBCURLE_AGAIN\fP if the only data that was
  58. read was for internal SSL processing, and no other data is available.
  59. .SH EXAMPLE
  60. .nf
  61. curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
  62. /* Do not do the transfer - only connect to host */
  63. curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
  64. res = curl_easy_perform(curl);
  65. if(res == CURLE_OK) {
  66. /* Extract the socket from the curl handle -
  67. we will need it for waiting. */
  68. res = curl_easy_getinfo(curl, CURLINFO_ACTIVESOCKET, &sockfd);
  69. /* read data */
  70. res = curl_easy_recv(curl, buf, sizeof(buf), &nread);
  71. }
  72. .fi
  73. .SH AVAILABILITY
  74. Added in 7.18.2.
  75. .SH RETURN VALUE
  76. On success, returns \fBCURLE_OK\fP, stores the received data into
  77. \fBbuffer\fP, and the number of bytes it actually read into \fB*n\fP.
  78. On failure, returns the appropriate error code.
  79. The function may return \fBCURLE_AGAIN\fP. In this case, use your operating
  80. system facilities to wait until data can be read, and retry.
  81. Reading exactly 0 bytes indicates a closed connection.
  82. If there's no socket available to use from the previous transfer, this function
  83. returns \fBCURLE_UNSUPPORTED_PROTOCOL\fP.
  84. .SH "SEE ALSO"
  85. .BR curl_easy_setopt "(3), " curl_easy_perform "(3), "
  86. .BR curl_easy_getinfo "(3), "
  87. .BR curl_easy_send "(3) "