Sockets Programming Interfaces
3-24
Receive Data from a Socket with the Sender’s Name (address)
recvfrom
Syntax
int recvfrom( int s, void *pbuf, int size, int flags, PSA pName, int *plen );
Parameter(s)
s
Socket
pbuf
Data buffer to place received data
size
Size of desired data
flags
Option flags
pName
Pointer to place name (address) of sender
plen
Pointer to size of pName
Return Value
If it succeeds, the function returns the number of bytes received. Otherwise,
a value of –1 is returned and the function fdError() can be called to determine
the error:
EBADF
The file descriptor (socket) is invalid.
ENOTSOCK
The file descriptor does not reference a socket.
EINVAL
Name arguments are invalid.
ENOTCONN
The socket is connection oriented and not con-
nected.
EWOULDBLOCK
The socket is specified as non-blocking, or the
timeout has expired.
Description
The recvfrom() function attempts to receive data from a socket. It is normally
called with unconnected, non-connection oriented sockets. The data is placed
into the buffer specified by pbuf, up to a maximum length specified by size. The
options in flags can be used to change the default behavior of the operation.
The name (address) of the sender is written to pName.
The argument pName is a result parameter that is filled in with the address of
the sending entity as known to the communications layer. The domain in which
the communication is occurring determines the exact format of the pName pa-
rameter. The plen is a value-result parameter; it should initially contain at least
sizeof( struct sockaddr ), the amount of space pointed to by pName; on return
it will contain the actual length (in bytes) of the address returned.
The functions returns the length of the message on successful completion.
For a datagram type socket, the receive operation always copies one packet’s
worth of data. If the buffer is too short to hold the entire packet, the data is trun-
cated and lost.
If no messages are available at the socket, it waits for a message to arrive, un-
less the socket is non-blocking. The function normally returns any data avail-
able, up to the requested amount, rather than waiting for receipt of the full
amount requested; this behavior is affected by the options specified in flags