Receive data from a socket.
Syntax
#include <sys/socket.h>
ssize_t read (int fd, void *buf, size_t len);
ssize_t recv (int fd, void *buf, size_t len, int flags);
ssize_t recvfrom (int fd, void *restrict buf, size_t len, int flags,
struct sockaddr *restrict addr, socklen_t *restrict addrlen);
Parameters
-
fd
The socket to read from.
-
buf
The buffer that will receive the data.
-
len
The maximum number of bytes to receive. The buffer pointed to by
buf
must be at leastlen
bytes. -
flags
Option flags for
read
/recv
/recvfrom
. Currently not used by any supported networking types. -
addr
Optional pointer to an address structure that will receive the address of the sender. The size of the structure must be specified in
addrlen
. -
addrlen
If
addr
is specified, a pointer to asocklen_t
containing the size of theaddr
buffer. Ifaddr
is not specified, set to null. The pointed tosocklen_t
will be overwritten with the correct size of the address structure.
Return Value
The return value is the number of bytes received if successful. Otherwise, the return value is negative and errno is set:
-
EBADF
Bad file descriptor.
-
ENOMEM
Out of memory.
-
EINVAL
Invalid argument.
Remarks
See the documentation for the correct socket address family (network stack) for details related to it.
For datagram sockets, the buffer size must be large enough for the largest expected packet. Remaining bytes will be truncated.
read
is equal to recv
with the flags
argument set to zero. recv
is
equal to recvfrom
with the addr
and addrlen
arguments set to zero.