内容简介
#include <sys/types.h> #include <sys/socket.h> 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) |
描述
The%uA0recvfrom() and%uA0recvmsg()%uA0调用用于从套接字接收消息,并且可以被用于接收套接字上的数据是否是面向连接的。If%uA0from%uA0is not NULL, and the underlying protocol provides the source address, this source address is filled in. The argument%uA0fromlen%uA0is a value-result parameter, initialized to the size of the buffer associated with%uA0from, and modified on return to indicate the actual size of the address stored there.
The%uA0recv() call is normally used only on a%uA0connected%uA0socket (see%uA0connect(2)) and is identical to%uA0recvfrom() with a NULL%uA0from%uA0parameter.
All three routines return the length of the message on successful completion. 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.
If no messages are available at the socket, the receive calls wait for a message to arrive, unless the socket is nonblocking (see%uA0fcntl(2)), in which case the value -1 is returned and the external variable%uA0errno%uA0set to%uA0EAGAIN. The receive calls normally return any data available, up to the requested amount, rather than waiting for receipt of the full amount requested.
The%uA0select(2) or%uA0poll(2) 调用可被用来确定何时多个数据到达。
The%uA0flags%uA0argument to a%uA0recv() call is formed by%uA0OR’ing one or more of the following values:
标签 | 描述 | |
---|---|---|
MSG_DONTWAIT | ||
%uA0 | Enables non-blocking operation if the operation would block,EAGAIN%uA0is returned (this can also be enabled using theO_NONBLOCK%uA0with the%uA0F_SETFL%uA0fcntl(2)). | |
MSG_ERRQUEUE | ||
%uA0 | This flag specifies that queued errors should be received from the socket error queue. The error is passed in an ancillary message with a type dependent on the protocol (for IPv4%uA0IP_RECVERR). The user should supply a buffer of sufficient size. See%uA0cmsg(3) andip(7) for more information. The payload of the original packet that caused the error is passed as normal data via%uA0msg_iovec. The original destination address of the datagram that caused the error is supplied via%uA0msg_name. | |
%uA0 |
For local errors, no address is passed (this can be checked with thecmsg_len%uA0member of the%uA0cmsghdr). For error receives, the%uA0MSG_ERRQUEUE%uA0is set in the%uA0msghdr. After an error has been passed, the pending socket error is regenerated based on the next queued error and will be passed on the next socket operation.
The error is supplied in a%uA0sock_extended_err%uA0structure:
|
|
%uA0 | ee_errno%uA0contains the errno number of the queued error.ee_origin%uA0is the origin code of where the error originated. The other fields are protocol specific. The macro%uA0SOCK_EE_OFFENDERreturns a pointer to the address of the network object where the error originated from given a pointer to the ancillary message. If this address is not known, the%uA0sa_family%uA0member of the%uA0sockaddrcontains%uA0AF_UNSPEC%uA0and the other fields of the%uA0sockaddr%uA0are undefined. The payload of the packet that caused the error is passed as normal data. | |
%uA0 | For local errors, no address is passed (this can be checked with thecmsg_len%uA0member of the%uA0cmsghdr). For error receives, theMSG_ERRQUEUE%uA0is set in the%uA0msghdr. After an error has been passed, the pending socket error is regenerated based on the next queued error and will be passed on the next socket operation. | |
MSG_OOB | ||
%uA0 | This flag requests receipt of out-of-band data that would not be received in the normal data stream. Some protocols place expedited data at the head of the normal data queue, and thus this flag cannot be used with such protocols. | |
MSG_PEEK | ||
%uA0 | This flag causes the receive operation to return data from the beginning of the receive queue without removing that data from the queue. Thus, a subsequent receive call will return the same data. | |
MSG_TRUNC | ||
%uA0 | Return the real length of the packet, even when it was longer than the passed buffer. Only valid for packet sockets. | |
MSG_WAITALL | ||
%uA0 | This flag requests that the operation block until the full request is satisfied. However, the call may still return less data than requested if a signal is caught, an error or disconnect occurs, or the next data to be received is of a different type than that returned. | |
The%uA0recvmsg() call uses a%uA0msghdr%uA0structure to minimize the number of directly supplied parameters. This structure has the following form, as defined in<sys/socket.h>:
|
||
Here%uA0msg_name%uA0and%uA0msg_namelen%uA0specify the source address if the socket is unconnected%uA0msg_name%uA0may be given as a null pointer if no names are desired or required. The fields%uA0msg_iov%uA0and%uA0msg_iovlenꃞscribe scatter-gather locations, as discussed in%uA0readv(2). The field%uA0msg_control, which has length%uA0msg_controllen, points to a buffer for other protocol control related messages or miscellaneous ancillary data. When%uA0recvmsg() is called,%uA0msg_controllen%uA0should contain the length of the available buffer in%uA0msg_control upon return from a successful call it will contain the length of the control message sequence. | ||
The messages are of the form:
|
||
Ancillary data should only be accessed by the macros defined in%uA0cmsg(3). | ||
As an example, Linux uses this auxiliary data mechanism to pass extended errors, IP options or file descriptors over Unix sockets. | ||
The%uA0msg_flags%uA0field in the msghdr is set on return of%uA0recvmsg(). It can contain several flags: | ||
MSG_EOR | ||
%uA0 | indicates end-of-record the data returned completed a record (generally used with sockets of type%uA0SOCK_SEQPACKET). | |
MSG_TRUNC | ||
%uA0 | indicates that the trailing portion of a datagram was discarded because the datagram was larger than the buffer supplied. | |
MSG_CTRUNC | ||
%uA0 | indicates that some control data were discarded due to lack of space in the buffer for ancillary data. | |
MSG_OOB | ||
%uA0 | is returned to indicate that expedited or out-of-band data were received. | |
MSG_ERRQUEUE | ||
%uA0 | indicates that no data was received but an extended error from the socket error queue. |
返回值
These calls return the number of bytes received, or -1 if an error occurred. The return value will be 0 when the peer has performed an orderly shutdown.错误
These are some standard errors generated by the socket layer. Additional errors may be generated and returned from the underlying protocol modules see their manual pages.标签 | 描述 |
---|---|
EAGAIN | The socket is marked non-blocking and the receive operation would block, or a receive timeout had been set and the timeout expired before data was received. |
EBADF | The argument%uA0s%uA0is an invalid descriptor. |
ECONNREFUSED | |
%uA0 | A remote host refused to allow the network connection (typically because it is not running the requested service). |
EFAULT | The receive buffer pointer(s) point outside the process’s address space. |
EINTR | The receive was interrupted by delivery of a signal before any data were available. |
EINVAL | Invalid argument passed. |
ENOMEM | Could not allocate memory for%uA0recvmsg(). |
ENOTCONN | |
%uA0 | The socket is associated with a connection-oriented protocol and has not been connected (see%uA0connect(2) and%uA0accept(2)). |
ENOTSOCK | |
%uA0 | The argument%uA0s%uA0does not refer to a socket. |
遵循于
4.4BSD (these function calls first appeared in 4.2BSD), POSIX.1-2001.POSIX.1-2001 only describes the%uA0MSG_OOB,%uA0MSG_PEEK, and%uA0MSG_WAITALL%uA0flags.
注意
The prototypes given above follow glibc2. The Single Unix Specification agrees, except that it has return values of type ‘ssize_t’ (while 4.x BSD and libc4 and libc5 all have ‘int’). The%uA0flags%uA0argument is ‘int’ in 4.x BSD, but ‘unsigned int’ in libc4 and libc5. The%uA0lenargument is ‘int’ in 4.x BSD, but ‘size_t’ in libc4 and libc5. The%uA0fromlen%uA0argument is ‘int *’ in 4.x BSD, libc4 and libc5. The present ‘socklen_t *’ was invented by POSIX. See alsoaccept(2).According to POSIX.1-2001, the%uA0msg_controllen%uA0field of the%uA0msghdr%uA0structure should be typed as%uA0socklen_t, but glibc currently (2.4) types it as%uA0size_t.