1RECV(3P) POSIX Programmer's Manual RECV(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 recv — receive a message from a connected socket
13
15 #include <sys/socket.h>
16
17 ssize_t recv(int socket, void *buffer, size_t length, int flags);
18
20 The recv() function shall receive a message from a connection-mode or
21 connectionless-mode socket. It is normally used with connected sockets
22 because it does not permit the application to retrieve the source
23 address of received data.
24
25 The recv() function takes the following arguments:
26
27 socket Specifies the socket file descriptor.
28
29 buffer Points to a buffer where the message should be stored.
30
31 length Specifies the length in bytes of the buffer pointed to by the
32 buffer argument.
33
34 flags Specifies the type of message reception. Values of this argu‐
35 ment are formed by logically OR'ing zero or more of the fol‐
36 lowing values:
37
38 MSG_PEEK Peeks at an incoming message. The data is treated
39 as unread and the next recv() or similar function
40 shall still return this data.
41
42 MSG_OOB Requests out-of-band data. The significance and
43 semantics of out-of-band data are protocol-spe‐
44 cific.
45
46 MSG_WAITALL On SOCK_STREAM sockets this requests that the
47 function block until the full amount of data can
48 be returned. The function may return the smaller
49 amount of data if the socket is a message-based
50 socket, if a signal is caught, if the connection
51 is terminated, if MSG_PEEK was specified, or if
52 an error is pending for the socket.
53
54 The recv() function shall return the length of the message written to
55 the buffer pointed to by the buffer argument. For message-based sock‐
56 ets, such as SOCK_DGRAM and SOCK_SEQPACKET, the entire message shall be
57 read in a single operation. If a message is too long to fit in the
58 supplied buffer, and MSG_PEEK is not set in the flags argument, the
59 excess bytes shall be discarded. For stream-based sockets, such as
60 SOCK_STREAM, message boundaries shall be ignored. In this case, data
61 shall be returned to the user as soon as it becomes available, and no
62 data shall be discarded.
63
64 If the MSG_WAITALL flag is not set, data shall be returned only up to
65 the end of the first message.
66
67 If no messages are available at the socket and O_NONBLOCK is not set on
68 the socket's file descriptor, recv() shall block until a message
69 arrives. If no messages are available at the socket and O_NONBLOCK is
70 set on the socket's file descriptor, recv() shall fail and set errno to
71 [EAGAIN] or [EWOULDBLOCK].
72
74 Upon successful completion, recv() shall return the length of the mes‐
75 sage in bytes. If no messages are available to be received and the peer
76 has performed an orderly shutdown, recv() shall return 0. Otherwise, -1
77 shall be returned and errno set to indicate the error.
78
80 The recv() function shall fail if:
81
82 EAGAIN or EWOULDBLOCK
83 The socket's file descriptor is marked O_NONBLOCK and no data is
84 waiting to be received; or MSG_OOB is set and no out-of-band
85 data is available and either the socket's file descriptor is
86 marked O_NONBLOCK or the socket does not support blocking to
87 await out-of-band data.
88
89 EBADF The socket argument is not a valid file descriptor.
90
91 ECONNRESET
92 A connection was forcibly closed by a peer.
93
94 EINTR The recv() function was interrupted by a signal that was caught,
95 before any data was available.
96
97 EINVAL The MSG_OOB flag is set and no out-of-band data is available.
98
99 ENOTCONN
100 A receive is attempted on a connection-mode socket that is not
101 connected.
102
103 ENOTSOCK
104 The socket argument does not refer to a socket.
105
106 EOPNOTSUPP
107 The specified flags are not supported for this socket type or
108 protocol.
109
110 ETIMEDOUT
111 The connection timed out during connection establishment, or due
112 to a transmission timeout on active connection.
113
114 The recv() function may fail if:
115
116 EIO An I/O error occurred while reading from or writing to the file
117 system.
118
119 ENOBUFS
120 Insufficient resources were available in the system to perform
121 the operation.
122
123 ENOMEM Insufficient memory was available to fulfill the request.
124
125 The following sections are informative.
126
128 None.
129
131 The recv() function is equivalent to recvfrom() with null pointer
132 address and address_len arguments, and to read() if the socket argument
133 refers to a socket and the flags argument is 0.
134
135 The select() and poll() functions can be used to determine when data is
136 available to be received.
137
139 None.
140
142 None.
143
145 poll(), pselect(), read(), recvmsg(), recvfrom(), send(), sendmsg(),
146 sendto(), shutdown(), socket(), write()
147
148 The Base Definitions volume of POSIX.1‐2017, <sys_socket.h>
149
151 Portions of this text are reprinted and reproduced in electronic form
152 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
153 table Operating System Interface (POSIX), The Open Group Base Specifi‐
154 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
155 Electrical and Electronics Engineers, Inc and The Open Group. In the
156 event of any discrepancy between this version and the original IEEE and
157 The Open Group Standard, the original IEEE and The Open Group Standard
158 is the referee document. The original Standard can be obtained online
159 at http://www.opengroup.org/unix/online.html .
160
161 Any typographical or formatting errors that appear in this page are
162 most likely to have been introduced during the conversion of the source
163 files to man page format. To report such errors, see https://www.ker‐
164 nel.org/doc/man-pages/reporting_bugs.html .
165
166
167
168IEEE/The Open Group 2017 RECV(3P)