This is an example of Stream_select on the PHP website.
http://php.net/manual/en/function.stream-socket-server.php
程序是从遍历连接的客户端是不是serversocket来判断连接, 遍历每个客户端都fread来读,根据读的结果是FALSE还是0判断是不是断开连接了,但是上面的程序一运行,cpu就上升,然后机器就卡住了,是不是这种判断连接有断开的方式有问题?
Reply content:
This is an example of Stream_select on the PHP website.
http://php.net/manual/en/function.stream-socket-server.php
The
program is from traversing the connected client is not serversocket to determine the connection, traversing each client is fread to read, according to whether the read result is False or 0 to determine whether it is disconnected, but the above program runs, the CPU goes up, then the machine is stuck , is there a problem with the way the connection is broken?
Run this example and report a bunch of errors, you didn't error?
I modified the
$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, 0); foreach ($read as $rstream) {$conn = Stream_socket_accept ($socket); if ($conn) {fwrite ($conn, "hello! The time is. Date ("n/j/y g:i a"). "\ n"); $sock _data = fread ($conn, 1024x768);//Var_dump ($sock _data); if ($sock _data = = = 0) {} elseif ($sock _data = = = False) {echo "Something bad happened". Php_eol; } else {fwrite ($conn, "You have sent: [". $sock _data. "]\n"); } fclose ($conn); }}}