Jump to main content

##

# recv Function

## C

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

## Description

This function is used to reveive incoming data that has been queued for a socket. This function can be used with both datagram and stream socket. If the available data is too large to fit in the supplied application buffer buf, excess bytes are discarded in case of SOCK_DGRAM type sockets. For SOCK_STREAM types, the data is buffered internally so the application can retrieve all data by multiple calls of recvfrom.

## Preconditions

The connect() function should be called for TCP and UDP sockets. Server side, accept() function should be called.

## Parameters

Parameters | Description
--- | ---
s | Socket descriptor returned from previous call to socket().
buf | Application data receive buffer.
len | Buffer length in bytes.
flags | No significance in this implementation.

## Returns

- If the recv() function is successful, the coket is valid and it has pending data:

  - The supplied buffer is non NULL and has non zero length, the function will return the number of bytes copied to the application buffer.
  - The supplied buffer is NULL or has zero length then no data will be copied and the function will return the number of bytes pending in the socket buffer.

- A return value of SOCKET_ERROR (-1) indicates an error condition (errno is set accordingly). errno is set to EWOULDBLOCK if there is no data pending in the socket buffer.

- A return value of 0 indicates socket has been shutdown by the peer.

## Remarks

None.