To whom it may concern, and it may concern you greatly, stream_set_blocking has no effect on stream_socket_accept.
If you want it to return right away, connection or not, use 0 for the timeout parameter.
説明
resource stream_socket_accept
( resource $server_socket
[, float $timeout = ini_get("default_socket_timeout")
[, string &$peername
]] )
以前に stream_socket_server() によって作られたソケットの接続を受け入れます。
パラメータ
- timeout
-
デフォルトのソケット接続待ちタイムアウトを上書きします。 時間は秒単位で指定します。
- peername
-
接続元のクライアントの名前 (アドレス) が含まれていて、 選択したトランスポートで有効であった場合に、それを設定します。
注意: 後で stream_socket_get_name() を使用して指定することもできます。
返り値
成功した場合に TRUE を、失敗した場合に FALSE を返します。
注意
警告
この関数は UDP サーバソケットとともに使用すべきではありません。 代わりに stream_socket_recvfrom() および stream_socket_sendto() を使用します。
参考
- stream_socket_server() - インターネットドメインまたは Unix ドメインのサーバソケットを作成する
- stream_socket_get_name() - ローカルまたはリモートのソケットの名前を取得する
- stream_set_blocking() - ストリームのブロックモードを有効にする / 解除する
- stream_set_timeout() - ストリームにタイムアウトを設定する
- fgets() - ファイルポインタから 1 行取得する
- fgetss() - ファイルポインタから 1 行取り出し、HTML タグを取り除く
- fwrite() - バイナリセーフなファイル書き込み処理
- fclose() - オープンされたファイルポインタをクローズする
- feof() - ファイルポインタがファイル終端に達しているかどうか調べる
- cURL 関数
stream_socket_accept
fred dot hakeem dot smith at muslimamerica dot bob dot net
02-Jan-2008 05:33
02-Jan-2008 05:33
mickael dot bailly at free dot fr
18-Jul-2006 07:10
18-Jul-2006 07:10
this function, compared to the function socket_accept, got an extra argument "timeout".
To make this function wait indefinitelly to incoming connections, just as in socket_accept, set timeout to -1. It works for me with PHP 5.0.4.
leleu256NOSPAM at hotmail dot com
02-Nov-2004 10:58
02-Nov-2004 10:58
This code could be very helpfull...
The following code is for the "server". It listen for a message until CTRL-C
<?php
while (true)
{
// disconnected every 5 seconds...
receive_message('127.0.0.1','85',5);
}
function receive_message($ipServer,$portNumber,$nbSecondsIdle)
{
// creating the socket...
$socket = stream_socket_server('tcp://'.$ipServer.':'.$portNumber, $errno, $errstr);
if (!$socket)
{
echo "$errstr ($errno)<br />\n";
}
else
{
// while there is connection, i'll receive it... if I didn't receive a message within $nbSecondsIdle seconds, the following function will stop.
while ($conn = @stream_socket_accept($socket,$nbSecondsIdle))
{
$message= fread($conn, 1024);
echo 'I have received that : '.$message;
fputs ($conn, "OK\n");
fclose ($conn);
}
fclose($socket);
}
}
?>
The following code is for the "client". It send a message, and read the respons...
<?php
send_message('127.0.0.1','85','Message to send...');
function send_message($ipServer,$portServer,$message)
{
$fp = stream_socket_client("tcp://$ipServer:$portServer", $errno, $errstr);
if (!$fp)
{
echo "ERREUR : $errno - $errstr<br />\n";
}
else
{
fwrite($fp,"$message\n");
$response = fread($fp, 4);
if ($response != "OK\n")
{echo 'The command couldn\'t be executed...\ncause :'.$response;}
else
{echo 'Execution successfull...';}
fclose($fp);
}
}
?>
