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...