首页 编程语言 php

php模拟ping

效果图:

代码:

<?php
$host = 'item.taobao.com'; //ping的地址,也可以是IP
$port = '80'; //ping的端口
$num = 3;
echo 'Pinging ' . $host . ' [' . gethostbyname($host) . '] with Port:' . $port . ' of data:<br /><br />' . "\r\n";
for ($i = 0; $i < $num; $i++) {
 // list($usec, $sec) = explode(" ", microtime());
 $time_start = microtime(1); // ((float) $usec + (float) $sec);
 $ip = gethostbyname($host);
 // $fp = @fsockopen($host, $port);
 $fp = @fsockopen($host, $port, $errno, $errstr, 1);
 if (!$fp) {
 echo 'replay time out!<br />';
 continue;
 }
 $get = "GET / HTTP/1.1\r\nHost:" . $host . "\r\nConnection: Close\r\n\r\n";
 @fputs($fp, $get);
 @fclose($fp);
 // list($usec, $sec) = explode(" ", microtime());
 $time_end = microtime(1); //((float) $usec + (float) $sec);
 $time = $time_end - $time_start;
 $time = ceil($time * 1000);
 echo 'Reply from ' . $ip . ': time=' . $time . 'ms<br />';
 sleep(1);
 ob_flush();
 flush();
}
相关推荐