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