BSD Sockets Interface Programmer's Guide

98 Chapter 4
Using Internet Datagram Sockets
Sending and Receiving Messages
Function result: Number of bytes actually received, –1 if an error
occurs.
Example:
addrlen = sizeof(sockaddr_in);
...
count = recvfrom(s, buffer, BUFFERSIZE, 0, clientaddr, &addrlen);
recvfrom blocks until there is a message to be received.
No more than len bytes of data are returned. The entire message is read
in one recvfrom, recvmsg, recv or read operation. If the message is
too long for the allocated buffer, the excess data are discarded. Because
only one message can be returned in a recvfrom call, if a second
message is in the queue, it is not affected. Therefore, the best technique
is to receive as much as possible on each call.
The host does not wait for len bytes to be available; if less than len
bytes are available, that number of bytes are returned.
Flag Options
The flag options are:
0 for no options.
flags settings for
optional flags
0 or MSG_PEEK unchanged
from address of
socket that
sent message
pointer to address
structure, not used
for input
pointer to
socket
address of
socket that
sent the
message
fromlen pointer to the
size of from
pointer to size of
from
pointer to
the actual
size of
address
returned
Parameter Contents INPUT Value
OUTPUT
Value