>  기사  >  백엔드 개발  >  php怎么实现这样的效果,大家来讨论下

php怎么实现这样的效果,大家来讨论下

WBOY
WBOY원래의
2016-06-13 13:31:38811검색

php如何实现这样的效果,大家来讨论下。
检测是否能ping通201.168.56.17 。如果可以ping通。。echo (延迟是多少)
如果ping不通 》返回 echo("ping return ;")

就是检测对方主机是否运行。。。

大牛来指点一二。新手来学习一下。大家一起讨论下啦

------解决方案--------------------
搜到一段代码:

PHP code
<?php $host = '192.168.1.1'; //要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<br />';
}

echo 'Pinging '.$host.' ['.gethostbyname($host).'] with Port:'.$port.' of data:<br><br>'."\r\n";  
for($i = 0;$i 
<br><font color="#e78608">------解决方案--------------------</font><br>用 php 中的 exec() 调用命令来实现  最好。
<br><font color="#e78608">------解决方案--------------------</font><br>$ip   =   "192.168.1.1";  <br>exec( "ping   $ip ",   $arr);  <br><br>print_r($arr);  <br> <div class="clear">
                 
              
              
        
            </div>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.