1recv(3SOCKET) Sockets Library Functions recv(3SOCKET)
2
3
4
6 recv, recvfrom, recvmsg - receive a message from a socket
7
9 cc [ flag... ] file... -lsocket -lnsl [ library... ]
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <sys/uio.h>
13
14 ssize_t recv(int s, void *buf, size_t len, int flags);
15
16
17 ssize_t recvfrom(int s, void *buf, size_t len, int flags,
18 struct sockaddr *from, socklen_t *fromlen);
19
20
21 ssize_t recvmsg(int s, struct msghdr *msg, int flags);
22
23
25 The recv(), recvfrom(), and recvmsg() functions are used to receive
26 messages from another socket. The s socket is created with
27 socket(3SOCKET).
28
29
30 If from is a non-NULL pointer, the source address of the message is
31 filled in. The value-result parameter fromlen is initialized to the
32 size of the buffer associated with from and modified on return to indi‐
33 cate the actual size of the address stored in the buffer. The length of
34 the message is returned. If a message is too long to fit in the sup‐
35 plied buffer, excess bytes may be discarded depending on the type of
36 socket from which the message is received. See socket(3SOCKET).
37
38
39 If no messages are available at the socket, the receive call waits for
40 a message to arrive. If the socket is non-blocking, -1 is returned with
41 the external variable errno set to EWOULDBLOCK. See fcntl(2).
42
43
44 For processes on the same host, recvmsg() can be used to receive a file
45 descriptor from another process, but it cannot receive ancillary data.
46 See libxnet(3LIB).
47
48
49 If a zero-length buffer is specified for a message, an EOF condition
50 results that is indistinguishable from the successful transfer of a
51 file descriptor. For that reason, one or more bytes of data should be
52 provided when recvmsg() passes a file descriptor.
53
54
55 The select(3C) call can be used to determine when more data arrives.
56
57
58 The flags parameter is formed by an OR operation on one or more of the
59 following:
60
61 MSG_OOB Read any out-of-band data present on the socket rather
62 than the regular in-band data.
63
64
65 MSG_PEEK Peek at the data present on the socket. The data is
66 returned, but not consumed to allow a subsequent
67 receive operation to see the same data.
68
69
70 MSG_WAITALL Messages are blocked until the full amount of data
71 requested is returned. The recv() function can return a
72 smaller amount of data if a signal is caught, the con‐
73 nection is terminated, MSG_PEEK is specified, or if an
74 error is pending for the socket.
75
76
77 MSG_DONTWAIT Pending messages received on the connection are
78 returned. If data is unavailable, the function does not
79 block. This behavior is the equivalent to specifying
80 O_NONBLOCK on the file descriptor of a socket, except
81 that write requests are unaffected.
82
83
84
85 The recvmsg() function call uses a msghdr structure defined in
86 <sys/socket.h> to minimize the number of directly supplied parameters.
87
89 Upon successful completion, these functions return the number of bytes
90 received. Otherwise, they return -1 and set errno to indicate the
91 error.
92
94 The recv(), recvfrom(), and recvmsg() functions return errors under the
95 following conditions:
96
97 EBADF The s file descriptor is invalid.
98
99
100 EINVAL The MSG_OOB flag is set and no out-of-band data is
101 available.
102
103
104 EINTR The operation is interrupted by the delivery of a sig‐
105 nal before any data is available to be received.
106
107
108 EIO An I/O error occurs while reading from or writing to
109 the file system.
110
111
112 ENOMEM Insufficient user memory is available to complete oper‐
113 ation.
114
115
116 ENOSR Insufficient STREAMS resources are available for the
117 operation to complete.
118
119
120 ENOTSOCK s is not a socket.
121
122
123 ESTALE A stale NFS file handle exists.
124
125
126 EWOULDBLOCK The socket is marked non-blocking and the requested
127 operation would block.
128
129
130 ECONNREFUSED The requested connection was refused by the peer. For
131 connected IPv4 and IPv6 datagram sockets, this indi‐
132 cates that the system received an ICMP Destination Port
133 Unreachable message from the peer.
134
135
136
137 The recv() and recvfrom() functions fail under the following condi‐
138 tions:
139
140 EINVAL The len argument overflows a ssize_t.
141
142
143
144 The recvmsg() function returns errors under the following conditions:
145
146 EINVAL The msg_iovlen member of the msghdr structure pointed to by
147 msg is less than or equal to 0, or greater than [IOV_MAX}.
148 See Intro(2) for a definition of [IOV_MAX}.
149
150
151 EINVAL One of the iov_len values in the msg_iov array member of the
152 msghdr structure pointed to by msg is negative, or the sum of
153 the iov_len values in the msg_iov array overflows a ssize_t.
154
155
157 See attributes(5) for descriptions of the following attributes:
158
159
160
161
162 ┌─────────────────────────────┬─────────────────────────────┐
163 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
164 ├─────────────────────────────┼─────────────────────────────┤
165 │Interface Stability │Committed │
166 ├─────────────────────────────┼─────────────────────────────┤
167 │MT-Level │Safe │
168 └─────────────────────────────┴─────────────────────────────┘
169
171 fcntl(2), ioctl(2), read(2), connect(3SOCKET), getsockopt(3SOCKET),
172 libxnet(3LIB), select(3C), send(3SOCKET), socket(3SOCKET),
173 socket.h(3HEAD), attributes(5)
174
175
176
177SunOS 5.11 20 Aug 2007 recv(3SOCKET)