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