1.\" Copyright (c) 1983, 1990, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.Dd May 17, 2025 29.Dt RECV 2 30.Os 31.Sh NAME 32.Nm recv , 33.Nm recvfrom , 34.Nm recvmsg , 35.Nm recvmmsg 36.Nd receive message(s) from a socket 37.Sh LIBRARY 38.Lb libc 39.Sh SYNOPSIS 40.In sys/socket.h 41.Ft ssize_t 42.Fn recv "int s" "void *buf" "size_t len" "int flags" 43.Ft ssize_t 44.Fn recvfrom "int s" "void *buf" "size_t len" "int flags" "struct sockaddr * restrict from" "socklen_t * restrict fromlen" 45.Ft ssize_t 46.Fn recvmsg "int s" "struct msghdr *msg" "int flags" 47.Ft ssize_t 48.Fn recvmmsg "int s" "struct mmsghdr * restrict msgvec" "size_t vlen" "int flags" "const struct timespec * restrict timeout" 49.Sh DESCRIPTION 50The 51.Fn recvfrom , 52.Fn recvmsg , 53and 54.Fn recvmmsg 55system calls 56are used to receive messages from a socket, 57and may be used to receive data on a socket whether or not 58it is connection-oriented. 59.Pp 60If 61.Fa from 62is not a null pointer 63and the socket is not connection-oriented, 64the source address of the message is filled in. 65The 66.Fa fromlen 67argument 68is a value-result argument, initialized to the size of 69the buffer associated with 70.Fa from , 71and modified on return to indicate the actual size of the 72address stored there. 73.Pp 74The 75.Fn recv 76function is normally used only on a 77.Em connected 78socket (see 79.Xr connect 2 ) 80and is identical to 81.Fn recvfrom 82with a 83null pointer passed as its 84.Fa from 85argument. 86.Pp 87The 88.Fn recvmmsg 89function is used to receive multiple 90messages at a call. 91Their number is supplied by 92.Fa vlen . 93The messages are placed in the buffers described by 94.Fa msgvec 95vector, after reception. 96The size of each received message is placed in the 97.Fa msg_len 98field of each element of the vector. 99If 100.Fa timeout 101is NULL the call blocks until the data is available for each 102supplied message buffer. 103Otherwise it waits for data for the specified amount of time. 104If the timeout expired and there is no data received, 105a value 0 is returned. 106The 107.Xr ppoll 2 108system call is used to implement the timeout mechanism, 109before first receive is performed. 110.Pp 111The 112.Fn recv , 113.Fn recvfrom 114and 115.Fn recvmsg 116return the length of the message on successful 117completion, whereas 118.Fn recvmmsg 119returns the number of received messages. 120If a message is too long to fit in the supplied buffer, 121excess bytes may be discarded depending on the type of socket 122the message is received from (see 123.Xr socket 2 ) . 124.Pp 125If no messages are available at the socket, the 126receive call waits for a message to arrive, unless 127the socket is non-blocking (see 128.Xr fcntl 2 ) 129in which case the value 130\-1 is returned and the global variable 131.Va errno 132is set to 133.Er EAGAIN . 134The receive calls except 135.Fn recvmmsg 136normally return any data available, 137up to the requested amount, 138rather than waiting for receipt of the full amount requested; 139this behavior is affected by the socket-level options 140.Dv SO_RCVLOWAT 141and 142.Dv SO_RCVTIMEO 143described in 144.Xr getsockopt 2 . 145The 146.Fn recvmmsg 147function implements this behaviour for each message in the vector. 148.Pp 149The 150.Xr select 2 151system call may be used to determine when more data arrives. 152.Pp 153The 154.Fa flags 155argument to a 156.Fn recv 157function is formed by 158.Em or Ap ing 159one or more of the values: 160.Bl -column ".Dv MSG_CMSG_CLOEXEC" -offset indent 161.It Dv MSG_OOB Ta process out-of-band data 162.It Dv MSG_PEEK Ta peek at incoming message 163.It Dv MSG_TRUNC Ta return real packet or datagram length 164.It Dv MSG_WAITALL Ta wait for full request or error 165.It Dv MSG_DONTWAIT Ta do not block 166.It Dv MSG_CMSG_CLOEXEC Ta set received fds close-on-exec 167.It Dv MSG_CMSG_CLOFORK Ta set received fds close-on-fork 168.It Dv MSG_WAITFORONE Ta do not block after receiving the first message 169(only for 170.Fn recvmmsg 171) 172.El 173.Pp 174The 175.Dv MSG_OOB 176flag requests receipt of out-of-band data 177that would not be received in the normal data stream. 178Some protocols place expedited data at the head of the normal 179data queue, and thus this flag cannot be used with such protocols. 180The 181.Dv MSG_PEEK 182flag causes the receive operation to return data 183from the beginning of the receive queue without removing that 184data from the queue. 185Thus, a subsequent receive call will return the same data. 186The 187.Dv MSG_TRUNC 188flag causes the receive operation to return the full length of the packet 189or datagram even if larger than provided buffer. The flag is supported 190on SOCK_DGRAM sockets for 191.Dv AF_INET 192, 193.Dv AF_INET6 194and 195.Dv AF_UNIX 196families. 197The 198.Dv MSG_WAITALL 199flag requests that the operation block until 200the full request is satisfied. 201However, the call may still return less data than requested 202if a signal is caught, an error or disconnect occurs, 203or the next data to be received is of a different type than that returned. 204The 205.Dv MSG_DONTWAIT 206flag requests the call to return when it would block otherwise. 207If no data is available, 208.Va errno 209is set to 210.Er EAGAIN . 211This flag is not available in 212.St -ansiC 213or 214.St -isoC-99 215compilation mode. 216The 217.Dv MSG_WAITFORONE 218flag sets MSG_DONTWAIT after the first message has been received. 219This flag is only relevant for 220.Fn recvmmsg . 221.Pp 222The 223.Fn recvmsg 224system call uses a 225.Fa msghdr 226structure to minimize the number of directly supplied arguments. 227This structure has the following form, as defined in 228.In sys/socket.h : 229.Bd -literal 230struct msghdr { 231 void *msg_name; /* optional address */ 232 socklen_t msg_namelen; /* size of address */ 233 struct iovec *msg_iov; /* scatter/gather array */ 234 int msg_iovlen; /* # elements in msg_iov */ 235 void *msg_control; /* ancillary data, see below */ 236 socklen_t msg_controllen;/* ancillary data buffer len */ 237 int msg_flags; /* flags on received message */ 238}; 239.Ed 240.Pp 241Here 242.Fa msg_name 243and 244.Fa msg_namelen 245specify the source address if the socket is unconnected; 246.Fa msg_name 247may be given as a null pointer if no names are desired or required. 248The 249.Fa msg_iov 250and 251.Fa msg_iovlen 252arguments 253describe scatter gather locations, as discussed in 254.Xr read 2 . 255The 256.Fa msg_control 257argument, 258which has length 259.Fa msg_controllen , 260points to a buffer for other protocol control related messages 261or other miscellaneous ancillary data. 262The messages are of the form: 263.Bd -literal 264struct cmsghdr { 265 socklen_t cmsg_len; /* data byte count, including hdr */ 266 int cmsg_level; /* originating protocol */ 267 int cmsg_type; /* protocol-specific type */ 268/* followed by 269 u_char cmsg_data[]; */ 270}; 271.Ed 272.Pp 273As an example, the SO_TIMESTAMP socket option returns a reception 274timestamp for UDP packets. 275.Pp 276With 277.Dv AF_UNIX 278domain sockets, ancillary data can be used to pass file descriptors and 279process credentials. 280See 281.Xr unix 4 282for details. 283.Pp 284The 285.Fa msg_flags 286field is set on return according to the message received. 287.Dv MSG_EOR 288indicates end-of-record; 289the data returned completed a record (generally used with sockets of type 290.Dv SOCK_SEQPACKET ) . 291.Dv MSG_TRUNC 292indicates that 293the trailing portion of a datagram was discarded because the datagram 294was larger than the buffer supplied. 295.Dv MSG_CTRUNC 296indicates that some 297control data were discarded due to lack of space in the buffer 298for ancillary data. 299.Dv MSG_OOB 300is returned to indicate that expedited or out-of-band data were received. 301.Pp 302The 303.Fn recvmmsg 304system call uses the 305.Fa mmsghdr 306structure, defined as follows in the 307.In sys/socket.h 308header: 309.Bd -literal 310struct mmsghdr { 311 struct msghdr msg_hdr; /* message header */ 312 ssize_t msg_len; /* message length */ 313}; 314.Ed 315.Pp 316On data reception the 317.Fa msg_len 318field is updated to the length of the received message. 319.Sh RETURN VALUES 320On successful completion, the 321.Fn recv , 322.Fn recvfrom , 323and 324.Fn recvmsg 325functions return the number of bytes received, while the 326.Fn recvmmsg 327function returns the number of messages received. 328If no messages are available to be received and the peer has 329performed an orderly shutdown, 0 is returned. 330Otherwise, -1 is returned and 331.Va errno 332is set to indicate the error. 333.Sh ERRORS 334The calls fail if: 335.Bl -tag -width Er 336.It Bq Er EBADF 337The argument 338.Fa s 339is an invalid descriptor. 340.It Bq Er ECONNRESET 341The remote socket end is forcibly closed. 342.It Bq Er ENOTCONN 343The socket is associated with a connection-oriented protocol 344and has not been connected (see 345.Xr connect 2 346and 347.Xr accept 2 ) . 348.It Bq Er ENOTSOCK 349The argument 350.Fa s 351does not refer to a socket. 352.It Bq Er EMFILE 353The 354.Fn recvmsg 355system call 356was used to receive rights (file descriptors) that were in flight on the 357connection. 358However, the receiving program did not have enough free file 359descriptor slots to accept them. 360In this case the descriptors are closed, with pending data either discarded 361in the case of the unreliable datagram protocol or preserved in the case of a 362reliable protocol. 363The pending data can be retrieved with another call to 364.Fn recvmsg . 365.It Bq Er EMSGSIZE 366The 367.Fa msg_iovlen 368member of the 369.Fa msghdr 370structure pointed to by 371.Fa msg 372is less than or equal to 0, or is greater than 373.Va IOV_MAX . 374.It Bq Er EAGAIN 375The socket is marked non-blocking and the receive operation 376would block, or 377a receive timeout had been set 378and the timeout expired before data were received. 379.It Bq Er EINTR 380The receive was interrupted by delivery of a signal before 381any data were available. 382.It Bq Er EFAULT 383The receive buffer pointer(s) point outside the process's 384address space. 385.El 386.Sh SEE ALSO 387.Xr fcntl 2 , 388.Xr getsockopt 2 , 389.Xr read 2 , 390.Xr select 2 , 391.Xr socket 2 , 392.Xr CMSG_DATA 3 , 393.Xr unix 4 394.Sh HISTORY 395The 396.Fn recv 397function appeared in 398.Bx 4.2 . 399The 400.Fn recvmmsg 401function appeared in 402.Fx 11.0 . 403