downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

socket_read> <socket_last_error
Last updated: Fri, 06 Nov 2009

view this page in

socket_listen

(PHP 4 >= 4.1.0, PHP 5)

socket_listenソケット上で接続待ち(listen)する

説明

bool socket_listen ( resource $socket [, int $backlog = 0 ] )

ソケット socketsocket_create() を用いて作成され、 socket_bind() で名前が付けられた後、 socket 上の接続要求を待つための通信ができるようになります。

socket_listen() は、ソケットが SOCK_STREAM 型または SOCK_SEQPACKET 型の場合のみ利用可能です。

パラメータ

socket

socket_create() で作成したソケットリソース。

backlog

最大 backlog 個の接続を処理用の キューで待ち受けることが可能です。もし待ちうけ用のキューが いっぱいになった場合、クライアントでは ECONNREFUSED の通知とともにエラーが発生します。あるいは、もし基盤となるプロトコルが リクエストの再送をサポートしている場合、再試行が成功するまで リクエストは無視されます。

注意: backlog パラメータに指定できる値の最大値は プラットフォームに大きく依存します。Linux では、最大値は SOMAXCONN に切り詰められます。win32 では、 もし SOMAXCONN を渡した場合、backlog の 最大値を適切な値に設定する責任はサービスの 提供側が負います。 このプラットフォームでは、実際の backlog の値を見つける標準的な 手段が提供されていません。

返り値

成功した場合に TRUE を、失敗した場合に FALSE を返します。 エラーコードは socket_last_error() で取得可能で、このコードを socket_strerror() に指定することにより エラーの内容を文字列として取得可能です。

参考



add a note add a note User Contributed Notes
socket_listen
Karuna Govind (karuna.kgx gmail)
15-Jul-2008 11:10
To change the maximum allowed backlog by your system (*nix machines only), first you need to find the variable for this limit:

sudo sysctl -a | grep somaxconn

On ubuntu boxes, it returns net.core.somaxconn (you need to look for the 'somaxconn' variable, the full name will vary across different systems).

Update this to a large number as follows:

sudo sysctl -w net.core.somaxconn=1024

This will work straight away. no restart required.
lewislp at yahoo dot com
30-Aug-2005 07:13
socket_listen() cannot be used for UDP communications as discussed below.

In addition, the example below discusses UDP connections, which only exist if the application manages them through a state table (the OS does not create a UDP connection upon receiving a datagram).  Having a function named socket_connect() that determines the remote IP and port only confuses the matter by giving the indication of some sort of connection handshake between two hosts.  Rather, socket_connect() only specifies the remote IP and port used by subsequent socket_send() calls.  You can achieve the same effect by skipping socket_connect() altogether and specifying the remote IP and port in socket_sendto() calls.

If you find yourself writing a connection-based protocol on top of UDP, consider using TCP.  If your application requires streaming data of some sort, use TCP to manage connections and control messages, and UDP to handle the streaming data (H.323 is an example of a suite of TCP and UDP protocols working in conjunction).

socket_read> <socket_last_error
Last updated: Fri, 06 Nov 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites