recvfrom

Top  Previous  Next

The recvfrom function receives a datagram and stores the source address.

It is normally used with connection-less sockets because it permits the application to retrieve the source address of received data.

It blocks until any data is available, connection is closed by peer, or any error occurs.

 

For message-based sockets (SOCK_DGRAM), the entire  message has to be read in a single operation. If a message is too long to fit in the supplied buffer, and MSG_PEEK is not set in the flags argument, the excess bytes are discarded. For stream based sockets (SOCK_STREAM), message boundaries are ignored, data is returned to the user as soon as it becomes available and no data is discarded.

 

Syntax

ssize_t recvfrom(

int socket,

void *buf,

size_t len,

int flags,

struct sockaddr *addr,

int *addrLen

);

 

Parameters

socket

A descriptor identifying a socket.

 

buf

A buffer for the incoming data.

 

len

Specifies the length in bytes of the buffer pointed by the buf argument.

 

flags

A set of options that modify the behavior of the function call beyond the options specified for the associated socket. Values of this argument are formed by logically OR'ing zero or more of the following values:

 

Type

Meaning

MSG_PEEK

Peeks at an incoming message. The data is treated as unread and next recv or similar function shall still return this data.

MSG_OOB

Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific.

MSG_WAITALL

On SOCK_STREAM sockets this requests that the function block until the full amount of data can be returned. This function may return the smaller amount of data if the socket is a message-based socket, if the connection is terminated, if MSG_PEEK was specified, or if an error is pending for the socket.

 

addr

An optional pointer to a buffer in a sockaddr structure that will hold the source address upon return.

 

addrLen

An optional pointer to the size, in bytes, of the buffer pointed to by the from parameter.

 

Return value

Upon successful completion, recvfrom returns the length of the message in bytes. If no messages are available to be received and the peer has performed an orderly shutdown, recvfrom returns 0. Otherwise, -1 is returned and errno set to indicate the error.