How to set the timeout for php socket

coldplay.xixi
Release: 2023-03-05 19:54:02
original
3355 people have browsed it

php socket setting timeout method: [socket_set_option($socket,SOL_SOCKET,SO_SNDTIMEO,array("sec"=>6, "usec"=>0))].

How to set the timeout for php socket

[Related learning recommendations: php programming (video)]

How to set timeout for php socket:

How to create socket and set timeout in php, post it and share it

//如果$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;
    }
Copy after login

If you want to know more about programming learning, please pay attention to the php training column!

The above is the detailed content of How to set the timeout for php socket. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Latest issues
Popular Tutorials
More>
Latest downloads
More>
web effects
Website source code
Website materials
Front end template
About us Disclaimer Sitemap
PHP Chinese website:Public welfare online PHP training,Help PHP learners grow quickly!