php socket如何设置超时时间_亿码酷站_编程开发技术教程

php socket如何设置超时时间_亿码酷站_编程开发技术教程

服务器 VIP24小时自助开通

【亿码酷站-编程开发教程】收集全网优质教程及源码资源!

全网优质软件开发、平面设计等教程及精品源码资源一站可得,www.ymkuzhan.com!

php socket设置超时时间的方法:【socket_set_option($socket,SOL_SOCKET,SO_SNDTIMEO,array(“sec”=>6, “usec”=>0))】。

php socket如何设置超时时间_亿码酷站_编程开发技术教程插图

【相关学习推荐:php编程(视频)】

php socket设置超时的方法:

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;
    }

php socket如何设置超时时间
—–文章转载自PHP中文网如有侵权请联系admin#tyuanma.cn删除

使用Container类实现ThinkPHP核心框架

分享到 :
服务器 VIP24小时自助开通
相关推荐

php如何实现数组转数字_亿码酷站_亿码酷站

php如何实现数组转数字_亿码酷站_亿码酷站

php如何判断文件是不是图片_编程技术_编程开发技术教程

php如何判断文件是不是图片_编程技术_编程开发技术教程

php如何返回查询结果_亿码酷站_亿码酷站

php如何返回查询结果_亿码酷站_亿码酷站

php如何实现同义词替换_亿码酷站_编程开发技术教程

php如何实现同义词替换_亿码酷站_编程开发技术教程