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 type of the socket. On a datagram socket, if the data is too long to fit in 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 (
    SOCKET s,
    char * buf,
    int len,
    int flags,
    struct sockaddr *from,
    opt int *fromlen
);

Parameters

s

A socket descriptor identifying bound socket.

buf

A 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.

from

If from is non-zero, 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 struct sockaddr.

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

If the function succeeds, it returns the number of bytes received. If the function fails, it returns SOCKET_ERROR.

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 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 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

eRTOS 1.0 SDK

Winsock2.h RtTcpipApi.lib