The PHP socket does not handle the data stream, how to handle it (as if it were blocked)
Requirements: PHP accepts a hardware to send data to port 8888, if received, should socket_send function return "\xfa\x01\x01\xff\xaa\xaa\x00\x01\x00\x00\x00\x00\x00\x01", After the hardware receives the data sent by the Socket_send, it will "drip" a sound, but the problem arises, a hardware is OK, but when multiple hardware at the same time to connect and send data at the same time, the hardware can not be a continuous response (that is, the "drip" sound), that is, can be continuously issued "drip" sound, it is , about a few seconds later, and began to respond, after a while, a few connections on the hardware is the case, I have used the non-blocking mode, or this, to solve the problem, the following post code
PHP Code
<?phperror_reporting (E_all); Set_time_limit (0) ini_set ("Allow_call_time_pass_reference", true);//listener Port $port = 8888;//Maximum Connection Pool $max_users = 50;//Create a listening port//$sock = Socket_create_listen ($PORT); $commonProtocol = Getprotobyname ("TCP"); $ Sock = Socket_create (Af_inet, Sock_stream, $commonProtocol); @socket_bind ($sock, ' 192.168.1.112@socket_listen ($sock) ; if (! $sock) {exit (1);} Do not block Socket_set_nonblock ($sock), $connections = Array (), $input = Array (), $close = Array (), while (true) {//sleep (3); $readfds = Array_merge ($connections, Array ($sock)); $writefds = Array (); Select a connection to get the read, write connection channel if (Socket_select ($readfds, $writefds, $e = null, $t =60)) {foreach ($readfds as $RFD) {//If the current server is listening connection if ($RFD = = $sock) {//Accept Client Connection $newco nn = socket_accept ($sock); $i = (int) $newconn; $reject = "; if (count ($connections) >= $MAX _users) {$REJECt = "Server full." Try again later.\n "; }//Put the current client connection as Socket_select select $connections [$i] = $newconn; Enter the connection resource cache container $writefds [$i] = $newconn; Not connected properly if ($reject) {$close [$i] = true; } else {echo "Welcome to the PHP Chat server!\n"; }//initializes the cache container for the current connection read content $input [$i] = ""; Continue }//Client connection $i = (int) $rfd; Read $tmp = @socket_read ($RFD, Php_normal_read); if (! $tmp) {//Cannot read content print "Connection closed on socket $i \ n"; Close ($i); Continue } $input [$i]. = $tmp; $tmp = substr ($input [$i],-1); /*if ($tmp! = "\ r" && $tmp! = "\ n") {//no end of line, more data coming Continue }*/$line = Trim ($input [$i]); $input [$i] = ""; Echo ' Client >> '. $line. " \ r \ n "; Socket_getpeername ($connections [$i],& $remoteIP,& $remotePort); Echo $remoteIP. " \ r \ n "Echo $remotePort." \ r \ n ";//$data =str_split ($buffer);//print_r ($data); $str =" \xfa\x01\x01\xff\xaa\xaa\x00\x01\x00\x00\x00\x00\x00\ X01 "; Socket_send ($connections [$i], $str, strlen ($STR), 0); } foreach ($writefds as $wfd) {$i = (int) $wfd; $w = Socket_write ($wfd, "Hello"); }}}function Close ($i) {Global $connections, $input, $close; Socket_shutdown ($connections [$i]); Socket_close ($connections [$i]);Unset ($connections [$i]); Unset ($input [$i]); Unset ($close [$i]); >
PHP socket does not handle data flow, how to avoid (as if blocking)