The recv
function retrieves data from a connection-oriented socket or from a set of sockets specified in a fd_set
structure.
int recv(
[in] SOCKET s,
[out] char *buf,
[in] int len,
[in] int flags
);
s
[in]
A descriptor identifying a connected socket.
buf
[out]
A buffer that will receive the data sent over the socket.
len
[in]
The length, in bytes, of the buffer specified by the buf
parameter.
flags
[in]
Flags that control the behavior of the function call. See Remarks for a list of possible values.
If no error occurs, recv
returns the number of bytes received. If the calling process has issued a graceful linger for the socket and all in-transit, unsent data has been received by the calling process, and then the peer has shut down the connection gracefully, and recv
is called on a non-blocking socket, it returns zero. If a socket is closed, and the data is available to be received, recv
will return and indicate success by returning the number of bytes received. If the socket is a connection-oriented socket, and the connection has been reset as the result of a crash or other network failure, recv
will fail and set WSAGetLastError
to WSAECONNRESET
.
If an I/O error occurs, recv
will return SOCKET_ERROR
, and a specific error code can be retrieved by calling WSAGetLastError
.
The recv
function is used with connection-oriented sockets (for example, those created with the SOCK_STREAM
socket type). For connectionless sockets (for example, those created with the SOCK_DGRAM
socket type), use the recvfrom
function.
If recv
returns zero, the other end has performed an orderly shutdown with a shutdown call. By default, this function blocks until data is available or the connection is terminated.
If s
is a non-blocking socket and data is not yet available, recv
will return SOCKET_ERROR
and set WSAGetLastError
to WSAEWOULDBLOCK
.
The flags
parameter can be used to influence the behavior of the function call. The possible values for flags
are:
MSG_PEEK
: (Winsock 2) Specifies that the data should be observed without removing it from the input queue. The next read operation will read the data again.MSG_OOB
: (Winsock 2) Process out-of-band data.
Client: Supported in Windows Vista and later.
Server: Supported in Windows Vista and later.
Header: Declared in winsock2.h
(include winsock2.h
, ws2tcpip.h
)
Library: Link with Ws2_32.lib
For a list of error codes, see Winsock Error Codes.