The Windows Sockets recv function receives data from a socket.

int recv (


char FAR* buf,

int len,

int flags




[in] A descriptor identifying a connected socket.


[out] A buffer for the incoming data.


[in] The length of buf.


[in] Specifies the way in which the call is made.


This function is used on connected sockets or bound connectionless sockets specified by the s parameter and is used to read incoming data.

For byte stream style socket (for example, type SOCK_STREAM), as much information as is currently available up to the size of the buffer supplied is returned. If the socket has been configured for in-line reception of out-of-band data (socket option SO_OOBINLINE) and out-of-band data is unread, only out-of-band data will be returned. The application can use the ioctlsocket SIOCATMARK to determine whether any more out-of-band data remains to be read.

For message-oriented sockets (for example, type SOCK_DGRAM), data is extracted from the first enqueued datagram (message) from the destination address specified in the connect call. If unconnected, the socket must be bound, and there are no source address restrictions on datagrams received. If the datagram or message is larger than the buffer supplied, the buffer is filled with the first part of the datagram, and recv generates the error WSAEMSGSIZE. For unreliable protocols (for example, UDP) the excess data is lost, for reliable protocols the data is retained by the service provider until it is successfully read by calling recv with a large enough buffer.

If no incoming data is available at the socket, the recv call waits for data to arrive unless the socket is nonblocking. In this case, a value of SOCKET_ERROR is returned with the error code set to WSAEWOULDBLOCK. The select, WSAAsyncSelect, or WSAEventSelect calls can be used to determine when more data arrives.

If the socket is connection oriented and the remote side has shut down the connection gracefully, a recv will complete immediately with zero bytes received. If the connection has been reset, a recv will fail with the error WSAECONNRESET.

The flags parameter can be used to influence the behavior of the function invocation beyond the options specified for the associated socket. That is, the semantics of this function are determined by the socket options and the flags parameter. The latter is constructed by or'ing any of the following values:

Peek at the incoming data. The data is copied into the buffer but is not removed from the input queue.
Process out-of-band data. (See section Out-Of-Band data for a discussion of this topic.)

Return Values

If no error occurs, recv returns the number of bytes received. If the connection has been gracefully closed, the return value is zero. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

Error Codes

A successful WSAStartup must occur before using this function.
The network subsystem has failed.
The buf argument is not totally contained in a valid part of the user address space.
The socket is not connected.
The (blocking) call was canceled through WSACancelBlockingCall.
A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function.
The connection has been broken due to the remote host resetting.
The descriptor is not a socket.
MSG_OOB was specified, but the socket is not stream style such as type SOCK_STREAM, out-of-band data is not supported in the communication domain associated with this socket, or the socket is unidirectional and supports only send operations.
The socket has been shut down; it is not possible to recv on a socket after shutdown has been invoked with how set to SD_RECEIVE or SD_BOTH.
The socket is marked as nonblocking and the receive operation would block.
The message was too large to fit into the specified buffer and was truncated.
The socket has not been bound with bind, or an unknown flag was specified, or MSG_OOB was specified for a socket with SO_OOBINLINE enabled or (for byte stream sockets only) len was zero or negative.
The virtual circuit was terminated due to a time-out or other failure. The application should close the socket as it is no longer usable.
The connection has been dropped because of a network failure or because the peer system failed to respond.
The virtual circuit was reset by the remote side executing a "hard" or "abortive" close. The application should close the socket as it is no longer usable. On a UDP datagram socket this error would indicate that a previous send operation resulted in an ICMP "Port Unreachable" message.

See Also

, select, send, socket, WSAAsyncSelect

Software for developers
Delphi Components
.Net Components
Software for Android Developers
More information resources
Unix Manual Pages
Delphi Examples
Databases for Amazon shops developers
Amazon Categories Database
Browse Nodes Database