1recvmsg(3XNET) X/Open Networking Services Library Functions recvmsg(3XNET)
2
3
4
6 recvmsg - receive a message from a socket
7
9 cc [ flag ... ] file ... -lxnet [ library ... ]
10 #include <sys/socket.h>
11
12 ssize_t recvmsg(int socket, struct msghdr *message, int flags);
13
14
16 The recvmsg() function receives a message from a connection-mode or
17 connectionless-mode socket. It is normally used with connectionless-
18 mode sockets because it permits the application to retrieve the source
19 address of received data.
20
21
22 The recvmsg() function receives messages from unconnected or connected
23 sockets and returns the length of the message.
24
25
26 The recvmsg() function returns the total length of the message. For
27 message-based sockets such as SOCK_DGRAM and SOCK_SEQPACKET, the entire
28 message must be read in a single operation. If a message is too long to
29 fit in the supplied buffers, and MSG_PEEK is not set in the flags argu‐
30 ment, the excess bytes are discarded, and MSG_TRUNC is set in the
31 msg_flags member of the msghdr structure. For stream-based sockets such
32 as SOCK_STREAM, message boundaries are ignored. In this case, data is
33 returned to the user as soon as it becomes available, and no data is
34 discarded.
35
36
37 If the MSG_WAITALL flag is not set, data will be returned only up to
38 the end of the first message.
39
40
41 If no messages are available at the socket, and O_NONBLOCK is not set
42 on the socket's file descriptor, recvmsg() blocks until a message
43 arrives. If no messages are available at the socket and O_NONBLOCK is
44 set on the socket's file descriptor, the recvmsg() function fails and
45 sets errno to EAGAIN or EWOULDBLOCK.
46
47
48 In the msghdr structure, defined in socket.h(3HEAD), the msg_name and
49 msg_namelen members specify the source address if the socket is uncon‐
50 nected. If the socket is connected, the msg_name and msg_namelen mem‐
51 bers are ignored. The msg_name member may be a null pointer if no names
52 are desired or required.
53
54
55 The msg_control and msg_controllen members specify a buffer to receive
56 ancillary data sent along with a message. Ancillary data consists of a
57 sequence of pairs. Each pair is composed of a cmsghdr structure fol‐
58 lowed by a data array. The cmsghdr structure, defined in
59 socket.h(3HEAD), contains descriptive information which allows an
60 application to correctly parse data. The data array contains the ancil‐
61 lary data message.
62
63
64 If ancillary data is not transferred, msg_control is set to NULL and
65 msg_controllen is set to 0.
66
67
68 The msg_iov and msg_iovlen fields of the msghdr structure are used to
69 specify where the received data will be stored. msg_iov points to an
70 array of iovec structures. The msg_iovlen must be set to the dimension
71 of this array. In each iovec structure, the iov_base field specifies a
72 storage area and the iov_len field gives its size in bytes. Each stor‐
73 age area indicated by msg_iov is filled with received data in turn
74 until all of the received data is stored or all of the areas have been
75 filled.
76
77
78 If the SO_TIMESTAMP option has been enabled through setsockopt(), then
79 a struct timeval is returned following the cmsghdr, and the cmsg_len
80 field of the cmsghdr indicates the size of the struct timeval.
81
82
83 On successful completion, the msg_flags member of the message header is
84 the bitwise-inclusive OR of all of the following flags that indicate
85 conditions detected for the received message:
86
87 MSG_EOR End of record was received (if supported by the proto‐
88 col).
89
90
91 MSG_OOB Out-of-band data was received.
92
93
94 MSG_TRUNC Normal data was truncated.
95
96
97 MSG_CTRUNC Control data was truncated.
98
99
101 The function takes the following arguments:
102
103 socket Specifies the socket file descriptor.
104
105
106 message Points to a msghdr structure, containing both the buffer to
107 store the source address and the buffers for the incoming
108 message. The length and format of the address depend on the
109 address family of the socket. The msg_flags member is
110 ignored on input, but may contain meaningful values on out‐
111 put.
112
113
114 flags Specifies the type of message reception. Values of this
115 argument are formed by logically OR'ing zero or more of the
116 following values:
117
118 MSG_OOB Requests out-of-band data. The significance
119 and semantics of out-of-band data are proto‐
120 col-specific.
121
122
123 MSG_PEEK Peeks at the incoming message.
124
125
126 MSG_WAITALL Requests that the function block until the
127 full amount of data requested can be
128 returned. The function may return a smaller
129 amount of data if a signal is caught, if the
130 connection is terminated, if MSG_PEEK was
131 specified, or if an error is pending for the
132 socket.
133
134
135
137 The select(3C) and poll(2) functions can be used to determine when data
138 is available to be received.
139
141 Upon successful completion, recvmsg() returns the length of the message
142 in bytes. If no messages are available to be received and the peer has
143 performed an orderly shutdown, recvmsg() returns 0. Otherwise, −1 is
144 returned and errno is set to indicate the error.
145
147 The recvmsg() function will fail if:
148
149 EAGAIN The socket's file descriptor is marked O_NONBLOCK and no
150 EWOULDBLOCK data is waiting to be received; or MSG_OOB is set and no
151 out-of-band data is available and either the socket's
152 file descriptor is marked O_NONBLOCK or the socket does
153 not support blocking to await out-of-band data.
154
155
156 EBADF The socket argument is not a valid open file descriptor.
157
158
159 ECONNRESET A connection was forcibly closed by a peer.
160
161
162 EFAULT The message parameter, or storage pointed to by the
163 msg_name, msg_control or msg_iov fields of the message
164 parameter, or storage pointed to by the iovec structures
165 pointed to by the msg_iov field can not be accessed or
166 written.
167
168
169 EINTR This function was interrupted by a signal before any
170 data was available.
171
172
173 EINVAL The sum of the iov_len values overflows an ssize_t. or
174 the MSG_OOB flag is set and no out-of-band data is
175 available.
176
177
178 EMSGSIZE The msg_iovlen member of the msghdr structure pointed to
179 by message is less than or equal to 0, or is greater
180 than IOV_MAX.
181
182
183 ENOTCONN A receive is attempted on a connection-mode socket that
184 is not connected.
185
186
187 ENOTSOCK The socket argument does not refer to a socket.
188
189
190 EOPNOTSUPP The specified flags are not supported for this socket
191 type.
192
193
194 ETIMEDOUT The connection timed out during connection establish‐
195 ment, or due to a transmission timeout on active connec‐
196 tion.
197
198
199
200 The recvmsg() function may fail if:
201
202 EIO An IO error occurred while reading from or writing to the
203 file system.
204
205
206 ENOBUFS Insufficient resources were available in the system to per‐
207 form the operation.
208
209
210 ENOMEM Insufficient memory was available to fulfill the request.
211
212
213 ENOSR There were insufficient STREAMS resources available for the
214 operation to complete.
215
216
218 See attributes(5) for descriptions of the following attributes:
219
220
221
222
223 ┌─────────────────────────────┬─────────────────────────────┐
224 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
225 ├─────────────────────────────┼─────────────────────────────┤
226 │Interface Stability │Standard │
227 ├─────────────────────────────┼─────────────────────────────┤
228 │MT-Level │MT-Safe │
229 └─────────────────────────────┴─────────────────────────────┘
230
232 poll(2), recv(3XNET), recvfrom(3XNET), select(3C), send(3XNET),
233 sendmsg(3XNET), sendto(3XNET), setsockopt(3XNET), shutdown(3XNET),
234 socket(3XNET), socket.h(3HEAD), attributes(5), standards(5)
235
236
237
238SunOS 5.11 27 Feb 2006 recvmsg(3XNET)