RECV(2) Linux Programmer's Manual RECV(2)
NAME
recv, recvfrom, recvmsg - receive a message from a socket
//从socket中接收信息
SYNOPSIS
#include
#include
ssize_t recv(int sockfd, void *buf, size_t len, int flags);
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
DESCRIPTION
The recvfrom() and recvmsg() calls are used to receive messages from a socket,
and may be used to receive data on a socket whether or not it is connection-
oriented.
//recvfrom() 和 recvmsg() 调用是用来从socket中接受数据,可以用来在socket上接收数据,不管socket是否是基于连接的.
If src_addr is not NULL, and the underlying protocol provides the source
address, this source address is filled in. When src_addr is NULL, nothing is
filled in; in this case, addrlen is not used, and should also be NULL. The
argument addrlen is a value-result argument, which the caller should
initialize before the call to the size of the buffer associated with src_addr,
and modified on return to indicate the actual size of the source address. The
returned address is truncated if the buffer provided is too small; in this
case, addrlen will return a value greater than was supplied to the call.
//如果src_addr 非空,那么下面的协议给出了已经填充好的源地址.当src_addr 为空的时候,没有任何信息填充;
//在这种情况下,addrlen 不再使用,并且也要设置为NULL.addrlen 参数是一个值-结果参数,就是说他的调用者应该在调用之前初始化src_addr参数相关缓存的大小,
//同时返回的时候修改为实际源地址的大小.返回的地址会截断如果缓冲给的太小的话;在这种情况下,返回的addrlen 值就是一个比提供给调用的值要大的值.
The recv() call is normally used only on a connected socket (see connect(2))
and is identical to recvfrom() with a NULL src_addr argument.
//recv()一般就用在连接了的socket(参见connect(2)),等效于参数src_addr 为NULL的时候的recvfrom() .
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.
//所有这些函数都返回成功接收完成的信息的长度.如果信息对提供的缓冲来说太长了,剩余的字节可能会被丢弃,这取决于数据接收的socket的类型.
If no messages are available at the socket, the receive calls wait for a
message to arrive, unless the socket is nonblocking (see fcntl(2)), in which
case the value -1 is returned and the external variable errno is set to EAGAIN
or EWOULDBLOCK. The receive calls normally return any data available, up to
the requested amount, rather than waiting for receipt of the full amount
requested.
//如果在socket上没有有效数据,那么接收的调用者就等待下个信息的到达,除非socket是非阻塞的(参见fcntl(2)),在这种情况下返回-1,
//扩展的变量errno设置为 EAGAIN 或者 EWOULDBLOCK.接收调用一般返回任何有效的数据,上限为请求的数目,而不是等待接收所有请求的数目.
The select(2) or poll(2) call may be used to determine when more data arrives.
//select(2) 或者 poll(2) 调用可以用来决定什么时候更多的数据到来.
The flags argument to a recv() call is formed by OR'ing one or more of the
following values:
//recv(2)调用的flags参数由以下的一个或几个值通过OR的形式来组织
MSG_CMSG_CLOEXEC (recvmsg() only; since Linux 2.6.23)
Set the close-on-exec flag for the file descriptor received via a UNIX
domain file descriptor using the SCM_RIGHTS operation (described in
unix(7)). This flag is useful for the same reasons as the O_CLOEXEC
flag of open(2).
//MSG_CMSG_CLOEXEC (recvmsg() 专用; 从 Linux 2.6.23 后开始支持)
//对文件描述符设置 close-on-exec 标志位,这个标志位是通过UNIX域文件描述符(设置了 SGM_RIGHTS选项)接收的(在unix(7)中有描述).
//基于相同的原因,这个标志位在open(2)的O_CLOEXEC标志位上是有用的.
MSG_DONTWAIT (since Linux 2.2)
Enables nonblocking operation; if the operation would block, the call
fails with the error EAGAIN or EWOULDBLOCK (this can also be enabled
using the O_NONBLOCK flag with the F_SETFL fcntl(2)).
//MSG_DONTWAIT (从 Linux 2.2 后开始支持)
//使能非阻塞选项;如果操作阻塞的话,那么调用就会失败,error为 EAGAIN 或者 EWOULDBLOCK(这个标志位也能够在fcntl(2)中使用 O_NOBLOCK 使能).
MSG_ERRQUEUE (since Linux 2.2)
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 IP_RECVERR). The user should
supply a buffer of sufficient size. See cmsg(3) and ip(7) for more
information. The payload of the original packet that caused the error
is passed as normal data via msg_iovec. The original destination
address of the datagram that caused the error is supplied via msg_name.
//MSG_ERRQUEUE (从 Linux 2.2 后开始支持)
//这个标志位指定队列的错误号应该从一个错误队列接收.错误是通过协议(对IPv4 是 IP_RECVERR)类型的额外信息传递的.
//用户应该提供一个足够大小的缓存.在 cmsq(3) 和 ip(7)中查看更多的细节.原始包的载荷造成的错误和正常数据一样通过 msg_iovec 传递.
//数据报的原目的地址造成的错误由 msg_name 提供.
For local errors, no address is passed (this can be checked with the
cmsg_len member of the cmsghdr). For error receives, the MSG_ERRQUEUE
is set in the msghdr. 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.
//对本地错误,没有传递地址(这种情况可以通过 cmsghhdr 的 cmsg_len 成员来检测).如果接收到了错误,msghdr设置为 MSG_ERRQUEUE.
//一个错误传递之后,随后的socket错误会再次基于下个队列的错误而产生,并会通过下一个socket选项来传递.
The error is supplied in a sock_extended_err structure:
//error 由sock_extended_err 结构体提供
#define SO_EE_ORIGIN_NONE 0
#define SO_EE_ORIGIN_LOCAL 1
#define SO_EE_ORIGIN_ICMP 2
#define SO_EE_ORIGIN_ICMP6 3
struct sock_extended_err
{
uint32_t ee_errno; /* error number */
uint8_t ee_origin; /* where the error originated 起源,发生*/
uint8_t ee_type; /* type */
uint8_t ee_code; /* code */
uint8_t ee_pad; /* padding 填充*/
uint32_t ee_info; /* additional information */
uint32_t ee_data; /* other data */
/* More data may follow */
};
struct sockaddr *SO_EE_OFFENDER(struct sock_extended_err *);
ee_errno contains the errno number of the queued error. ee_origin is
the origin code of where the error originated. The other fields are
protocol-specific. The macro SOCK_EE_OFFENDER returns 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
sa_family member of the sockaddr contains AF_UNSPEC and the other
fields of the sockaddr are undefined. The payload of the packet that
caused the error is passed as normal data.
//ee_errno 包含了队列错误的errno.
//ee_origin 是哪里错误发生的原始代码.
//其它元素是协议指定的.宏 SOCK_ERR_OFFENDER 返回一个指向网络对象的错误原始发生地方(通过给定的额外的信息的指针来标识)的指针.
//如果这个地址是未知的, sockaddr的 sa_family 成员就包含了 AF_UNSPEC 同时sockaddr的其它域 是未定义的.
//包的载荷导致的错误通过一般的数据来传递.
For local errors, no address is passed (this can be checked with the
cmsg_len member of the cmsghdr). For error receives, the MSG_ERRQUEUE
is set in the msghdr. 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.
//对本地的错误,没有传递地址(这种情况可以通过 cmsghhdr 的 cmsg_len 成员来检测).如果接收到了错误,msghdr设置为 MSG_ERRQUEUE.
//一个错误传递之后,随后的socket错误会再次基于下个队列的错误而产生,并会通过下一个socket选项来传递.
MSG_OOB
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
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 (since Linux 2.2)
For raw (AF_PACKET), Internet datagram (since Linux 2.4.27/2.6.8), and
netlink (since Linux 2.6.22) sockets: return the real length of the
packet or datagram, even when it was longer than the passed buffer.
Not implemented for UNIX domain (unix(7)) sockets.
//MSG_TRUNC (从 Linux 2.2 后开始支持)
//对原始数据(AF_PACKET) ,Internet 数据报(从 2.4.27/2.6.8 后开始支持),netlink (从 since Linux 2.6.22 开始支持)的sock:
//返回包或者数据报的实际长度,甚至实际长度比传递给缓存的值要大.并没有在UNIX域的sock上实现(参见unix(7)).
For use with Internet stream sockets, see tcp(7).
//对Internet流socket的使用,参见tcp(7).
MSG_WAITALL (since Linux 2.2)
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.
//MSG_WAITALL (从 Linux 2.2 后开始支)
//这个标志位请求操作阻塞直到满足完整的请求的数据.然而,调用可能仍然返回比请求数据少的数据如果信号发生的话,或者错误或者是断开连接的错误,或者是
//下一个接收的数据不是期望返回的类型.
The recvmsg() call uses a msghdr structure to minimize the number of directly
supplied arguments. This structure is defined as follows in :
//recvmsg() 调用使用 msghdr 结构体来最小化直接提供参数的数目.这个结构体在,中定义:
struct iovec { /* Scatter/gather array items */
void *iov_base; /* Starting address */
size_t iov_len; /* Number of bytes to transfer */
};
struct msghdr {
void *msg_name; /* optional address */
socklen_t msg_namelen; /* size of address */
struct iovec *msg_iov; /* scatter/gather array */
size_t msg_iovlen; /* # 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 */
};
Here msg_name and msg_namelen specify the source address if the socket is
unconnected; msg_name may be given as a null pointer if no names are desired
or required. The fields msg_iov and msg_iovlen describe scatter-gather
locations, as discussed in readv(2). The field msg_control, which has length
msg_controllen, points to a buffer for other protocol control-related messages
or miscellaneous ancillary data. When recvmsg() is called, msg_controllen
should contain the length of the available buffer in msg_control; upon return
from a successful call it will contain the length of the control message
sequence.
//这里 msg_name 和 msg_namelen 指定了源地址如果socket未连接的话;msg_name 可能给定为空指针如果没有名字描述或需要的话.
//域 msg_iov 和 msg_iovle 描述了 分散-聚集 位置,在readv(2)中有描述.;含有msg_controllen 的 msg_control成员指向一个
//别的协议控制信息或者各方面的额外信息的缓存.当调用recvmsg() 的时候,msg_controllen 应该包含在 msg_control 中有效缓存的长度;
//在接到成功调用的时候,msg_controllen 会包含控制信息的长度.
The messages are of the form:
struct cmsghdr {
socklen_t cmsg_len; /* data byte count, including hdr */
int cmsg_level; /* originating protocol */
int cmsg_type; /* protocol-specific type */
/* followed by
unsigned char cmsg_data[]; */
};
Ancillary data should only be accessed by the macros defined in cmsg(3).
//额外的数据应该被定义在 cmsq(3)中的宏来访问
As an example, Linux uses this ancillary data mechanism to pass extended
errors, IP options, or file descriptors over UNIX domain sockets.
//作为例子,Linux使用额外的数据几只来传递扩展的错误,IP 选项,或者是在UNIX域socket上的文件描述符.
The msg_flags field in the msghdr is set on return of recvmsg(). It can
contain several flags:
//在 msghhdr 中的msg_flags 域在 recvmsg()返回的时候被设置.msg_flags 可以包含以下几个值
MSG_EOR
indicates end-of-record; the data returned completed a record
(generally used with sockets of type SOCK_SEQPACKET).
//表示到达记录的末尾;返回的数据完成了一个记录(一般使用子 SOCK_SEQPACKET 类型的socket中).
MSG_TRUNC
indicates that the trailing portion of a datagram was discarded because
the datagram was larger than the buffer supplied.
//表示尾部的数据报被截断,因为数据报比所给的缓冲要大.
MSG_CTRUNC
indicates that some control data were discarded due to lack of space in
the buffer for ancillary data.
//表示一些控制数据被丢弃了取决于对额外数据而言缓冲没有足够的空间.
MSG_OOB
is returned to indicate that expedited or out-of-band data were
received.
//返回来表示畅通的或者越界的数据接收到了.
MSG_ERRQUEUE
indicates that no data was received but an extended error from the
socket error queue.
//表示没有接收到数据,但是从socket错误队列中来了一个扩展的错误.
RETURN VALUE
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.
//这些调用返回接收到字符的数目,如果发生错误返回-1.如果返回值是0说明节点被有序的关闭了.
//(译者注:写程序的时候注意:如果recv0的话,就是socket已经关闭的意思,不能认识是接收到0个字节;之后就可以关闭这个socket重新开始连接了)
ERRORS
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.
//这些是由socket层产生的标注错误.额外的错误也可能产生并从下面的协议模型返回.参见他们的说明页:
EAGAIN or EWOULDBLOCK
The socket is marked nonblocking and the receive operation would block,
or a receive timeout had been set and the timeout expired before data
was received. POSIX.1-2001 allows either error to be returned for this
case, and does not require these constants to have the same value, so a
portable application should check for both possibilities.
//socket被标记为非阻塞的,但是接收操作却阻塞了,或者是接收到已经设置的超时值或者是在数据接收前超时就发生了.
//POSIX.1-2001 允许在这种情况下返回这2个错误中的任何一个,而不需要这2个错误的内容有相同的值,这样一个可移植的程序就应该检查这2个可能的情况.
//(译者注:所以一般对超时的处理的errno都是将这2个错误号同时处理的,就是说发生了任何一种就是超时错误)
EBADF The argument sockfd is an invalid descriptor.
//sockfd不是有效的描述符
ECONNREFUSED
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; see signal(7).
//在任何数据有效之前接收被信号的发送打断了,参见signal(7).
EINVAL Invalid argument passed.
//无效参数传入
ENOMEM Could not allocate memory for recvmsg().
//不能对recvmsg()分配足够的内存
ENOTCONN
The socket is associated with a connection-oriented protocol and has
not been connected (see connect(2) and accept(2)).
//socket和一个基于原始连接的协议有关,但是socket没有连接(参见 connect(2) 和 accept(2)).
ENOTSOCK
The argument sockfd does not refer to a socket.
//sockfd 参数没有指向一个socket
CONFORMING TO//兼容性
4.4BSD (these function calls first appeared in 4.2BSD), POSIX.1-2001.
POSIX.1-2001 only describes the MSG_OOB, MSG_PEEK, and MSG_WAITALL flags.
NOTES
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 flags argument is int in 4.x BSD, but
unsigned int in libc4 and libc5. The len argument is int in 4.x BSD, but
size_t in libc4 and libc5. The addrlen argument is int * in 4.x BSD, libc4
and libc5. The present socklen_t * was invented by POSIX. See also
accept(2).
//给出的协议类型遵循 glibc2.单独的UNIX规格允许,除了返回的类型是 ssize_t(而 4.x BSD 和 libc4 和 libc5 都是返回int).
//flags 参数在 4.x BSD 中是int,但是在 libc4 和 libc5 中是 unsigned int.
//len 参数在 4.x BSD 中是int,但是在 libc4 和 libc5 中是 sieze_t.
//addrlen 参数在 4.x BSD,libc4 和 libc5 中是int *.
//现在的socklen_t * 是有POSIX发明的.
//参见 accept(2)
According to POSIX.1-2001, the msg_controllen field of the msghdr structure
should be typed as socklen_t, but glibc currently types it as size_t.
//根据POSIX.1-2001, msghdr 结构的 msg_controllen 域应该为 socklen_t 类型,但是 glibc 当前的类型是 size_t.
EXAMPLE
An example of the use of recvfrom() is shown in getaddrinfo(3).
SEE ALSO
fcntl(2), getsockopt(2), read(2), select(2), shutdown(2), socket(2), cmsg(3),
sockatmark(3), socket(7)
COLOPHON
This page is part of release 3.31 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can be found
at http://www.kernel.org/doc/man-pages/.
Linux 2010-08-29 RECV(2)