php socket超時設(shè)置的方法:首先打開相應(yīng)的腳本文件;然后創(chuàng)建一個sendUdp方法;接著通過“socket_create”等方法對超時情況進行設(shè)置;最后保存該文件即可。
創(chuàng)新互聯(lián)專注于同仁網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供同仁營銷型網(wǎng)站建設(shè),同仁網(wǎng)站制作、同仁網(wǎng)頁設(shè)計、同仁網(wǎng)站官網(wǎng)定制、小程序開發(fā)服務(wù),打造同仁網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供同仁網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
推薦:《PHP教程》
php socket 超時設(shè)置
1、php創(chuàng)建socket的方法和設(shè)置超時的方法,貼出來分享一下
//如果$waitAckSec=0,則返回成功發(fā)送的字節(jié)?? //如果$waitAckSec大于0,則返回發(fā)送后接收到得內(nèi)容 //任何情況下,失敗都返回FALSE function sendUdp($host, $port, $buff,$waitAckSec=0) { $socket = ($result = @socket_create(AF_INET,SOCK_DGRAM,SOL_UDP)); //發(fā)送超時1秒 socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>3, "usec"=>0 ) ); //接收超時6秒 socket_set_option($socket,SOL_SOCKET,SO_SNDTIMEO,array("sec"=>6, "usec"=>0 ) ); if($socket){ $result = @socket_sendto($socket,$buff,strlen($buff),0,$host,$port); if($waitAckSec>0){ $result = FALSE; $read = array($socket); $write = NULL; $except = NULL; if(@socket_select($read,$write,$except,$waitAckSec)>0){ $fromHost = ""; $fromPort = 0; @socket_recvfrom($socket,$result,4096,0,$fromHost,$fromPort); $result = phpext_unpack($result); if($result["needAck"] == 1){ $this->sendUdp($host, $port, $result["ackdata"]); if(isset ($result['data']['list']) && isset ($result['data']['totalCount'])){ $list = $result['data']['list']; $count = $result['data']['totalCount']; while($count> count($list)){ @socket_recvfrom($socket,$result_temp,4096,0,$fromHost,$fromPort); $result_temp = phpext_unpack($result_temp); $this->sendUdp($host, $port, $result_temp["ackdata"]); $list = array_merge($list,$result_temp['data']['list']); } $result['data']['list'] = $list; } }else{ @socket_recvfrom($socket,$result,4096,0,$fromHost,$fromPort); $result = phpext_unpack($result); if($result["needAck"] == 1){ $this->sendUdp($host, $port, $result["ackdata"]); if(isset ($result['data']['result']) && isset ($result['data']['userID'])){ $list = $result['data']['list']; $count = $result['data']['totalCount']; while($count> count($list)){ @socket_recvfrom($socket,$result_temp,4096,0,$fromHost,$fromPort); $result_temp = phpext_unpack($result_temp); $this->sendUdp($host, $port, $result_temp["ackdata"]); $list = array_merge($list,$result_temp['data']['list']); } $result['data']['list'] = $list; } } } }else{ $result = SEND_UDP_ERROR; } } @socket_close($socket); } return $result; }