RECV(2)RECV(2)NAME
recv, recvfrom, recvmsg - receive a message from a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
int recv(int s, void *buf, int len, int flags);
int recvfrom(int s, void *buf, int len, int flags,
struct sockaddr *from, socklen_t *fromlen);
int recvmsg(int s, struct msghdr *msg, int flags);
#if _XOPEN_SOURCE >= 500
ssize_t recv(int s, void *buf, size_t len, int flags);
ssize_t recvfrom(int s, void *buf, size_t len, int flags,
struct sockaddr *from, socklen_t *fromlen);
ssize_t recvmsg(int s, struct msghdr *msg, int flags);
#elif _XOPEN_SOURCE < 500
ssize_t recv(int s, void *buf, size_t len, int flags);
ssize_t recvfrom(int s, void *buf, size_t len, int flags,
struct sockaddr *from, size_t *fromlen);
ssize_t recvmsg(int s, struct msghdr *msg, int flags);
#endif
DESCRIPTION
Recv, recvfrom, and recvmsg are used to receive messages from a socket.
The recv call is normally used only on a connected socket (see
connect(2)), while recvfrom and recvmsg may be used to receive data on a
socket whether it is in a connected state or not.
If from is non-zero, the source address of the message is filled in.
Fromlen is a value-result parameter, initialized to the size of the
buffer associated with from, and modified on return to indicate the
actual size of the address stored there. A successful call returns the
length of the message. If a message is too long to fit in the supplied
buffer, excess bytes may be discarded depending on the type of socket the
message is received from (see socket(2)).
If no messages are available at the socket, the receive call waits for a
message to arrive, unless the socket is nonblocking (see ioctl(2)) in
which case the call returns -1 with the external variable errno set to
EWOULDBLOCK.
The select(2) call may be used to determine when more data arrives.
The flags argument to a recv call is formed by or'ing one or more of the
values,
#define MSG_OOB 0x1 /* process out-of-band data */
#define MSG_PEEK 0x2 /* peek at incoming message */
#define MSG_WAITALL 0x40 /* wait for full request or error */
Page 1
RECV(2)RECV(2)
#define MSG_DONTWAIT 0x80 /* this message should be nonblocking */
The recvmsg call uses a msghdr structure to minimize the number of
directly supplied parameters. This structure has the following form, as
defined in <sys/socket.h>:
struct msghdr {
caddr_t msg_name; /* optional address */
int msg_namelen; /* size of address */
struct iovec *msg_iov; /* scatter/gather array */
int msg_iovlen; /* # elements in msg_iov */
caddr_t msg_accrights; /* access rights sent/received */
int msg_accrightslen;
};
Here msg_name and msg_namelen specify the destination address if the
socket is unconnected; msg_name may be given as a null pointer if no
names are desired or required. The msg_iov and msg_iovlen describe the
scatter/gather locations. The iovec structure is defined as
struct iovec {
caddr_t iov_base;
int iov_len;
};
Each iovec entry specifies the base address and length of an area in
memory where data should be placed. recvmsg will always fill an area
completely before proceeding to the next.
A buffer to receive any access rights sent along with the message is
specified in msg_accrights, which has length msg_accrightslen. Access
rights are opaque data that are interpreted within the context of the
communication domain and are currently limited to file descriptors, which
each occupy the size of an int (see unix(7F) for details).
RETURN VALUE
These calls return the number of bytes received, or -1 if an error
occurred.
ERRORS
The calls fail if:
[EBADF] The argument s is an invalid descriptor.
[ENOTSOCK] The argument s is not a socket.
[EWOULDBLOCK] The socket is marked non-blocking and the receive
operation would block.
[EINTR] The receive was interrupted by delivery of a signal
before any data was available for the receive.
Page 2
RECV(2)RECV(2)
[EFAULT] The data was specified to be received into a non-
existent or protected part of the process address
space.
[EMSGSIZE] The msg_iovlen member of the msghdr structure pointed
to by message is less than or equal to 0, or is
greater than MSG_MAXIOVLEN, as defined in
<sys/socket.h>.
SEE ALSOfcntl(2), getsockopt(2), read(2), select(2), send(2), socket(2)NOTES
ABI-compliant versions of the above calls can be obtained from
libsocket.so.
When using recvmsg to receive access rights, it may be necessary for the
application to request a single byte of normal data as well, so that the
call does not return immediately if the access rights are not yet
present. Doing so will cause the recvmsg call to block until the access
rights are available.
For each of these three functions, recv, recvfrom and recvmsg , there are
three types of functions in n32 and 64 bit C libraries for IRIX 6.5.19
and later versions. One is the normal type when _XOPEN_SOURCE is not
defined; the second is XPG5 type when _XOPEN_SOURCE is set to >= 500; and
the third is XPG4 type when _XOPEN_SOURCE set to < 500.
recv function:
1. For the normal case when _XOPEN_SOURCE is not defined, third argument
type, will be an int and the normal recv is used.
2. When _XOPEN_SOURCE is >= 500 or < 500, third argument type will be a
size_t and XPG5 or XPG4 recv is used.
recvfrom function:
1. For the normal case when _XOPEN_SOURCE is not defined, third argument
type, will be an int and the sixth argument will be a pointer to
socklen_t type, which is actually a pointer to an int, and the normal
recvfrom is used.
2. When _XOPEN_SOURCE is set to >= 500, third argument type will be a
size_t and the sixth argument will be a pointer to a socklen_t type,
which is actually a pointer to u_int32_t type.
3. When _XOPEN_SOURCE is set to < 500, third argument type will be a
size_t and the sixth argument will be a pointer to a size_t type.
recvmsg function:
1. For the normal case when _XOPEN_SOURCE is not defined, the normal
struct msghdr will be used in the second argument.
2. When _XOPEN_SOURCE is set to >= 500, _XOPEN5 struct msghdr will be
used in the second argument.
3. When _XOPEN_SOURCE is set to < 500, _XOPEN4 struct msghdr will be used
in the second argument.
Page 3
RECV(2)RECV(2)
In the AF_UNIX domain recvmsg function behaves in the following way:
a. If _XOPEN_SOURCE is not defined, the msg_accrights field of struct
msghdr that is used to receive access rights is limited to file
descriptors, which each occupy the size of an int.
b. If _XOPEN_SOURCE is defined, the msg_control field of struct msghdr
that is used to receive ancillary data is limited to a cmsghdr structure
followed by an array of file descriptors. The fields of cmsghdr
structure are set to:
cmsg_level set to SOL_SOCKET; cmsg_type set to SCM_RIGHTS; and cmsg_len
set to data byte count, including the cmsghdr.
Refer <sys/socket.h> for alternate definitions of socklen_t type and
struct msghdr.
XPG5 type functions are not supported in o32 C library.
The XPG5 type recv, recvfrom and recvmsg functions are actually defined
as static inline functions in <sys/socket.h>, and each call a new
function _xpg5_recv, _xpg5_recvfrom or _xpg5_recvmsg which are specific
to IRIX 6.5.19 and later. Applications that call any of these XPG5 type
functions should check for the existence of the new symbol as done in the
following example.
#include <sys/socket.h>
#include <optional_sym.h>
if (_MIPS_SYMBOL_PRESENT(_xpg5_recv)) {
recv(s, &buf, len, flags);
} else {
...
}
Because the static inline functions are defined in each source file that
includes <sys/socket.h>, these static functions will have different
addresses in case the inline expansion is not performed. This may cause
problems if the address of the function is examined in programs. To
avoid this problem, use -D_XPG5_RECV_USER_DEFINED compile option to
disable the static inline definition of recv in <sys/socket.h>, and
define a user defined function in the following way:
ssize_t
recv(int _s, void *_buf, size_t _len, int _flags)
{
return(_xpg5_recv(_s, _buf, _len, _flags));
}
Similarly use -D_XPG5_RECVFROM_USER_DEFINED or
-D_XPG5_RECVMSG_USER_DEFINED compile option to disable the static inline
definition of recvfrom or recvmsg in <sys/socket.h>
Page 4
RECV(2)RECV(2)
Use the appropriate compile option always, when a user defined XPG5
function is required.
Page 5