NAME 名称
listen - listen for connections on a socket 在一个套接字上倾听连接
SYNOPSIS 概述
#include <sys/socket.h>
int listen(int s, int backlog);
DESCRIPTION 描述
在接收连接之前,首先要使用 socket(2) 创建一个套接字,然后调用 listen 使其能够自动接收到来的连接并且为连接队列指定一个长度限制. 之后就可以使用 accept(2) 接收连接. listen 调用仅适用于 SOCK_STREAM 或者 SOCK_SEQPACKET 类型的套接字.
参数 backlog 指定未完成连接队列的***长度.如果一个连接请求到达时未完成连接队列已满,那么客户端将接收到错误 ECONNREFUSED. 或者,如果下层协议支持重发,那么这个连接请求将被忽略,这样客户端在重试的时候就有成功的机会.
NOTES 注意
在TCP套接字中 backlog 的含义在Linux 2.2中已经改变. 它指定了已经完成连接正等待应用程序接收的套接字队列的长度,而不是未完成连接的数目.未完成连接套接字队列的***长度可以使用 tcp_max_syn_backlog sysctl设置当打开syncookies时不存在逻辑上的***长度,此设置将被忽略.参见 tcp(7) 以获取更多信息.
RETURN VALUE 返回值
函数执行成功时返回0.错误时返回-1,并置相应错误代码. errno
ERRORS 错误
- EBADF
- 参数 s 不是合法的描述符.
- ENOTSOCK
- 参数 s 不是一个套接字.
- EOPNOTSUPP
- 套接字类型不支持 listen 操作.
CONFORMING TO 兼容于
Single Unix, 4.4BSD, POSIX 1003.1g. listen 函数调用最初出现于4.2BSD.
BUGS 勘误
如果套接字类型是 AF_INET, 并且参数 backlog 大于常量 SOMAXCONN (Linux 2.0&2.2中是128),它将被自动截断为 SOMAXCONN 的值. 有的BSD系统(以及一些BSD扩展)将backlog值限制为5.
SEE ALSO 参见
accept(2), connect(2), socket(2)
#p#
NAME
listen - listen for connections on a socket
SYNOPSIS
#include <sys/socket.h>
int listen(int s, int backlog);
DESCRIPTION
To accept connections, a socket is first created with socket(2), a willingness to accept incoming connections and a queue limit for incoming connections are specified with listen, and then the connections are accepted with accept(2). The listen call applies only to sockets of type SOCK_STREAM or SOCK_SEQPACKET.
The backlog parameter defines the maximum length the queue of pending connections may grow to. If a connection request arrives with the queue full the client may receive an error with an indication of ECONNREFUSED or, if the underlying protocol supports retransmission, the request may be ignored so that retries succeed.
NOTES
The behaviour of the backlog parameter on TCP sockets changed with Linux 2.2. Now it specifies the queue length for completely established sockets waiting to be accepted, instead of the number of incomplete connection requests. The maximum length of the queue for incomplete sockets can be set using the tcp_max_syn_backlog sysctl. When syncookies are enabled there is no logical maximum length and this sysctl setting is ignored. See tcp(7) for more information.
RETURN VALUE
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
ERRORS
- EADDRINUSE
- Another socket is already listening on the same port.
- EBADF
- The argument s is not a valid descriptor.
- ENOTSOCK
- The argument s is not a socket.
- EOPNOTSUPP
- The socket is not of a type that supports the listen operation.
CONFORMING TO
Single Unix, 4.4BSD, POSIX 1003.1g draft. The listen function call first appeared in 4.2BSD.
BUGS
If the socket is of type AF_INET, and the backlog argument is greater than the constant SOMAXCONN (128 in Linux 2.0 & 2.2), it is silently truncated to SOMAXCONN. Don't rely on this value in portable applications since BSD (and some BSD-derived systems) limit the backlog to 5.
SEE ALSO
accept(2), connect(2), socket(2)