recvfrom

recvfrom receives a datagram on a socket and returns the number of bytes received. If on a datagram socket the data is too long to fit in the supplied buffer, excess bytes are discarded, and recvfrom returns SOCKET_ERROR with error code WSAEMSGSIZE.

On a blocking socket, recvfrom blocks until some data has been received. On a non-blocking socket, in absence of received data, recv fails with error code EWOULDBLOCK.

Syntax

int recvfrom (
    SOCKET s,
    char * buf,
    int len,
    int flags,
    struct sockaddr *from,
    opt int *fromlen
);

Parameters

s

Socket descriptor identifying bound socket.

buf

Pointer to a character buffer to hold received data.

len

An integer that defines the length, in bytes, of the buf parameter.

flags

Receive flags. The logical OR of one or more of the following:

Flag Description

MSG_PEEK

This value can be used to read the data while it is still maintaining its position in the queue as originally received. The next recvfrom call will read the same data.

MSG_OOB

This value can be used to receive out-of-band data. If no out-of-band data is present, recv will return -1. Urgent data is data that was sent with the MSG_OOB flag. The application is responsible for any special handling that may be required for the delivery of urgent data. Urgent data is only implemented for TCP.

Note: For TCP, if the MSG_OOB flag is specified and either no urgent data is present or the socket option SO_OOBINLINE has been specified, recv returns -1. If there is urgent data on its way but it has not yet arrived, recv returns -1 and WSAGetLastError returns WSAEWOULDBLOCK.

from

If from is nonzero, the source address of the received packet is filled in. On a UDP or TCP socket, the application MUST pass in the address of a sockaddr structure.

fromlen

Initialized to the size of the address structure associated with from. Upon return, fromlen is modified to indicate the actual size of the address stored in from.

Return Value

The number of bytes received if the function succeeds, SOCKET_ERROR if the function fails

Use WSAGetLastError to retrieve a specific error code.

Remarks

On a raw socket, the count of bytes includes all protocol headers encapsulated within the header of of the raw socket. For example, on a raw Ethernet socket, if a UDP datagram is received, then the count of bytes includes the IP and UDP headers as well as user data.

For a non-stream socket, a return value of zero indicates that a valid zero-length message was received.

Requirements

Minimum Supported Version RTX64 2013
Header Winsock2.h
Library RtTcpip.lib