recv(2) 4 BSD recv(2)
NAME
recv, recvfrom, recvmsg - receive a message from a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
cc = recv (s, buf, len, flags)
int cc, s;
char *buf;
int len, flags;
cc = recvfrom (s, buf, len, flags, from, fromlen)
int cc, s;
char *buf;
int len, flags;
struct sockaddr *from;
int *fromlen;
cc = recvmsg (s, msg, flags)
int cc, s;
struct msghdr msg[];
int flags;
DESCRIPTION
recv, recvfrom, and recvmsg are used to receive messages
from a socket.
The recv call may be 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. The length of the message is returned in cc. 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 non-
blocking (see ioctl(2)) in which case a cc of -1 is returned
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 receive call is formed by or'ing one
or more of the values,
Page 1 CX/UX Programmer's Reference Manual
recv(2) 4 BSD recv(2)
MSGPEEK /* peek at incoming message */
MSGOOB /* process out-of-band data */
The recvmsg call uses a msghdr structure to minimize the
number of directly supplied parameters, and to receive file
descriptors from another process (Unix domain only). This
structure has the following form, as defined in
<sys/socket.h>:
struct msghdr {
caddrt msgname; /* optional address */
int msgnamelen; /* size of address */
struct iov *msgiov; /* scatter/gather array */
int msgiovlen; /* # elements in msgiov */
caddrt msgaccrights; /* access rights sent/received */
int msgaccrightslen;
};
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 loca-
tions, as described in read(2). Access rights to be sent
along with the message are specified in msg_accrights, which
has length msg_accrightslen.
RECEIVING OUT-OF-BAND DATA IN INTERNET DOMAIN
When out-of-band data arrives at a socket, the system could
signal a group of processes, or a specific process, or no
process at all.
- If the socket's process group id is positive, the
system signals all process in that process group.
- If the socket's process group id is negative, the
system only signals the process whose process id is the
absolute value of the socket's process group id.
- If the socket's process group id is 0 (the default
value), the system does not signal any process.
To assign a socket process group id, use the ioctl(2) system
call with the command SIOCSPGRP.
RECEIVING FILE DESCRIPTORS IN CX/UX DOMAIN
In Unix domain, process A can open a set of files, then send
these file descriptors to process B. Process B then can read
or write those files which were already opened (and perhaps
accessible only) by A.
Page 2 CX/UX Programmer's Reference Manual
recv(2) 4 BSD recv(2)
The active process stores the file descriptors in the field
msg_accrights of the structure msghdr, and use the system
call sendmsg(2) to send the descriptors. The passive process
then can receive the descriptors via the system call
recvmsg(2); the descriptors are returned in the field
msg_accrights of msghdr.
NOTE
The ability to send and receive file descriptors is not
available in CX/SX when configured to B1 level security.
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 avail-
able for the receive.
[EFAULT] The data was specified to be received
into a non-existent or protected part of
the process address space.
NOTE
These functions are defined in the 88open Binary and Object
Compatibility Standards' Networking Supplements (BCSNS and
OCSNS) for use in BCSNS/OCSNS compliant applications on
Series. OCSNS-defined functions may be accessed by passing
OCS options to cc(1) and ld(1).
SEE ALSO
read(2), send(2), socket(2). See also bind(2) for address
formats in different communication domains.
Page 3 CX/UX Programmer's Reference Manual