作业帮 > PHP > 教育资讯

PHP判断端口是否打开的代码

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/13 06:23:02 PHP
PHP判断端口是否打开的代码
PHP判断端口是否打开的代码PHP
【BK网络学院 - PHP】
以下是PHP判断端口是否打开的代码:

$host = 'www.baidu'; //要ping的地址,也可以是IP
$port = '80'; //要ping的端口
$num = 3;
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function ping($host,$port)
{
$time_start = microtime_float();
$ip = gethostbyname($host);
$fp = @fsockopen($host,$port,&$errno,&$errstr,1);
if(!$fp) return 'replay time out!';
$get = "GET / HTTP/1.1\r\nHost:".$host."\r\nConnection: Close\r\n\r\n";
@fputs($fp,$get);
@fclose($fp);
$time_end = microtime_float();
$time = $time_end - $time_start;
$time = ceil($time * 1000);
return 'Reply from '.$ip.': time='.$time.'ms
';
}
echo 'Pinging '.$host.' ['.gethostbyname($host).'] with Port:'.$port.' of data:

'."\r\n";
for($i = 0;$i < $num;$i++)
{
ping($host,$port);
sleep(1);
ob_flush();
flush();
}
?>

PHP