listen 中文man頁面
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)