1 .\" Copyright (c) 1983, 1990, 1991 The Regents of the University of California.
2 .\" All rights reserved.
4 .\" SPDX-License-Identifier: BSD-4-Clause-UC
6 .\" $Id: recv.2,v 1.3 1999/05/13 11:33:38 freitag Exp $
8 .\" Modified Sat Jul 24 00:22:20 1993 by Rik Faith <faith@cs.unc.edu>
9 .\" Modified Tue Oct 22 17:45:19 1996 by Eric S. Raymond <esr@thyrsus.com>
10 .\" Modified 1998,1999 by Andi Kleen
11 .\" 2001-06-19 corrected SO_EE_OFFENDER, bug report by James Hawtin
13 .TH recv 2 (date) "Linux man-pages (unreleased)"
15 recv, recvfrom, recvmsg \- receive a message from a socket
18 .RI ( libc ", " \-lc )
21 .B #include <sys/socket.h>
23 .BI "ssize_t recv(int " sockfd ", void " buf [. len "], size_t " len ,
25 .BI "ssize_t recvfrom(int " sockfd ", void " buf "[restrict ." len "], size_t " len ,
27 .BI " struct sockaddr *_Nullable restrict " src_addr ,
28 .BI " socklen_t *_Nullable restrict " addrlen );
29 .BI "ssize_t recvmsg(int " sockfd ", struct msghdr *" msg ", int " flags );
37 calls are used to receive messages from a socket.
39 to receive data on both connectionless and connection-oriented sockets.
40 This page first describes common features of all three system calls,
41 and then describes the differences between the calls.
43 The only difference between
53 is generally equivalent to
56 Also, the following call
60 recv(sockfd, buf, len, flags);
68 recvfrom(sockfd, buf, len, flags, NULL, NULL);
72 All three calls return the length of the message on successful
74 If a message is too long to fit in the supplied buffer, excess
75 bytes may be discarded depending on the type of socket the message is
78 If no messages are available at the socket, the receive calls wait for a
79 message to arrive, unless the socket is nonblocking (see
81 in which case the value \-1 is returned and
84 .BR EAGAIN " or " EWOULDBLOCK .
85 The receive calls normally return any data available, up to the requested
86 amount, rather than waiting for receipt of the full amount requested.
88 An application can use
93 to determine when more data arrives on a socket.
94 .SS The flags argument
97 argument is formed by ORing one or more of the following values:
99 .BR MSG_CMSG_CLOEXEC " (" recvmsg "() only; since Linux 2.6.23)"
100 Set the close-on-exec flag for the file descriptor received
101 via a UNIX domain file descriptor using the
103 operation (described in
105 This flag is useful for the same reasons as the
110 .BR MSG_DONTWAIT " (since Linux 2.2)"
111 Enables nonblocking operation; if the operation would block,
112 the call fails with the error
113 .BR EAGAIN " or " EWOULDBLOCK .
114 This provides similar behavior to setting the
119 operation), but differs in that
121 is a per-call option, whereas
123 is a setting on the open file description (see
125 which will affect all threads in the calling process
126 and as well as other processes that hold file descriptors
127 referring to the same open file description.
129 .BR MSG_ERRQUEUE " (since Linux 2.2)"
131 specifies that queued errors should be received from the socket error queue.
132 The error is passed in
133 an ancillary message with a type dependent on the protocol (for IPv4
135 The user should supply a buffer of sufficient size.
140 for more information.
141 The payload of the original packet that caused the error
142 is passed as normal data via
144 The original destination address of the datagram that caused the error
148 The error is supplied in a
154 #define SO_EE_ORIGIN_NONE 0
155 #define SO_EE_ORIGIN_LOCAL 1
156 #define SO_EE_ORIGIN_ICMP 2
157 #define SO_EE_ORIGIN_ICMP6 3
159 struct sock_extended_err
161 uint32_t ee_errno; /* Error number */
162 uint8_t ee_origin; /* Where the error originated */
163 uint8_t ee_type; /* Type */
164 uint8_t ee_code; /* Code */
165 uint8_t ee_pad; /* Padding */
166 uint32_t ee_info; /* Additional information */
167 uint32_t ee_data; /* Other data */
168 /* More data may follow */
171 struct sockaddr *SO_EE_OFFENDER(struct sock_extended_err *);
178 number of the queued error.
180 is the origin code of where the error originated.
181 The other fields are protocol-specific.
184 returns a pointer to the address of the network object
185 where the error originated from given a pointer to the ancillary message.
186 If this address is not known, the
192 and the other fields of the
195 The payload of the packet that caused the error is passed as normal data.
197 For local errors, no address is passed (this
198 can be checked with the
207 After an error has been passed, the pending socket error
208 is regenerated based on the next queued error and will be passed
209 on the next socket operation.
212 This flag requests receipt of out-of-band data that would not be received
213 in the normal data stream.
214 Some protocols place expedited data
215 at the head of the normal data queue, and thus this flag cannot
216 be used with such protocols.
219 This flag causes the receive operation to
220 return data from the beginning of the
221 receive queue without removing that data from the queue.
223 subsequent receive call will return the same data.
225 .BR MSG_TRUNC " (since Linux 2.2)"
228 Internet datagram (since Linux 2.4.27/2.6.8),
229 netlink (since Linux 2.6.22),
230 and UNIX datagram as well as sequenced-packet
231 .\" commit 9f6f9af7694ede6314bed281eec74d588ba9474f
232 (since Linux 3.4) sockets:
233 return the real length of the packet or datagram,
234 even when it was longer than the passed buffer.
236 For use with Internet stream sockets, see
239 .BR MSG_WAITALL " (since Linux 2.2)"
240 This flag requests that the operation block until the full request is
242 However, the call may still return less data than requested if
243 a signal is caught, an error or disconnect occurs, or the next data to be
244 received is of a different type than that returned.
245 This flag has no effect for datagram sockets.
249 places the received message into the buffer
251 The caller must specify the size of the buffer in
257 and the underlying protocol provides the source address of the message,
258 that source address is placed in the buffer pointed to by
260 .\" (Note: for datagram sockets in both the UNIX and Internet domains,
264 .\" is also filled in for stream sockets in the UNIX domain, but is not
265 .\" filled in for stream sockets in the Internet domain.)
266 .\" [The above notes on AF_UNIX and AF_INET sockets apply as at
267 .\" Kernel 2.4.18. (MTK, 22 Jul 02)]
270 is a value-result argument.
272 it should be initialized to the size of the buffer associated with
276 is updated to contain the actual size of the source address.
277 The returned address is truncated if the buffer provided is too small;
280 will return a value greater than was supplied to the call.
282 If the caller is not interested in the source address,
286 should be specified as NULL.
291 call is normally used only on a
295 It is equivalent to the call:
299 recvfrom(fd, buf, len, flags, NULL, 0);
308 structure to minimize the number of directly supplied arguments.
309 This structure is defined as follows in
315 void *msg_name; /* Optional address */
316 socklen_t msg_namelen; /* Size of address */
317 struct iovec *msg_iov; /* Scatter/gather array */
318 size_t msg_iovlen; /* # elements in msg_iov */
319 void *msg_control; /* Ancillary data, see below */
320 size_t msg_controllen; /* Ancillary data buffer len */
321 int msg_flags; /* Flags on received message */
328 field points to a caller-allocated buffer that is used to
329 return the source address if the socket is unconnected.
330 The caller should set
332 to the size of this buffer before this call;
333 upon return from a successful call,
335 will contain the length of the returned address.
336 If the application does not need to know the source address,
338 can be specified as NULL.
344 describe scatter-gather locations, as discussed in
351 points to a buffer for other protocol control-related messages or
352 miscellaneous ancillary data.
357 should contain the length of the available buffer in
359 upon return from a successful call it will contain the length
360 of the control message sequence.
362 The messages are of the form:
367 size_t cmsg_len; /* Data byte count, including header
368 (type is socklen_t in POSIX) */
369 int cmsg_level; /* Originating protocol */
370 int cmsg_type; /* Protocol\-specific type */
372 unsigned char cmsg_data[]; */
377 Ancillary data should be accessed only by the macros defined in
380 As an example, Linux uses this ancillary data mechanism to pass extended
381 errors, IP options, or file descriptors over UNIX domain sockets.
382 For further information on the use of ancillary data in various
394 It can contain several flags:
397 indicates end-of-record; the data returned completed a record (generally
398 used with sockets of type
399 .BR SOCK_SEQPACKET ).
402 indicates that the trailing portion of a datagram was discarded because the
403 datagram was larger than the buffer supplied.
406 indicates that some control data was discarded due to lack of space in the
407 buffer for ancillary data.
410 is returned to indicate that expedited or out-of-band data was received.
413 indicates that no data was received but an extended error from the socket
416 .BR MSG_CMSG_CLOEXEC " (since Linux 2.6.23)"
417 .\" commit 4a19542e5f694cd408a32c3d9dc593ba9366e2d7
425 These calls return the number of bytes received, or \-1
426 if an error occurred.
427 In the event of an error,
429 is set to indicate the error.
431 When a stream socket peer has performed an orderly shutdown,
432 the return value will be 0 (the traditional "end-of-file" return).
434 Datagram sockets in various domains (e.g., the UNIX and Internet domains)
435 permit zero-length datagrams.
436 When such a datagram is received, the return value is 0.
438 The value 0 may also be returned if the requested number of bytes
439 to receive from a stream socket was 0.
441 These are some standard errors generated by the socket layer.
443 may be generated and returned from the underlying protocol modules;
444 see their manual pages.
446 .BR EAGAIN " or " EWOULDBLOCK
447 .\" Actually EAGAIN on Linux
448 The socket is marked nonblocking and the receive operation
449 would block, or a receive timeout had been set and the timeout expired
450 before data was received.
451 POSIX.1 allows either error to be returned for this case,
452 and does not require these constants to have the same value,
453 so a portable application should check for both possibilities.
458 is an invalid file descriptor.
461 A remote host refused to allow the network connection (typically
462 because it is not running the requested service).
465 The receive buffer pointer(s) point outside the process's
469 The receive was interrupted by delivery of a signal before
470 any data was available; see
474 Invalid argument passed.
475 .\" e.g., msg_namelen < 0 for recvmsg() or addrlen < 0 for recvfrom()
478 Could not allocate memory for
482 The socket is associated with a connection-oriented protocol
483 and has not been connected (see
491 does not refer to a socket.
493 According to POSIX.1,
494 .\" POSIX.1-2001, POSIX.1-2008
499 structure should be typed as
503 field should be typed as
505 but glibc currently types both as
507 .\" glibc bug for msg_controllen raised 12 Mar 2006
508 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=2448
509 .\" The problem is an underlying kernel issue: the size of the
510 .\" __kernel_size_t type used to type these fields varies
511 .\" across architectures, but socklen_t is always 32 bits,
512 .\" as (at least with GCC) is int.
517 4.4BSD (first appeared in 4.2BSD).
519 POSIX.1 describes only the
526 If a zero-length datagram is pending,
532 argument of zero provide different behavior.
533 In this circumstance,
535 has no effect (the datagram remains pending), while
537 consumes the pending datagram.
541 for information about a Linux-specific system call
542 that can be used to receive multiple datagrams in a single call.
544 An example of the use of