这是php网站上关于stream_select 的例子
http://php.net/manual/en/function.stream-socket-server.php
<?php
$master = array();
$socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr);
$master[] = $socket;
$read = $master;
while (1) {
$read = $master;
$_w=$_e=NULL;
$mod_fd = stream_select($read, $_w , $_e, 5);
for ($i = 0; $i < $mod_fd; ++$i) {
if ($read[$i] === $socket) {
$conn = stream_socket_accept($socket);
fwrite($conn, "Hello! The time is ".date("n/j/Y g:i a")."\n");
$master[] = $conn;
} else {
$sock_data = fread($read[$i], 1024);
var_dump($sock_data);
if (strlen($sock_data) === 0) { // connection closed
$key_to_del = array_search($read[$i], $master, TRUE);
fclose($read[$i]);
unset($master[$key_to_del]);
} else if ($sock_data === FALSE) {
echo "Something bad happened";
$key_to_del = array_search($read[$i], $master, TRUE);
unset($master[$key_to_del]);
} else {
echo "The client has sent :"; var_dump($sock_data);
fwrite($read[$i], "You have sent :[".$sock_data."]\n");
fclose($read[$i]);
unset($master[array_search($read[$i], $master)]);
}
}
}
}
程序是从遍历连接的客户端是不是serversocket来判断连接, 遍历每个客户端都fread来读,根据读的结果是FALSE还是0判断是不是断开连接了,但是上面的程序一运行,cpu就上升,然后机器就卡住了,是不是这种判断连接有断开的方式有问题?
运行这个例子,报了一堆错误,你没报错?
我修改了一下