'\" te
.\"  Copyright 1989 AT&T
.\" Copyright (C) 2006, Sun Microsystems, Inc. All Rights Reserved
.\" Copyright (c) 2018, Joyent, Inc.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH RECV 3SOCKET "September 10, 2018"
.SH NAME
recv, recvfrom, recvmsg \- receive a message from a socket
.SH SYNOPSIS
.LP
.nf
\fBcc\fR [ \fIflag\fR... ] \fIfile\fR... \fB-lsocket\fR \fB -lnsl \fR [ \fIlibrary\fR... ]
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>

\fBssize_t\fR \fBrecv\fR(\fBint\fR \fIs\fR, \fBvoid *\fR\fIbuf\fR, \fBsize_t\fR \fIlen\fR, \fBint\fR \fIflags\fR);
.fi

.LP
.nf
\fBssize_t\fR \fBrecvfrom\fR(\fBint\fR \fIs\fR, \fBvoid *\fR\fIbuf\fR, \fBsize_t\fR \fIlen\fR, \fBint\fR \fIflags\fR,
     \fBstruct sockaddr *\fR\fIfrom\fR, \fBsocklen_t *\fR\fIfromlen\fR);
.fi

.LP
.nf
\fBssize_t\fR \fBrecvmsg\fR(\fBint\fR \fIs\fR, \fBstruct msghdr *\fR\fImsg\fR, \fBint\fR \fIflags\fR);
.fi

.SH DESCRIPTION
.LP
The \fBrecv()\fR, \fBrecvfrom()\fR, and \fBrecvmsg()\fR functions are used to
receive messages from another socket. The \fIs\fR socket is created with
\fBsocket\fR(3SOCKET).
.sp
.LP
If \fIfrom\fR is a non-\fBNULL\fR pointer, the source address of the message is
filled in. The value-result parameter \fIfromlen\fR is initialized to the size
of the buffer associated with \fIfrom\fR and modified on return to indicate the
actual size of the address stored in the buffer. The length of the message is
returned. If a message is too long to fit in the supplied buffer, excess bytes
may be discarded depending on the type of socket from which the message is
received. See \fBsocket\fR(3SOCKET).
.sp
.LP
If no messages are available at the socket, the receive call waits for a
message to arrive. If the socket is non-blocking, \fB-1\fR is returned with the
external variable \fBerrno\fR set to \fBEWOULDBLOCK\fR. See \fBfcntl\fR(2).
.sp
.LP
For processes on the same host, \fBrecvmsg()\fR can be used to receive a file
descriptor from another process, but it cannot receive ancillary data. See
\fBlibxnet\fR(3LIB).
.sp
.LP
If a zero-length buffer is specified for a message, an EOF condition results
that is indistinguishable from the successful transfer of a file descriptor.
For that reason, one or more bytes of data should be provided when
\fBrecvmsg()\fR passes a file descriptor.
.sp
.LP
The \fBpoll\fR(2), \fBselect\fR(3C), and \fBport_get\fR(3C) functions can be
used to determine when more data arrives.
.sp
.LP
The \fIflags\fR parameter is formed by an \fBOR\fR operation on one or more of
the following:
.sp
.ne 2
.na
\fB\fBMSG_OOB\fR\fR
.ad
.RS 16n
Read any \fBout-of-band\fR data present on the socket rather than the regular
\fBin-band\fR data.
.RE

.sp
.ne 2
.na
\fB\fBMSG_PEEK\fR\fR
.ad
.RS 16n
Peek at the data present on the socket. The data is returned, but not consumed
to allow a subsequent receive operation to see the same data.
.RE

.sp
.ne 2
.na
\fB\fBMSG_WAITALL\fR\fR
.ad
.RS 16n
Messages are blocked until the full amount of data requested is returned. The
\fBrecv()\fR function can return a smaller amount of data if a signal is
caught, the connection is terminated, \fBMSG_PEEK\fR is specified, or if an
error is pending for the socket.
.RE

.sp
.ne 2
.na
\fB\fBMSG_DONTWAIT\fR\fR
.ad
.RS 16n
Pending messages received on the connection are returned. If data is
unavailable, the function does not block. This behavior is the equivalent to
specifying \fBO_NONBLOCK\fR on the file descriptor of a socket, except that
write requests are unaffected.
.RE

.sp
.LP
The \fBrecvmsg()\fR function call uses a \fBmsghdr\fR structure defined in
<\fBsys/socket.h\fR> to minimize the number of directly supplied parameters.
.SH RETURN VALUES
.LP
Upon successful completion, these functions return the number of bytes
received. Otherwise, they return \fB-1\fR and set \fBerrno\fR to indicate the
error.
.SH ERRORS
.LP
In addition to the errors documented below, an asynchronous error generated by
the underlying socket protocol may be returned. For the full list of errors,
please see the corresponding socket protocol manual page. For example, for a
list of TCP errors, please see \fBtcp\fR(4P).
.sp
.LP
The \fBrecv()\fR, \fBrecvfrom()\fR, and \fBrecvmsg()\fR functions return errors
under the following conditions:
.sp
.ne 2
.na
\fB\fBEBADF\fR\fR
.ad
.RS 16n
The \fIs\fR file descriptor is invalid.
.RE

.sp
.ne 2
.na
\fB\fBECONNRESET\fR\fR
.ad
.RS 16n
The \fIs\fR argument refers to a connection oriented socket and the connection
was forcibly closed by the peer and is no longer valid. I/O can no longer be
performed to \fIfiledes\fR.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 16n
The \fBMSG_OOB\fR flag is set and no out-of-band data is available.
.RE

.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 16n
The operation is interrupted by the delivery of a signal before any data is
available to be received.
.RE

.sp
.ne 2
.na
\fB\fBEIO\fR\fR
.ad
.RS 16n
An I/O error occurs while reading from or writing to the file system.
.RE

.sp
.ne 2
.na
\fB\fBENOMEM\fR\fR
.ad
.RS 16n
Insufficient user memory is available to complete operation.
.RE

.sp
.ne 2
.na
\fB\fBENOSR\fR\fR
.ad
.RS 16n
Insufficient \fBSTREAMS\fR resources are available for the operation to
complete.
.RE

.sp
.ne 2
.na
\fB\fBENOTSOCK\fR\fR
.ad
.RS 16n
\fIs\fR is not a socket.
.RE

.sp
.ne 2
.na
\fB\fBESTALE\fR\fR
.ad
.RS 16n
A stale NFS file handle exists.
.RE

.sp
.ne 2
.na
\fB\fBEWOULDBLOCK\fR\fR
.ad
.RS 16n
The socket is marked non-blocking and the requested operation would block.
.RE

.sp
.ne 2
.na
\fB\fBECONNREFUSED\fR\fR
.ad
.RS 16n
The requested connection was refused by the peer. For connected IPv4 and IPv6
datagram sockets, this indicates that the system received an \fBICMP
Destination Port Unreachable\fR message from the peer.
.RE

.sp
.LP
The \fBrecv()\fR and \fBrecvfrom()\fR functions fail under the following
conditions:
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
The \fIlen\fR argument overflows a \fBssize_t\fR.
.RE

.sp
.LP
The \fBrecvmsg()\fR function returns errors under the following conditions:
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
The \fBmsg_iovlen\fR member of the \fBmsghdr\fR structure pointed to by
\fImsg\fR is less than or equal to \fB0\fR, or greater than \fB[IOV_MAX}\fR.
See \fBIntro\fR(2) for a definition of \fB[IOV_MAX}\fR.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
One of the \fIiov_len\fR values in the \fBmsg_iov\fR array member of the
\fBmsghdr\fR structure pointed to by \fImsg\fR is negative, or the sum of the
\fIiov_len\fR values in the \fBmsg_iov\fR array overflows a \fBssize_t\fR.
.RE

.SH ATTRIBUTES
.LP
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Committed
_
MT-Level	Safe
.TE

.SH SEE ALSO
.LP
\fBfcntl\fR(2), \fBioctl\fR(2), \fBpoll\fR(2), \fBread\fR(2),
\fBconnect\fR(3SOCKET), \fBgetsockopt\fR(3SOCKET), \fBlibxnet\fR(3LIB),
\fBport_get(3C)\fR, \fBselect\fR(3C),
.BR socket.h (3HEAD),
.BR send (3SOCKET),
.BR sockaddr (3SOCKET),
.BR socket (3SOCKET),
.BR tcp (4P),
.BR attributes (7)