receive a message from a socket
#include <sys/types.h>
#include <sys/socket.h>
int recv( int s,
void *buf,
int len,
int flags );
The recv() function receives a message from a socket.
It's normally used only on a
connected socket-see
connect()-and is identical to
recvfrom()
with a zero from parameter.
This routine returns the length of the message on
successful completion. If a message is too long for the
supplied buffer, buf, excess bytes may be discarded depending on
the type of socket that the message is received
from-see socket().
If no messages are available at the socket, the receive call
waits for a message to arrive, unless the socket is
nonblocking-see
ioctl()-in which case
-1 is returned and the external variable
errno is set to EWOULDBLOCK. Normally, the
receive calls return any data available, up to the requested
amount, rather than wait for the full amount requested; this
behavior is affected by the socket-level options SO_RCVLOWAT
and SO_RCVTIMEO described in
getsockopt().
The select() call may be used to determine when
more data is to arrive.
The flags argument is
formed by ORing one or more of the values:
- MSG_OOB
- Process out-of-band data. This flag
requests receipt of out-of-band data that wouldn't be
received in the normal data stream. This flag can't be used
with protocols that place expedited data at the head of the
normal data queue.
- MSG_PEEK
- Peek at incoming message. This flag
causes the receive operation to return data from the
beginning of the receive queue without removing that data
from the queue. Thus, a subsequent receive call will return
the same data.
- MSG_WAITALL
- Wait for full request or error. This
flag requests that the operation block until the full
request is satisfied. But the call may still return less
data than requested if a signal is caught, if an error or
disconnect occurs, or if the next data to be received is of
a different type than that returned.
The number of bytes received.
If an error occurred, errno could contain one of the
following:
- EBADF
- The argument s is an invalid descriptor.
- EFAULT
- The receive buffer pointer(s) point outside the
process's address space.
- EINTR
- The receive was interrupted by delivery of a signal
before any data was available.
- ENOTCONN
- The socket is associated with a connection-oriented
protocol and hasn't been connected; see connect()
and accept().
- EWOULDBLOCK
- Either the socket is marked nonblocking and the receive
operation would block, or a receive timeout had been set and
the timeout expired before data was received.
POSIX 1003.1g (draft)
Safety: | |
Interrupt handler |
No |
Signal handler |
No |
Thread |
Yes |
ioctl(),
getsockopt(),
recvfrom(),
recvmsg(),
socket()
read(),
select()
in the C Library Reference