listen 中文man頁(yè)面
NAME 名稱(chēng)
listen - listen for connections on a socket 在一個(gè)套接字上傾聽(tīng)連接
SYNOPSIS 概述
#include <sys/socket.h>
int listen(int s, int backlog);
DESCRIPTION 描述
在接收連接之前,首先要使用 socket(2) 創(chuàng)建一個(gè)套接字,然后調(diào)用 listen 使其能夠自動(dòng)接收到來(lái)的連接并且為連接隊(duì)列指定一個(gè)長(zhǎng)度限制. 之后就可以使用 accept(2) 接收連接. listen 調(diào)用僅適用于 SOCK_STREAM 或者 SOCK_SEQPACKET 類(lèi)型的套接字.
參數(shù) backlog 指定未完成連接隊(duì)列的***長(zhǎng)度.如果一個(gè)連接請(qǐng)求到達(dá)時(shí)未完成連接隊(duì)列已滿(mǎn),那么客戶(hù)端將接收到錯(cuò)誤 ECONNREFUSED. 或者,如果下層協(xié)議支持重發(fā),那么這個(gè)連接請(qǐng)求將被忽略,這樣客戶(hù)端在重試的時(shí)候就有成功的機(jī)會(huì).
NOTES 注意
在TCP套接字中 backlog 的含義在Linux 2.2中已經(jīng)改變. 它指定了已經(jīng)完成連接正等待應(yīng)用程序接收的套接字隊(duì)列的長(zhǎng)度,而不是未完成連接的數(shù)目.未完成連接套接字隊(duì)列的***長(zhǎng)度可以使用 tcp_max_syn_backlog sysctl設(shè)置當(dāng)打開(kāi)syncookies時(shí)不存在邏輯上的***長(zhǎng)度,此設(shè)置將被忽略.參見(jiàn) tcp(7) 以獲取更多信息.
RETURN VALUE 返回值
函數(shù)執(zhí)行成功時(shí)返回0.錯(cuò)誤時(shí)返回-1,并置相應(yīng)錯(cuò)誤代碼. errno
ERRORS 錯(cuò)誤
- EBADF
- 參數(shù) s 不是合法的描述符.
- ENOTSOCK
- 參數(shù) s 不是一個(gè)套接字.
- EOPNOTSUPP
- 套接字類(lèi)型不支持 listen 操作.
CONFORMING TO 兼容于
Single Unix, 4.4BSD, POSIX 1003.1g. listen 函數(shù)調(diào)用最初出現(xiàn)于4.2BSD.
BUGS 勘誤
如果套接字類(lèi)型是 AF_INET, 并且參數(shù) backlog 大于常量 SOMAXCONN (Linux 2.0&2.2中是128),它將被自動(dòng)截?cái)酁?SOMAXCONN 的值. 有的BSD系統(tǒng)(以及一些BSD擴(kuò)展)將backlog值限制為5.
SEE ALSO 參見(jiàn)
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)