listenのヘルプ・マニュアル
日本語 英語
listen --help
man listen
LISTEN(2) Linux Programmer’s Manual LISTEN(2)
名前
listen - ソケット(socket)上の接続を待つ
書式
#include /* 「注意」参照 */
#include
int listen(int sockfd, int backlog);
説明
listen() は sockfd が 参 照するソケットを接続待ちソケット (passive
socket) として印をつける。接続待ちソケットとは、 accept(2) を使って到着
した接続要求を受け付けるのに使用されるソケットである。
sockfd 引き数は、 SOCK_STREAM 型か SOCK_SEQPACKET 型のソケットを参照す
るファイルディスクリプタである。
backlog 引き数は、 sockfd についての保留中の接続のキューの最大長を指 定
す る 。 キ ュ ーがいっぱいの状態で接続要求が到着すると、クライアントは
ECONNREFUSED というエラーを受け取る。下位層のプロトコルが再送信をサポー
ト していれば、要求は無視され、これ以降の接続要求の再送信が成功するかも
しれない。
返り値
成功時には0を返す。エラー時には -1を返し、 errno を適切に設定する。
エラー
EADDRINUSE
別のソケットが既に同じポートを listen している。
EBADF 引き数 sockfd は有効なディスクリプターではない。
ENOTSOCK
引き数 sockfd はソケットではない。
EOPNOTSUPP
ソケットは listen() がサポートしている型ではない。
準拠
4.4BSD, POSIX.1-2001. listen() 関数は 4.2BSDで初めて実装された。
注意
接続を受け付けるには、以下の処理が実行される。
1. socket(2) でソケットを作成する。
2. bind(2) を使ってソケットにローカルアドレスを割り当てて、他の ソ
ケットがこのソケットに connect(2) できるようにする。
3. listen() を使って、接続要求を受け付ける意志と接続要求を入れるキ
ュー長を指定する。
4. accept(2) を使って接続を受け付ける。
POSIX.1-2001 では のインクルードは必須とされてお ら ず 、
Linux ではこのヘッダファイルは必要ではない。しかし、歴史的には、いくつ
かの実装 (BSD 系) でこのヘッダファイルが必要であり、移植性が必要なア プ
リケーションではこのファイルをインクルードするのが賢明であろう。
TCP ソケットでの backlog 引き数の振る舞いは Linux 2.2 で変更された。現
在ではこの引き数は、受け付けられるのを待っている、 完全に確立されたソケ
ッ トのキューの長さを指定する。以前は不完全な接続要求の数であったが、こ
れを置き 換 え た 。 不 完 全 な ソ ケ ッ ト の キ ュ ー の 最 大 長 は
/proc/sys/net/ipv4/tcp_max_syn_backlog を用いて設定できる。 syncookie
が有効になっている場合、論理的な最大長は存在せず、この設定は無視され る
。
backlog 引 き数が /proc/sys/net/core/somaxconn の値よりも大きければ、
backlog の値は暗黙のうちにこの値に切り詰められる。このファイルのデフ ォ
ル ト値は 128 である。バージョン 2.4.5 以前のカーネルでは、この上限値は
コード埋め込みの固定値 SOMAXCONN であり、その値は 128 であった。
例
bind(2) 参照。
関連項目
accept(2), bind(2), connect(2), socket(2), socket(7)
Linux 2008-11-20 LISTEN(2)
LISTEN(2) Linux Programmer’s Manual LISTEN(2)
NAME
listen - listen for connections on a socket
SYNOPSIS
#include /* See NOTES */
#include
int listen(int sockfd, int backlog);
DESCRIPTION
listen() marks the socket referred to by sockfd as a passive socket,
that is, as a socket that will be used to accept incoming connection
requests using accept(2).
The sockfd argument is a file descriptor that refers to a socket of
type SOCK_STREAM or SOCK_SEQPACKET.
The backlog argument defines the maximum length to which the queue of
pending connections for sockfd may grow. If a connection request
arrives when the queue is 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 a later reattempt at
connection succeeds.
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 sockfd is not a valid descriptor.
ENOTSOCK
The argument sockfd is not a socket.
EOPNOTSUPP
The socket is not of a type that supports the listen() opera-
tion.
CONFORMING TO
4.4BSD, POSIX.1-2001. The listen() function call first appeared in
4.2BSD.
NOTES
To accept connections, the following steps are performed:
1. A socket is created with socket(2).
2. The socket is bound to a local address using bind(2), so that
other sockets may be connect(2)ed to it.
3. A willingness to accept incoming connections and a queue limit
for incoming connections are specified with listen().
4. Connections are accepted with accept(2).
POSIX.1-2001 does not require the inclusion of , and this
header file is not required on Linux. However, some historical (BSD)
implementations required this header file, and portable applications
are probably wise to include it.
The behavior of the backlog argument 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 /proc/sys/net/ipv4/tcp_max_syn_backlog. When
syncookies are enabled there is no logical maximum length and this set-
ting is ignored. See tcp(7) for more information.
If the backlog argument is greater than the value in
/proc/sys/net/core/somaxconn, then it is silently truncated to that
value; the default value in this file is 128. In kernels before
2.4.25, this limit was a hard coded value, SOMAXCONN, with the value
128.
EXAMPLE
See bind(2).
SEE ALSO
accept(2), bind(2), connect(2), socket(2), socket(7)
COLOPHON
This page is part of release 3.22 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 2008-11-20 LISTEN(2)