recv Function

The recv function retrieves incoming data from a connected socket.

Syntax

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

Parameters

Parameter Description
s A descriptor identifying a socket.
buf A pointer to the buffer that will receive the incoming data.
len The maximum number of bytes to receive.
flags Flags that control the reception of data.

Return Value

If no error occurs, recv returns the number of bytes received. If the calling process has no message waiting to be received on a nonblocking socket and (MSG_DONTWAIT | MSG_PARTIAL) are not set, recv returns 0.

If an error occurs, recv returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.

Note The socket function recv is used for stream sockets and datagram sockets.
Important This function is synchronous. It blocks the calling thread until data is received or an error occurs. For asynchronous operations, consider using WSARecv or related overlapped I/O functions.

Remarks

The recv function is used to receive data on a socket. For a connection-oriented socket, the data is received from the socket specified by the s parameter. The data is copied into the buffer pointed to by the buf parameter.

The recv call can be one of the following:

The len parameter specifies the maximum number of bytes to receive. If the data received is less than len bytes, the buffer contains only the data received.

If len is greater than the amount of data available to be received on the socket, the call will return with all the data available. For example, if there are 100 bytes of data and len is 500, the call will return with 100 bytes of data.

Flags

The flags parameter can be used to influence the behavior of the receive operation. Common flags include:

Refer to the Winsock documentation for a complete list of available flags.

Example

The following code snippet demonstrates a basic usage of the recv function:

#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>

#pragma comment(lib, "Ws2_32.lib")

int main() {
    WSADATA wsaData;
    SOCKET ClientSocket = INVALID_SOCKET;
    char recvbuf[512];
    int recvbuflen = 512;
    int iResult;

    // Initialize Winsock
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        std::cerr << "WSAStartup failed." << std::endl;
        return 1;
    }

    // Create a socket (assuming a client socket connected to a server)
    // This is a simplified example; actual socket creation and connection
    // would be more involved.
    ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (ClientSocket == INVALID_SOCKET) {
        std::cerr << "socket failed with error: " << WSAGetLastError() << std::endl;
        WSACleanup();
        return 1;
    }

    // ... Connect ClientSocket to a server ...

    // Receive data
    iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
    if (iResult > 0) {
        std::cout << "Bytes received: " << iResult << std::endl;
        // Process the received data in recvbuf
        recvbuf[iResult] = '\0'; // Null-terminate for string operations
        std::cout << "Received: " << recvbuf << std::endl;
    } else if (iResult == 0) {
        std::cout << "Connection closing..." << std::endl;
    } else {
        std::cerr << "recv failed with error: " << WSAGetLastError() << std::endl;
    }

    // Cleanup
    closesocket(ClientSocket);
    WSACleanup();

    return 0;
}

See Also