• php socket 超时设置


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

     1 //如果$waitAckSec=0,则返回成功发送的字节�?
     2     //如果$waitAckSec大于0,则返回发送后接收到得内容
     3     //任何情况下,失败都返回FALSE
     4     function sendUdp($host, $port, $buff,$waitAckSec=0) {
     5         $socket = ($result = @socket_create(AF_INET,SOCK_DGRAM,SOL_UDP));
     6         //发送超时1秒
     7         socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>3, "usec"=>0 ) );
     8         //接收超时6秒
     9         socket_set_option($socket,SOL_SOCKET,SO_SNDTIMEO,array("sec"=>6, "usec"=>0 ) );
    10         if($socket){
    11             $result = @socket_sendto($socket,$buff,strlen($buff),0,$host,$port);
    12             if($waitAckSec>0){
    13                 $result = FALSE;
    14                 $read = array($socket);
    15                 $write = NULL;
    16                 $except = NULL;
    17                 if(@socket_select($read,$write,$except,$waitAckSec)>0){
    18                     $fromHost = "";
    19                     $fromPort = 0;
    20                     @socket_recvfrom($socket,$result,4096,0,$fromHost,$fromPort);    
    21                     $result = phpext_unpack($result);
    22                     if($result["needAck"] == 1){
    23                         $this->sendUdp($host, $port, $result["ackdata"]);
    24                         if(isset ($result['data']['list']) && isset ($result['data']['totalCount'])){
    25                             $list = $result['data']['list'];
    26                             $count = $result['data']['totalCount'];
    27                             while($count>  count($list)){
    28                                 @socket_recvfrom($socket,$result_temp,4096,0,$fromHost,$fromPort);
    29                                 $result_temp = phpext_unpack($result_temp);
    30                                 $this->sendUdp($host, $port, $result_temp["ackdata"]);
    31                                 $list = array_merge($list,$result_temp['data']['list']);
    32                             }
    33                             $result['data']['list'] = $list;
    34                         }
    35                     }else{
    36                         @socket_recvfrom($socket,$result,4096,0,$fromHost,$fromPort);
    37                         $result = phpext_unpack($result);
    38                         if($result["needAck"] == 1){
    39                             $this->sendUdp($host, $port, $result["ackdata"]);
    40                             if(isset ($result['data']['result']) && isset ($result['data']['userID'])){
    41                                 $list = $result['data']['list'];
    42                                 $count = $result['data']['totalCount'];
    43                                 while($count>  count($list)){
    44                                     @socket_recvfrom($socket,$result_temp,4096,0,$fromHost,$fromPort);
    45                                     $result_temp = phpext_unpack($result_temp);
    46                                     $this->sendUdp($host, $port, $result_temp["ackdata"]);
    47                                     $list = array_merge($list,$result_temp['data']['list']);
    48                                 }
    49                                 $result['data']['list'] = $list;
    50                             }
    51                         }
    52                     }
    53                 }else{
    54                     $result = SEND_UDP_ERROR;
    55                 }
    56             }
    57             @socket_close($socket);
    58         }
    59         return $result;
    60     }
    现主要从事PHP、Uinx/Linux、C/C++等方面的项目开发。
  • 相关阅读:
    bootstrap按钮
    bootstrap输入框组
    bootstrap单选
    bootstrap复选框
    bootstrap输入框
    bootstrap浮动
    HO引擎近况20201017
    C#.NET项目中引用EXCEL报错的问题
    MATLAB实例:读取Fashion MNIST数据,保存为.mat文件,并展示部分样例
    MATLAB实例:BP神经网络用于回归任务
  • 原文地址:https://www.cnblogs.com/lsl8966/p/3508278.html
Copyright © 2020-2023  润新知