United States-English |
|
|
HP-UX Reference > Rrecv(2)HP-UX 11i Version 3: February 2007 |
|
NAMErecv(), recvfrom(), recvmsg() — receive a message from a socket SYNOPSIS#include <sys/socket.h> int recv(int s, void *buf, int len, int flags); int recvfrom( int s, void *buf, int len, int flags, void *from, int *fromlen ); int recvmsg(int s, struct msghdr msg[], int flags); DESCRIPTIONThe recv(), recvfrom(), and recvmsg() system calls are used to receive messages from a socket. s is a socket descriptor from which messages are received. buf is a pointer to the buffer into which the messages are placed. len is the maximum number of bytes that can fit in the buffer referenced by buf. If the socket uses connection-based communications, such as a SOCK_STREAM socket, these calls can only be used after the connection has been established (see connect(2)). For connectionless sockets such as SOCK_DGRAM, these calls can be used whether a connection has been specified or not. recvfrom() operates in the same manner as recv() except that it is able to return the address of the socket from which the message was sent. For connected datagram sockets, recvfrom() simply returns the same address as getpeername() (see getpeername(2)). For stream sockets, recvfrom() retrieves data in the same manner as recv(), but does not return the socket address of the sender. If from is nonzero, the source address of the message is placed in the socket address structure pointed to by from. fromlen is a value-result parameter, initialized to the size of the structure associated with from, and modified on return to indicate the actual size of the address stored there. If the memory pointed to by from is not large enough to contain the entire address, only the first fromlen bytes of the address are returned. For message-based sockets such as SOCK_DGRAM, the entire message must be read in a single operation. If a message is too long to fit in the supplied buffer, the excess bytes are discarded. For stream-based sockets such as SOCK_STREAM, there is no concept of message boundaries. In this case, data is returned to the user as soon as it becomes available, and no data is discarded. See the AF_CCITT Only subsection below for a list of the exceptions to this behavior for connections in the address family AF_CCITT. recvmsg() performs the same action as recv(), but scatters the read data into the buffers specified in the msghdr structure (see X/Open Sockets Only below). This structure is defined in <sys/socket.h> and has the following form (HP-UX BSD Sockets Only): struct msghdr { caddr_t msg_name; /* optional address */ int msg_namelen; /* size of address */ struct iovec *msg_iov; /* scatter array for data */ int msg_iovlen; /* # of elements in msg_iov */ caddr_t msg_accrights; /* access rights */ int msg_accrightslen; /* size of msg_accrights */ } msg_name points to a sockaddr structure in which the address of the sending socket is to be stored, if the socket is connectionless; msg_name may be a null pointer if no name is specified. msg_iov specifies the locations of the character arrays for storing the incoming data. msg_accrights specifies a buffer to receive any access rights sent along with the message. Access rights are limited to file descriptors of size int. If access rights are not being transferred, set the msg_accrights field to NULL. Access rights are supported only for AF_UNIX. By default, recv(), recvfrom() and recvmsg() are blocking system calls and do not complete until there is data to read or the end of the socket data stream is reached (the remote side of a connection-based socket has performed an orderly shutdown and there is no more data to read). There are three ways to enable nonblocking mode:
Although the use of FIONBIO is not recommended, if nonblocking I/O is enabled using FIOSNBIO or the equivalent FIONBIO request (defined in <sys/ioctl.h> and explained in ioctl(2), ioctl(5) and socket(7)), the recv() request completes in one of six ways:
If nonblocking I/O is disabled using FIOSNBIO, recv() always executes completely (blocking as necessary) and returns the number of bytes read. If the O_NONBLOCK flag is set using fcntl() (defined in <sys/fcntl.h> and explained in fcntl(2) and fcntl(5)), POSIX-style nonblocking I/O is enabled. In this case, the recv() request completes in one of six ways:
If the O_NDELAY flag is set using fcntl() (defined in <sys/fcntl.h> and explained in fcntl(2) and fcntl(5)), nonblocking I/O is enabled. In this case, the recv() request completes in one of five ways:
If the O_NONBLOCK or O_NDELAY flag is cleared using fcntl(), the corresponding style of nonblocking I/O, if previously enabled, is disabled. In this case, recv() always executes completely (blocking as necessary) and returns the number of bytes read. Since both the fcntl() O_NONBLOCK and O_NDELAY flags and ioctl() FIOSNBIO request are supported, some clarification on how these features interact is necessary. If the O_NONBLOCK or O_NDELAY flag has been set, recv() requests behave accordingly, regardless of any FIOSNBIO requests. If neither the O_NONBLOCK flag nor the O_NDELAY flag has been set, FIOSNBIO requests control the behavior of recv(). By default nonblocking I/O is disabled. select() can be used to determine when more data arrives by selecting the socket for reading. The flags parameter can be set to MSG_PEEK, MSG_OOB, both, or zero. If it is set to MSG_PEEK, any data returned to the user still is treated as if it had not been read. The next recv() rereads the same data. The MSG_OOB flag is used to receive out-of-band data. For TCP SOCK_STREAM sockets, both the MSG_PEEK and MSG_OOB flags can be set at the same time. The MSG_OOB flag value is supported for TCP SOCK_STREAM sockets only. MSG_OOB is not supported for AF_UNIX or AF_VME_LINK sockets. A read() call made to a socket behaves in exactly the same way as a recv() with flags set to zero. AF_CCITT OnlyConnections in the address family AF_CCITT support message-based sockets only. Although the user specifies connection-based communications (SOCK_STREAM), the X.25 subsystem communicates via messages. This address family does not support SOCK_DGRAM socket types. Normally, each recv() returns one complete X.25 message. If the socket is in nonblocking mode, recv() behaves as described above. Note that if the user specifies len less than the actual X.25 message size, the excess data is discarded and no error indication is returned. The size of the next available message as well as the state of MDTF, D, and Q bits can be obtained with ioctl(X25_NEXT_MSG_STAT). Connections of the address family AF_CCITT receive data in the same way as message-based connections described above, with the following additions and exceptions:
X/Open Sockets OnlyFor X/Open Sockets, the msghdr structure has the following form: (UNIX 03) struct msghdr { void *msg_name; /* optional address */ socklen_t msg_namelen; /* size of address */ struct iovec *msg_iov; /* scatter array for data */ int msg_iovlen; /* # of elements in msg_iov */ void *msg_control; /* ancillary data, see below */ socklen_t msg_controllen; /* ancillary data buffer len */ int msg_flags; /* flags on received message */ } Obsolescent (UNIX 95) struct msghdr { void *msg_name; /* optional address */ size_t msg_namelen; /* size of address */ struct iovec *msg_iov; /* scatter array for data */ int msg_iovlen; /* # of elements in msg_iov */ void *msg_control; /* ancillary data, see below */ size_t msg_controllen; /* ancillary data buffer len */ int msg_flags; /* flags on received message */ } msg_control specifies a buffer to receive any ancillary data sent along with the message. Ancillary data consists of a sequence of pairs, each consisting of a cmsghdr structure followed by a data array. The data array contains the ancillary data message, and the cmsghdr structure contains descriptive information that allows an application to correctly parse the data. cmsghdr has the following structure: (UNIX 03) struct cmsghdr { socklen_t cmsg_len; /* data byte count, including hdr*/ int cmsg_level; /* originating protocol */ int cmsg_type; /* protocol-specific type */ } Obsolescent (UNIX 95) struct cmsghdr { size_t cmsg_len; /* data byte count, including hdr*/ int cmsg_level; /* originating protocol */ int cmsg_type; /* protocol-specific type */ } If the cmsg_level is SOL_SOCKET, and cmsg_type is SCM_RIGHTS, then it indicates that the data array contains the access rights to be received. Access rights are supported only for AF_UNIX. Access rights are limited to file descriptors of size int. If the cmsg_level is IPPROTO_IPV6, then cmsg_type must be one of the supported types: IPV6_PKTINFO, IPV6_HOPLIMIT, IPV6_NEXTHOP, IPV6_RTHDR, IPV6_HOPOPTS, IPV6_DSTOPTS or IPV6_RTHDRDSTOPTS. (See description in IPv6(7P)). If ancillary data are not being transferred, set the msg_control field to NULL, and set the msg_controllen field to 0. The flags parameter accepts a new value, MSG_WAITALL, which requests that the function block until the full amount of data requested can be returned. The function may return a smaller amount of data if a signal is caught, the connection is terminated, or an error is pending for the socket. On successful completion of recvmsg(), the msg_flags member of the message header is the bitwise-inclusive OR of all of the following flags that indicate conditions detected for the received message.
RETURN VALUErecv(), recvfrom(), and recvmsg() return the following values:
ERRORSIf recv(), recvfrom(), or recvmsg() fails, errno is set to one of the following values.
OBSOLESCENCECurrently, the socklen_t and size_t types are the same size. This is compatible with the UNIX 95 and UNIX 03 profiles. However, in a future release, socklen_t might be a different size. In that case, passing a size_t pointer will evoke compile-time warnings, which must be corrected in order for the application to behave correctly. Also, the size of the msghdr and cmsghdr structures and the relative position of their members will be different, which might affect application behavior. Applications that use socklen_t now, where appropriate, will avoid such migration problems. On the other hand, applications that need to be portable to the UNIX 95 profile should follow the X/Open specification (see xopen_networking(7)). WARNINGSLinking binary objects compiled to X/Open Sockets specification and binary objects compiled to HP-UX BSD Sockets specification to the same executable may result in unexpected behavior, including application abnormal termination and unexpected socket errors. See xopen_networking(7) for details and remedy. FUTURE DIRECTIONCurrently, the default behavior is the HP-UX BSD Sockets; however, it might be changed to X/Open Sockets in a future release. At that time, any HP-UX BSD Sockets behavior that is incompatible with X/Open Sockets might be obsoleted. Applications that conform to the X/Open specification now will avoid migration problems (see xopen_networking(7)). AUTHORrecv(), recvmsg(), and recvfrom() were developed by HP and the University of California, Berkeley. SEE ALSOgetsockopt(2), read(2), select(2), send(2), socket(2), thread_safety(5), inet(7F), socket(7), IPv6(7P), TCP(7P), UDP(7P), UNIX(7P), xopen_networking(7). |
Printable version | ||
|