按照安德烈的例子here,我试图让stream socket server为多个客户提供服务。
$master = array();
$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr);
if (!$socket) {
echo "$errstr ($errno)<br />\n";
} else {
$master[] = $socket;
while (1) {
$read = $write= $master;
$mod_fd = stream_select($read, $write, $_e = NULL, 5);
if ($mod_fd === FALSE) {
break;
}
foreach ($read as $readStream) {
if ($readStream === $socket) {
$conn = stream_socket_accept($socket);
stream_set_blocking($conn, 0);
stream_set_timeout($conn, 5);
$master[] = $conn;
}
foreach ($this->master as $x) {
var_dump(socket_get_status($x));
}
}
}
}
但是流不会超时,即使我从客户端拉出网络电缆也没有设置eof
标志或timed_out
标志。
输出始终为:
array(7) {
["stream_type"]=>
string(14) "tcp_socket/ssl"
["mode"]=>
string(2) "r+"
["unread_bytes"]=>
int(0)
["seekable"]=>
bool(false)
["timed_out"]=>
bool(false)
["blocked"]=>
bool(true)
["eof"]=>
bool(false)
}
注意:我正在使用$>php test.php
修改
我找到了一个可能的答案here为什么它不起作用但我尝试了:
socket_set_option($conn, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>5, 'usec'=>0));
建议,但这没有任何区别