您的位置 首页 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();
}
 

文章来源:智云一二三科技

文章标题:php模拟ping

文章地址:https://www.zhihuclub.com/153504.shtml

关于作者: 智云科技

热门文章

网站地图