1RECV(P) POSIX Programmer's Manual RECV(P)
2
3
4
6 recv - receive a message from a connected socket
7
9 #include <sys/socket.h>
10
11 ssize_t recv(int socket, void *buffer, size_t length, int flags);
12
13
15 The recv() function shall receive a message from a connection-mode or
16 connectionless-mode socket. It is normally used with connected sockets
17 because it does not permit the application to retrieve the source
18 address of received data.
19
20 The recv() function takes the following arguments:
21
22 socket Specifies the socket file descriptor.
23
24 buffer Points to a buffer where the message should be stored.
25
26 length Specifies the length in bytes of the buffer pointed to by the
27 buffer argument.
28
29 flags Specifies the type of message reception. Values of this argument
30 are formed by logically OR'ing zero or more of the following
31 values:
32
33 MSG_PEEK
34 Peeks at an incoming message. The data is treated as unread and
35 the next recv() or similar function shall still return this
36 data.
37
38 MSG_OOB
39 Requests out-of-band data. The significance and semantics of
40 out-of-band data are protocol-specific.
41
42 MSG_WAITALL
43 On SOCK_STREAM sockets this requests that the function block
44 until the full amount of data can be returned. The function may
45 return the smaller amount of data if the socket is a message-
46 based socket, if a signal is caught, if the connection is termi‐
47 nated, if MSG_PEEK was specified, or if an error is pending for
48 the socket.
49
50
51
52 The recv() function shall return the length of the message written to
53 the buffer pointed to by the buffer argument. For message-based sock‐
54 ets, such as SOCK_DGRAM and SOCK_SEQPACKET, the entire message shall be
55 read in a single operation. If a message is too long to fit in the
56 supplied buffer, and MSG_PEEK is not set in the flags argument, the
57 excess bytes shall be discarded. For stream-based sockets, such as
58 SOCK_STREAM, message boundaries shall be ignored. In this case, data
59 shall be returned to the user as soon as it becomes available, and no
60 data shall be discarded.
61
62 If the MSG_WAITALL flag is not set, data shall be returned only up to
63 the end of the first message.
64
65 If no messages are available at the socket and O_NONBLOCK is not set on
66 the socket's file descriptor, recv() shall block until a message
67 arrives. If no messages are available at the socket and O_NONBLOCK is
68 set on the socket's file descriptor, recv() shall fail and set errno to
69 [EAGAIN] or [EWOULDBLOCK].
70
72 Upon successful completion, recv() shall return the length of the mes‐
73 sage in bytes. If no messages are available to be received and the peer
74 has performed an orderly shutdown, recv() shall return 0. Otherwise, -1
75 shall be returned and errno set to indicate the error.
76
78 The recv() function shall fail if:
79
80 EAGAIN or EWOULDBLOCK
81
82 The socket's file descriptor is marked O_NONBLOCK and no data is
83 waiting to be received; or MSG_OOB is set and no out-of-band
84 data is available and either the socket's file descriptor is
85 marked O_NONBLOCK or the socket does not support blocking to
86 await out-of-band data.
87
88 EBADF The socket argument is not a valid file descriptor.
89
90 ECONNRESET
91 A connection was forcibly closed by a peer.
92
93 EINTR The recv() function was interrupted by a signal that was caught,
94 before any data was available.
95
96 EINVAL The MSG_OOB flag is set and no out-of-band data is available.
97
98 ENOTCONN
99 A receive is attempted on a connection-mode socket that is not
100 connected.
101
102 ENOTSOCK
103 The socket argument does not refer to a socket.
104
105 EOPNOTSUPP
106 The specified flags are not supported for this socket type or
107 protocol.
108
109 ETIMEDOUT
110 The connection timed out during connection establishment, or due
111 to a transmission timeout on active connection.
112
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
126 The following sections are informative.
127
129 None.
130
132 The recv() function is equivalent to recvfrom() with a zero address_len
133 argument, and to read() if no flags are used.
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() , read() , recvmsg() , recvfrom() , select() , send() ,
146 sendmsg() , sendto() , shutdown() , socket() , write() , the Base Defi‐
147 nitions volume of IEEE Std 1003.1-2001, <sys/socket.h>
148
150 Portions of this text are reprinted and reproduced in electronic form
151 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
152 -- Portable Operating System Interface (POSIX), The Open Group Base
153 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
154 Electrical and Electronics Engineers, Inc and The Open Group. In the
155 event of any discrepancy between this version and the original IEEE and
156 The Open Group Standard, the original IEEE and The Open Group Standard
157 is the referee document. The original Standard can be obtained online
158 at http://www.opengroup.org/unix/online.html .
159
160
161
162IEEE/The Open Group 2003 RECV(P)