Sockets Programming Interfaces
3-23
Sockets and Stream IO API
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 recv() function attempts to receive data from a socket. It is normally used
on a connected socket (see connect()). The data is placed into the buffer spe-
cified 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 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
as well as the socket-level options SO_RCVLOWAT and SO_RCVTIMEO de-
scribed in getsockopt().
The select call (fdSelect()) may be used to determine when more data arrives.
The flags argument to a recv() call is formed by combining one or more of the
following flags:
MSG_DONTWAIT
requests that the operation not block when no data
is available.
MSG_OOB
requests receipt of out-of-band data that would not
be received in the normal data stream. Some proto-
cols place expedited data at the head of the normal
data queue, and thus this flag cannot be used with
such protocols.
MSG_PEEK
causes the receive operation to return data from the
beginning of the receive queue without removing
that data from the queue. Thus, a subsequent re-
ceive call will return the same data.
MSG_WAITALL
requests that the operation block until the full re-
quest is satisfied. However, the call may still return
less data than requested if an error or disconnect
occurs, or the next data to be received is of a differ-
ent type than that returned.