php socket cannot handle the data flow, how to avoid it (it seems to be blocked)
PHP socket cannot handle the data stream, what should I do (it seems to be blocked)
Requirement: PHP accepts a piece of hardware to send data to port 8888. If received, the socket_send function should return "xFAx01x01xFFxAAxAAx00x01x00x00x00x00x00x01". After the hardware receives the data sent by socket_send, it will make a "beep" sound, but a problem occurs. A piece of hardware still Okay, but when multiple hardware are connected at the same time and send data at the same time, the hardware will not respond continuously (that is, it will make a "beep" sound). That is to say, after it can make a "beep" sound continuously, it will no longer ring. Probably After a few seconds, it started to respond again. After a while, it stopped working again. This is the case for several connected hardware. I have used non-blocking mode, but it still happens. Looking for a solution, the code is posted below
-
PHP code
<!--?php
error_reporting(E_ALL);
set_time_limit(0);
ini_set("allow_call_time_pass_reference",true);
//监听端口
$PORT = 8888;
//最大连接池
$MAX_USERS = 50;
//创建监听端口
//$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);
}
//不阻塞
socket_set_nonblock($sock);
$connections = array();
$input = array();
$close = array();
while (true)
{
//sleep(3);
$readfds = array_merge($connections, array($sock));
$writefds = array();
//选择一个连接,获取读、写连接通道
if (socket_select($readfds, $writefds, $e = null, $t=60))
{
foreach ($readfds as $rfd)
{
//如果是当前服务端的监听连接
if ($rfd == $sock)
{
//接受客户端连接
$newconn = socket_accept($sock);
$i = (int)$newconn;
$reject = '';
if (count($connections) -->= $MAX_USERS)
{
$reject = "Server full. Try again later.\n";
}
//将当前客户端连接放如socket_select选择
$connections[$i] = $newconn;
//输入的连接资源缓存容器
$writefds[$i] = $newconn;
//连接不正常
if ($reject)
{
$close[$i] = true;
}
else
{
echo "Welcome to the PHP Chat Server!\n";
}
//初始化当前连接读取内容的缓存容器
$input[$i] = "";
continue;
}
//客户端连接
$i = (int)$rfd;
//读取
$tmp = @socket_read($rfd, 14, PHP_NORMAL_READ);
if (!$tmp)
{
//读取不到内容
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]);
}
?>
Copy after login
http://www.bkjia.com/PHPjc/1023580.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1023580.htmlTechArticlephp socket cannot handle the data flow, how to avoid it (it seems to be blocked) php socket cannot handle the data flow , how to deal with it (it seems to be blocked) Requirement: php accepts a hardware to 8...