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
127 don't generate SIGPIPE
132 flag requests receipt of out-of-band data
133 that would not be received in the normal data stream.
134 Some protocols place expedited data at the head of the normal
135 data queue, and thus this flag cannot be used with such protocols.
138 flag causes the receive operation to return data
139 from the beginning of the receive queue without removing that
141 Thus, a subsequent receive call will return the same data.
144 flag requests that the operation block until
145 the full request is satisfied.
146 However, the call may still return less data than requested
147 if a signal is caught, an error or disconnect occurs,
148 or the next data to be received is of a different type than that returned.
151 flag requests the call to return when it would block otherwise.
152 If no data is available,
158 flag requests that disconnected stream-oriented sockets don't generate
165 structure to minimize the number of directly supplied parameters.
166 This structure has the following form, as defined in
170 void *msg_name; /* optional address */
171 socklen_t msg_namelen; /* size of address */
172 struct iovec *msg_iov; /* scatter/gather array */
173 int msg_iovlen; /* # elements in msg_iov */
174 void *msg_control; /* ancillary data, see below */
175 socklen_t msg_controllen; /* ancillary data buffer len */
176 int msg_flags; /* flags on received message */
184 specify the source address if the socket is unconnected;
186 may be given as a null pointer if no names are desired or required.
190 describe scatter gather locations, as discussed in
195 points to a buffer for other protocol control related messages
196 or other miscellaneous ancillary data.
197 The messages are of the form:
200 socklen_t cmsg_len; /* data byte count, including hdr */
201 int cmsg_level; /* originating protocol */
202 int cmsg_type; /* protocol-specific type */
203 /* followed by u_char cmsg_data[]; */
209 for how these messages are constructed and decomposed.
211 Open file descriptors are now passed as ancillary data for
226 field is set on return according to the message received.
227 It will contain zero or more of the following values:
229 .Bl -tag -width MSG_CTRUNC -offset indent -compact
231 Returned to indicate that expedited or out-of-band data was received.
233 Indicates end-of-record;
234 the data returned completed a record (generally used with sockets of type
235 .Dv SOCK_SEQPACKET ) .
238 the trailing portion of a datagram was discarded because the datagram
239 was larger than the buffer supplied.
242 control data were discarded due to lack of space in the buffer
246 These calls return the number of bytes received, or \-1 if an error occurred.
253 .Bl -tag -width "[EHOSTUNREACH]"
257 is an invalid descriptor.
259 The socket is associated with a connection-oriented protocol
260 and has not been connected (see
267 does not refer to a socket.
269 The socket is marked non-blocking, and the receive operation
271 a receive timeout had been set,
272 and the timeout expired before data were received.
274 The receive was interrupted by delivery of a signal before
275 any data were available.
277 The receive buffer pointer(s) point outside the process's
279 .It Bq Er ECONNREFUSED
280 The socket is associated with a connection-oriented protocol
281 and the connection was forcefully rejected (see
284 The socket is associated with a connection-oriented protocol
285 and the connection was forcefully closed by the peer.
292 may return the following error:
302 may return one of the following errors:
316 was less than 0 or larger than
340 flag is an extension to that specification.
344 function call appeared in