1RECV(2) Linux Programmer's Manual RECV(2)
2
3
4
6 recv, recvfrom, recvmsg - receive a message from a socket
7
9 #include <sys/types.h>
10 #include <sys/socket.h>
11
12 ssize_t recv(int sockfd, void *buf, size_t len, int flags);
13
14 ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
15 struct sockaddr *src_addr, socklen_t *addrlen);
16
17 ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
18
20 The recvfrom() and recvmsg() calls are used to receive messages from a
21 socket, and may be used to receive data on a socket whether or not it
22 is connection-oriented.
23
24 If src_addr is not NULL, and the underlying protocol provides the
25 source address, this source address is filled in. When src_addr is
26 NULL, nothing is filled in; in this case, addrlen is not used, and
27 should also be NULL. The argument addrlen is a value-result argument,
28 which the caller should initialize before the call to the size of the
29 buffer associated with src_addr, and modified on return to indicate the
30 actual size of the source address. The returned address is truncated
31 if the buffer provided is too small; in this case, addrlen will return
32 a value greater than was supplied to the call.
33
34 The recv() call is normally used only on a connected socket (see con‐
35 nect(2)) and is identical to recvfrom() with a NULL src_addr argument.
36
37 All three routines return the length of the message on successful com‐
38 pletion. If a message is too long to fit in the supplied buffer,
39 excess bytes may be discarded depending on the type of socket the mes‐
40 sage is received from.
41
42 If no messages are available at the socket, the receive calls wait for
43 a message to arrive, unless the socket is nonblocking (see fcntl(2)),
44 in which case the value -1 is returned and the external variable errno
45 is set to EAGAIN or EWOULDBLOCK. The receive calls normally return any
46 data available, up to the requested amount, rather than waiting for
47 receipt of the full amount requested.
48
49 The select(2) or poll(2) call may be used to determine when more data
50 arrives.
51
52 The flags argument to a recv() call is formed by ORing one or more of
53 the following values:
54
55 MSG_CMSG_CLOEXEC (recvmsg() only; since Linux 2.6.23)
56 Set the close-on-exec flag for the file descriptor received via
57 a UNIX domain file descriptor using the SCM_RIGHTS operation
58 (described in unix(7)). This flag is useful for the same rea‐
59 sons as the O_CLOEXEC flag of open(2).
60
61 MSG_DONTWAIT (since Linux 2.2)
62 Enables nonblocking operation; if the operation would block, the
63 call fails with the error EAGAIN or EWOULDBLOCK (this can also