简体   繁体   中英

PHP ping IP:Port

I want to Ping a server based on IP and Port. Why port? Because the system that I am building will have Login Server and Game server on different PORTS Right Now I am using fsockopen

        $start = microtime(true);
        $fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
        if (!$fp) {
            return false;
        }
        else {
            $ping = microtime(true) - $start;
            $ping = round($ping * 1000);
            $res['ip'] = $host;
            $res['port'] = $port;
            $res['ping'] = $ping;
            $res['msg'] = 'success';
            return $res;
        }

I cant and I don't wan't to use exec() 1. The IP and Port will be inputed by user and exec() cant ping on port.

with fsockopen I can take only latency (that is not so correct).

If anybody know how cant I get Latensy, Packets send|recived, Jitter and etc.

Maybe some sort of 3rd party API for PHP Ping. Thank you in advance

You must use socket_create() since ping uses ICMP and fsockopen is only for TCP and UDP. ICMP echo packets do not specify any port. In the socket_create manual, in the user comments there are several ping() example implementations.

You can check ICMP packet format in RFC 792

While ICMP is layer3 protocol, but port number is defined in layer4. So you never find port number in ICMP packet

That means, you can get latency between two host(IP) , but you can't get latency between IP:ports .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM