• php socket 处理只是来数据流,该怎样避免(好像是堵塞了)


    php socket 处理只是来数据流,该怎样处理(好像是堵塞了)
    需求:php接受一个硬件往8888port上发送数据,假设收到后,应socket_send函数返回“xFAx01x01xFFxAAxAAx00x01x00x00x00x00x00x01”。硬件再接收到socket_send发送的数据后,会“滴”一声,可是问题出现了。一个硬件还好,可是当多个硬件同一时候连接并同一时候发送数据时,会出现硬件不能连续的回应(即发出“滴”的声音),也就是说能连续发出“滴”声后,便不在响了,大概几秒钟后。又開始响应了,过一会又不行了。几个连接上的硬件都是这样。我已经用了非堵塞模式,还是会这样,求解决方法,以下贴出代码

    PHP code
    <?php
    error_reporting(E_ALL);
    set_time_limit(0);
    ini_set("allow_call_time_pass_reference",true);
    
    //监听port
    $PORT = 8888;
    //最大连接池
    $MAX_USERS = 50;
    //创建监听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);
    }
    //不堵塞
    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)
                {
                    //接受client连接
                    $newconn = socket_accept($sock);
                    $i = (int)$newconn;
                    $reject = '';
                    if (count($connections) >= $MAX_USERS)
                    {
                        $reject = "Server full. Try again later.
    ";                   
                    }                
                    //将当前client连接放如socket_select选择
                    $connections[$i] = $newconn;
                    //输入的连接资源缓存容器
                    $writefds[$i] = $newconn;               
                    //连接不正常
                    if ($reject)
                    {                  
                        $close[$i] = true;
                    }
                    else
                    {
                        echo "Welcome to the PHP Chat Server!
    ";                  
                    }               
                    //初始化当前连接读取内容的缓存容器
                    $input[$i] = "";
                    continue;
                }
                //client连接
                $i = (int)$rfd;
                //读取
                $tmp = @socket_read($rfd, 14, PHP_NORMAL_READ);
                if (!$tmp)
                {
                    //读取不到内容              
                    print "connection closed on socket $i
    ";
                    close($i);
                    continue;
                }
                $input[$i] .= $tmp;
                $tmp = substr($input[$i], -1);
                /*if ($tmp != "
    " && $tmp != "
    ")
                {
                    // no end of line, more data coming
                    continue;
                }*/
                $line = trim($input[$i]);
                $input[$i] = "";
                echo 'Client >>'.$line."
    ";
                
                
                
                
                socket_getpeername($connections[$i],&$remoteIP,&$remotePort);
    echo $remoteIP."
    ";
    echo $remotePort."
    ";
    //$data=str_split($buffer);
    //print_r($data);
    $str="xFAx01x01xFFxAAxAAx00x01x00x00x00x00x00x01";
     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]);
    }
    ?>
    
    
    
  • 相关阅读:
    大话设计模式C++实现-第1章-简单工厂模式
    mac下的git的安装与简单的配置
    Execute failed: java.io.IOException: Cannot run program &quot;sdk-linux/build-tools/22.0.0/aapt&quot;: error=2
    UIScrollView 循环滚动,代码超简单
    字符编码的前世今生
    Android 4.4 KitKat 支持 u 盘功能
    Java Tread多线程(1)实现Runnable接口
    (hdu step 6.3.3)Air Raid(最小路径覆盖:求用最少边把全部的顶点都覆盖)
    每日算法之二十三:Reverse Nodes in k-Group
    Android4.0-4.4 加入实体按键振动支持的方法(java + smali版本号)
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/7354266.html
Copyright © 2020-2023  润新知