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     }

 

转载于:https://www.cnblogs.com/lsl8966/p/3508278.html

你可能感兴趣的:(php,网络)