PHP头条
热点:

phpsocket处理不过来数据流,该如何避免(好像是阻塞了)


php socket 处理不过来数据流,该如何处理(好像是阻塞了)
需求:php接受一个硬件往8888端口上发送数据,如果收到后,应socket_send函数返回“\xFA\x01\x01\xFF\xAA\xAA\x00\x01\x00\x00\x00\x00\x00\x01”,硬件再接收到socket_send发送的数据后,会“滴”一声,但是问题出现了,一个硬件还好,但是当多个硬件同时连接并同时发送数据时,会出现硬件不能连续的回应(即发出“滴”的声音),也就是说能连续发出“滴”声后,便不在响了,大概几秒钟后,又开始响应了,过一会又不行了,几个连接上的硬件都是这样,我已经用了非阻塞模式,还是会这样,求解决方法,下面贴出代码

PHP code

= $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]);
}
?>




------解决方案--------------------
非阻塞模式

www.phpzy.comtrue/phprm/36056.htmlTechArticlephpsocket处理不过来数据流,该如何避免(好像是阻塞了) php socket 处理不过来数据流,该如何处理(好像是阻塞了) 需求:php接受一个硬件往8888端口上发送数据,如果收到后,应socket_send函数...

相关文章

PHP之友评论

今天推荐