Windows API Reference

Networking / Winsock /recv

recv Function

The recv function retrieves incoming data from a connected socket or a listening socket.

Syntax

int recv(
      SOCKET s,
      char *buf,
      int len,
      int flags
    );
            

Parameters

Parameter Description
s A descriptor identifying an unconnected or connection-oriented socket.
buf A buffer into which the received data will be copied.
len The maximum number of bytes that can be received.
flags Flags that specify the way a call to this function is made. Can be zero or any combination of the MSG_ flags.

Return Value

If no error occurs, recv returns the number of bytes received, and the buffer indicated by buf will contain this data.
If the calling process has put the socket into nonsocket mode with a successful call to ioctlsocket, zero is returned.
If the connection has been gracefully closed, zero is returned.
If an operating system error occurs, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

Remarks

The recv function is used with connection-oriented protocols, such as the Transmission Control Protocol (TCP).

If the current socket is connection-oriented, the recv function is used to read data from the socket. The number of bytes read is specified by the len parameter. If len bytes or more are available, recv reads up to len bytes.

If the socket has been configured for non-blocking operation, and the data is not yet available, recv will return SOCKET_ERROR with the error code set to WSAEWOULDBLOCK.

For a connection-oriented socket, if all data has been received, recv returns 0.

Requirements

Attribute Value
Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Header winsock2.h
Library Ws2_32.lib
DLL Ws2_32.dll

See Also