php socket 處理不過來資料流,該如何避免(好像是阻塞了)_PHP教程

來源:互聯網
上載者:User

php socket 處理不過來資料流,該如何避免(好像是阻塞了)


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

http://www.bkjia.com/PHPjc/1023580.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1023580.htmlTechArticlephp socket 處理不過來資料流,該如何避免(好像是阻塞了) php socket 處理不過來資料流,該如何處理(好像是阻塞了) 需求:php接受一個硬體往8...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.