SergkeiM
SergkeiM

Reputation: 4168

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

Upvotes: 0

Views: 3098

Answers (2)

Brightshine
Brightshine

Reputation: 965

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 .

Upvotes: 1

sivann
sivann

Reputation: 2131

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.

Upvotes: 1

Related Questions