Heim  >  Artikel  >  Backend-Entwicklung  >  php怎么实现这样的效果,大家来讨论下

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

WBOY
WBOYOriginal
2016-06-13 13:31:38809Durchsuche

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>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn