recvfrom

recvfrom receives a message on a non-TCP socket and returns the number of bytes received. If the data is too long to fit in the supplied buffer, excess bytes may be discarded, depending on the socket type. On a datagram socket, if the data is too long to fit in with the supplied buffer, recvfrom fails 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

Copy
int recvfrom (
    [in]                  SOCKET s,
    [out]                 char * buf,
    [in]                  int len,
    [in]                  int flags,
    [out]                 struct sockaddr *from,
    [in, out, optional]   opt int *fromlen
);

Parameters

[in] s

The socket descriptor identifying the bound socket.

[out] buf

A pointer to a character buffer to hold received data.

[in] len

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

[in] flags

The 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 still maintaining its position in the queue as initially received. The next recvfrom call will read the same data.

[out] 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.

[in, out, optional] fromlen

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

Return Value

If the function succeeds, it returns the number of bytes received. If the function fails, it returns SOCKET_ERROR. Call 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 the raw socket. For example, on a raw Ethernet socket, if a UDP datagram is received, the count of bytes includes the IP and UDP headers and 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 Header Library

wRTOS 1.0 SDK

winsock2.h

RttcpipApi.lib