1RECV(2) System Calls 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 cc = recv(s, buf, len, flags)
13 int cc, s;
14 char *buf;
15 int len, flags;
16
17 cc = recvfrom(s, buf, len, flags, from, fromlen)
18 int cc, s;
19 char *buf;
20 int len, flags;
21 struct sockaddr *from;
22 int *fromlen;
23
24 cc = recvmsg(s, msg, flags)
25 int cc, s;
26 struct msghdr msg[];
27 int flags;
28
30 Recv, recvfrom, and recvmsg are used to receive messages from a socket.
31
32 The recv call is normally used only on a connected socket (see con‐
33 nect(2)), while recvfrom and recvmsg may be used to receive data on a
34 socket whether it is in a connected state or not.
35
36 If from is non-zero, the source address of the message is filled in.
37 Fromlen is a value-result parameter, initialized to the size of the
38 buffer associated with from, and modified on return to indicate the
39 actual size of the address stored there. The length of the message is
40 returned in cc. If a message is too long to fit in the supplied buf‐
41 fer, excess bytes may be discarded depending on the type of socket the
42 message is received from (see socket(2)).
43
44 If no messages are available at the socket, the receive call waits for
45 a message to arrive, unless the socket is nonblocking (see ioctl(2)) in
46 which case a cc of -1 is returned with the external variable errno set
47 to EWOULDBLOCK.
48
49 The select(2) call may be used to determine when more data arrives.
50
51 The flags argument to a recv call is formed by or'ing one or more of
52 the values,
53
54 #define MSG_OOB 0x1 /* process out-of-band data */
55 #define MSG_PEEK 0x2 /* peek at incoming message */
56
57 The recvmsg call uses a msghdr structure to minimize the number of
58 directly supplied parameters. This structure has the following form,
59 as defined in <sys/socket.h>:
60
61 struct msghdr {
62 caddr_t msg_name; /* optional address */
63 int msg_namelen; /* size of address */
64 struct iovec *msg_iov; /* scatter/gather array */
65 int msg_iovlen; /* # elements in msg_iov */
66 caddr_t msg_accrights; /* access rights sent/received */
67 int msg_accrightslen;
68 };
69
70 Here msg_name and msg_namelen specify the destination address if the
71 socket is unconnected; msg_name may be given as a null pointer if no
72 names are desired or required. The msg_iov and msg_iovlen describe the
73 scatter gather locations, as described in read(2). A buffer to receive
74 any access rights sent along with the message is specified in
75 msg_accrights, which has length msg_accrightslen. Access rights are
76 currently limited to file descriptors, which each occupy the size of an
77 int.
78
80 These calls return the number of bytes received, or -1 if an error
81 occurred.
82
84 The calls fail if:
85
86 [EBADF] The argument s is an invalid descriptor.
87
88 [ENOTSOCK] The argument s is not a socket.
89
90 [EWOULDBLOCK] The socket is marked non-blocking and the receive
91 operation would block.
92
93 [EINTR] The receive was interrupted by delivery of a signal
94 before any data was available for the receive.
95
96 [EFAULT] The data was specified to be received into a non-
97 existent or protected part of the process address
98 space.
99
101 fcntl(2), read(2), send(2), select(2), getsockopt(2), socket(2)
102
103
104
1054.2 Berkeley Distribution May 23, 1986 RECV(2)