#include <sys.types.h>
#include <sys.socket.h>
#include <netinet.in.h> /* AF_INET, AF_INET6 and connectionless operation*/
Kernighan-Ritchie-C:
int recv(s, buf, len, flags);
int s;
char *buf;
int len;
int flags;
int recvfrom(s, buf, len, flags, from, fromlen);
int s;
char *buf;
int len;
int flags;
struct sockaddr_in *from; /* AF_INET */
struct sockaddr_in6 *from; /* AF_INET6 */
int *fromlen;
ANSI-C:
int recv(int s, char* buf, int len, int flags);
int recvfrom(int s, char* buf, int len, int flags, struct sockaddr* from,
int* fromlen);
Description
The recv() and recvfrom() functions receive messages from a socket. The function recvmsg() is necessary, if messages are to be received from a raw socket.
recv() can only receive messages from a socket on which a connection has already been set up (see the connect() function in section "connect() - initiate a connection on a socket").
recvfrom() can receive messages from a socket with or without a connection.
The function call recvfrom() with from != null pointer and fromlen != null pointer is only supported for datagrams.
The s parameter designates the socket from which the message is received.
buf specifies the storage area in which the data is to be received.
len specifies the length of this buffer.
If the from parameter is not the null pointer (connectionless operation), the address of the message sender is stored in the address area referenced by from.
fromlen is a result parameter. Before the function is called, the integer variable to which fromlen points must contain the size of the buffer referenced by from. After the function returns, *fromlen contains the current length of the address stored in *from.
The function returns the length of the message.
The flags parameter is currently only supported with a datagram socket with the MSG_PEEK flag in the AF_INET and AF_INET6 address families. In the other cases flags should be supplied with the value 0.
MSG_PEEK allows data to be read without it being deleted at the source. This means that a repeat read operation is necessary.
The complete message must be read in a single operation with a datagram socket (only AF_INET, AF_INET6). If the specified message buffer is too small, the data extending beyond the buffer size is deleted.
Message limits are ignored with a stream socket (AF_INET, AF_INET6). As soon as data is available, it is returned to the caller, and no data is deleted.
Message limits are observed for a socket belonging to the AF_ISO address family. As soon as data is available, it is returned to the caller, and no data is deleted.
If no messages are available on the socket, the receive call waits for an incoming message unless the socket is non-blocking (see soc_ioctl()). In this case, -1 is returned, and the errno variable is set to the value EWOULDBLOCK.
The select() function can be used to determine when further data arrives.
Return value
>0:
If successful. The value indicates the number of received bytes.
=0:
If successful.
No more data can be received by sockets of type SOCK_STREAM or sockets belonging to the AF_ISO address family. The partner has closed his connection.
Sockets of type SOCK_DGRAM receive a data packet with the length 0 or the data is deleted by the transport system for a timeout.
-1:
If errors occur. errno is set to indicate the error.
Errors indicated by errno
EBADF
The s parameter is not a valid descriptor.
ECONNRESET
The connection to the partner was interrupted, although there were still some data packets expected (only with sockets of type SOCK_STREAM).
EFAULT
The length of the area for accepting the address is too small.
EIO
User data has been lost.
ENETDOWN
The connection to the network is down.
ENOTCONN
No connection exists for the socket.
EOPNOTSUPP
The flags parameter contains a non-zero value.
or
The socket is not of type SOCK_STREAM, and recv() supports only stream sockets.
EPIPE
There is no connection to the partner (only with sockets of type SOCK_STREAM).
EWOULDBLOCK
The socket is marked as non-blocking, and the requested operation would block.
See also
connect(), getsockopt(), select(), send(), soc_ioctl(), soc_read(), soc_readv(), socket()