网站建设资讯

NEWS

网站建设资讯

phpsocket超时设置的方法

php socket超时设置的方法:首先打开相应的脚本文件;然后创建一个sendUdp方法;接着通过“socket_create”等方法对超时情况进行设置;最后保存该文件即可。

创新互联专注于同仁网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供同仁营销型网站建设,同仁网站制作、同仁网页设计、同仁网站官网定制、小程序开发服务,打造同仁网络公司原创品牌,更为您提供同仁网站排名全网营销落地服务。

推荐:《PHP教程》

php socket 超时设置

1、php创建socket的方法和设置超时的方法,贴出来分享一下

//如果$waitAckSec=0,则返回成功发送的字节�?
    //如果$waitAckSec大于0,则返回发送后接收到得内容
    //任何情况下,失败都返回FALSE
    function sendUdp($host, $port, $buff,$waitAckSec=0) {
        $socket = ($result = @socket_create(AF_INET,SOCK_DGRAM,SOL_UDP));
        //发送超时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;
    }

当前文章:phpsocket超时设置的方法
标题URL:http://njwzjz.com/article/cjpois.html