The recv() call
receives data on a socket with descriptor s and
stores it in a buffer. The recv() call applies only to connected sockets.
This
call returns the length of the incoming message or data. If data is
not available for socket s, and s is
in blocking mode, the recv() call blocks the caller until data arrives.
If data is not available and s is in nonblocking
mode, recv() returns a -1 and sets errno to EWOULDBLOCK. See fcntl() or ioctl() for
a description of how to set nonblocking mode.
If a datagram
packet is too long to fit in the supplied buffer, datagram sockets
discard extra bytes. Stream sockets act like streams of information
with no boundaries separating data. For example, if applications
A and B are connected with a stream socket and Application A sends
1000 bytes, each call to this function can return 1 byte, or 10 bytes,
or the entire 1000 bytes. Therefore, applications using stream sockets
should place this call in a loop and call this function until all
data has been received.
#include <manifest.h>
#include <bsdtypes.h>
#include <socket.h>
int recv(int s, char *buf, int len, int flags)
- Parameter
- Description
- s
- Socket descriptor.
- buf
- Points to the buffer that receives the data.
- len
- Length in bytes of the buffer pointed to by buf.
- flags
- Set the flags parameter by specifying
one or more of the following flags. If more
than one flag is specified, the logical OR operator (|) must be used
to separate them. Setting this parameter is supported only for sockets
in the AF_INET domain. Setting these flags is not supported for sockets
in the AF_IUCV domain.
- MSG_OOB
- Reads any out-of-band data on the socket. This is valid for stream
(TCP) sockets only.
- MSG_PEEK
- Peeks at the data present on the socket; the data is returned
but not consumed, so that a subsequent receive operation sees the
same data.
Return values
If successful, the byte length
of the message or datagram is returned. The value -1 indicates
an error. The value 0 indicates connection closed. Errno identifies
the specific error.
- Errno
- Description
- EBADF
- Indicates that s is not a valid socket
descriptor.
- EFAULT
- Using the buf and len parameters
would result in an attempt to access storage outside the caller address
space.
- EWOULDBLOCK
- Indicates that s is in nonblocking mode
and data is not available to read.
- ENOTCONN
- Indicates an unconnected TCP socket.
- EMSGSIZE
- For non-TCP sockets, this indicates that length exceeds the maximum
data size as determined by getsockopt() using SO_SNDBUF for the socket
type, either TCP, UDP, or RAW.
Related calls
connect(), fcntl(), getsockopt(),
ioctl(), read(), readv(), recvfrom(), recvmsg(), select(), selectex(),
send(), sendmsg(), sendto(), setsockopt(), socket(), write(), writev()