1 .\" $OpenBSD: recv.2,v 1.43 2015/02/16 16:20:15 tedu Exp $
2 .\" $NetBSD: recv.2,v 1.6 1995/02/27 12:36:08 cgd Exp $
4 .\" Copyright (c) 1983, 1990, 1991, 1993
5 .\" The Regents of the University of California. All rights reserved.
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
15 .\" 3. Neither the name of the University nor the names of its contributors
16 .\" may be used to endorse or promote products derived from this software
17 .\" without specific prior written permission.
19 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .\" @(#)recv.2 8.3 (Berkeley) 2/21/94
40 .Nd receive a message from a socket
44 .Fn recv "int s" "void *buf" "size_t len" "int flags"
46 .Fn recvfrom "int s" "void *buf" "size_t len" "int flags" "struct sockaddr *from" "socklen_t *fromlen"
48 .Fn recvmsg "int s" "struct msghdr *msg" "int flags"
53 are used to receive messages from a socket,
55 and may be used to receive
56 data on a socket whether or not it is connection-oriented.
60 is non-null and the socket is not connection-oriented,
61 the source address of the message is filled in.
63 is a value-result parameter, initialized to the size of
64 the buffer associated with
66 and modified on return to indicate the actual size of the
71 call is normally used only on a
81 On successful completion, all three routines return the number of
83 If a message is too long to fit in the supplied
84 buffer, excess bytes may be discarded depending on the type of socket
85 the message is received from (see
88 If no messages are available at the socket, the
89 receive call waits for a message to arrive, unless
90 the socket is nonblocking (see
92 in which case the value
93 \-1 is returned and the external variable
97 The receive calls normally return any data available,
98 up to the requested amount,
99 rather than waiting for receipt of the full amount requested;
100 this behavior is affected by the socket-level options
111 system calls may be used to determine when more data arrive.
115 argument is the bitwise OR of zero or more of the following values:
117 .Bl -tag -width "MSG_DONTWAITXX" -offset indent -compact
119 process out-of-band data
121 peek at incoming message
123 wait for full request or error
130 flag requests receipt of out-of-band data
131 that would not be received in the normal data stream.
132 Some protocols place expedited data at the head of the normal
133 data queue, and thus this flag cannot be used with such protocols.
136 flag causes the receive operation to return data
137 from the beginning of the receive queue without removing that
139 Thus, a subsequent receive call will return the same data.
142 flag requests that the operation block until
143 the full request is satisfied.
144 However, the call may still return less data than requested
145 if a signal is caught, an error or disconnect occurs,
146 or the next data to be received is of a different type than that returned.
149 flag requests the call to return when it would block otherwise.
150 If no data is available,
159 structure to minimize the number of directly supplied parameters.
160 This structure has the following form, as defined in
164 void *msg_name; /* optional address */
165 socklen_t msg_namelen; /* size of address */
166 struct iovec *msg_iov; /* scatter/gather array */
167 int msg_iovlen; /* # elements in msg_iov */
168 void *msg_control; /* ancillary data, see below */
169 socklen_t msg_controllen; /* ancillary data buffer len */
170 int msg_flags; /* flags on received message */
178 specify the source address if the socket is unconnected;
180 may be given as a null pointer if no names are desired or required.
184 describe scatter gather locations, as discussed in
189 points to a buffer for other protocol control related messages
190 or other miscellaneous ancillary data.
191 The messages are of the form:
194 socklen_t cmsg_len; /* data byte count, including hdr */
195 int cmsg_level; /* originating protocol */
196 int cmsg_type; /* protocol-specific type */
197 /* followed by u_char cmsg_data[]; */
203 for how these messages are constructed and decomposed.
205 Open file descriptors are now passed as ancillary data for
220 field is set on return according to the message received.
221 It will contain zero or more of the following values:
223 .Bl -tag -width MSG_CTRUNC -offset indent -compact
225 Returned to indicate that expedited or out-of-band data was received.
227 Indicates end-of-record;
228 the data returned completed a record (generally used with sockets of type
229 .Dv SOCK_SEQPACKET ) .
232 the trailing portion of a datagram was discarded because the datagram
233 was larger than the buffer supplied.
236 control data were discarded due to lack of space in the buffer
240 These calls return the number of bytes received, or \-1 if an error occurred.
247 .Bl -tag -width "[EHOSTUNREACH]"
251 is an invalid descriptor.
253 The socket is associated with a connection-oriented protocol
254 and has not been connected (see
261 does not refer to a socket.
263 The socket is marked non-blocking, and the receive operation
265 a receive timeout had been set,
266 and the timeout expired before data were received.
268 The receive was interrupted by delivery of a signal before
269 any data were available.
271 The receive buffer pointer(s) point outside the process's
273 .It Bq Er ECONNREFUSED
274 The socket is associated with a connection-oriented protocol
275 and the connection was forcefully rejected (see
278 The socket is associated with a connection-oriented protocol
279 and the connection was forcefully closed by the peer.
286 may return the following error:
296 may return one of the following errors:
310 was less than 0 or larger than
334 flag is an extension to that specification.
338 function call appeared in