您好,登錄后才能下訂單哦!
小編給大家分享一下php socket設(shè)置超時(shí)的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
php socket設(shè)置超時(shí)的方法:首先如果【$waitAckSec=0】,則返回成功發(fā)送的字節(jié);然后如果【$waitAckSec】大于0,則返回發(fā)送后接收到得內(nèi)容;最后任何情況下,失敗都返回FALSE。
php socket設(shè)置超時(shí)的方法:
php創(chuàng)建socket的方法和設(shè)置超時(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ā)送超時(shí)1秒 socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>3, "usec"=>0 ) ); //接收超時(shí)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; }
以上是php socket設(shè)置超時(shí)的方法的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。