日期:2011-09-07  浏览次数:20550 次

因为朋友说端口连接不上,特意试了一下用php做Ping测试,结果还是挺满意的。

代码和运行图如下:(如果有大神改进就更好了)

\

<?php

$host = 'www.php100.com';

$port = '80';

$num = 3; //Ping次数

//获取时间

function mt_f (){

list($usec,$sec) = explode(" ",microtime());

return ((float)$usec + (float)$sec); //微秒加秒

}

function ping_f($host,$port){

$time_s = mt_f();

$ip = gethostbyname($host);

$fp = @fsockopen($host,$port);

if(!$fp)

return 'reply time out!';

$get = "GET / HTTP/1.1\r\nHost:".$host."\r\nConnect:".$port."Close\r\n";

fputs($fp,$get);

fclose($fp);

$time_e = mt_f();

$time = $time_e - $time_s;

$time = ceil($time * 1000);

return 'reply from '.$ip.':'.$port.' time = '.$time.'ms<br />';

}

echo 'ping to '.$host.' ['.gethostbyname($host).'] with port:'.$port.' of data:<br />';

for($i = 0;$i <