zoukankan      html  css  js  c++  java
  • 试用php的ping命令

    使用PHP自动PING IP,校检网络连接是否正常!

    1. <?php      
    2. $server = 'ping kalvin.cn -n 1';      
    3. $last_line = exec($server, $arr);      
    4. echo "$last_line"; //最后总结结果      
    5. print_r($arr); //PING命令详细数据数组      
    6. ?>  

    国外一位大师使用Sockets Ping,似乎效率更高:

    PHP代码
    1. <?php      
    2.     // Checksum calculation function      
    3.     function icmpChecksum($data)      
    4.     {      
    5.     if (strlen($data)%2)      
    6.     $data .= "/x00";      
    7.           
    8.     $bit = unpack('n*', $data);      
    9.     $sum = array_sum($bit);      
    10.           
    11.     while ($sum >> 16)      
    12.     $sum = ($sum >> 16) + ($sum & 0xffff);      
    13.           
    14.     return pack('n*', ~$sum);      
    15.     }      
    16.     // Making the package      
    17.     $type= "/x08";      
    18.     $code= "/x00";      
    19.     $checksum= "/x00/x00";      
    20.     $identifier = "/x00/x00";      
    21.     $seqNumber = "/x00/x00";      
    22.     $data= "Scarface";      
    23.     $package = $type.$code.$checksum.$identifier.$seqNumber.$data;      
    24.     $checksum = icmpChecksum($package); // Calculate the checksum      
    25.     $package = $type.$code.$checksum.$identifier.$seqNumber.$data;      
    26.     // And off to the sockets      
    27.     $socket = socket_create(AF_INET, SOCK_RAW, 1);      
    28.     socket_connect($socket, "www.google.com", null);      
    29.     // If you're using below PHP 5, see the manual for the microtime_float      
    30.     // function. Instead of just using the m      
    31.     //     icrotime() function.      
    32.     $startTime = microtime(true);      
    33.     socket_send($socket, $package, strLen($package), 0);      
    34.     if (socket_read($socket, 255)) {      
    35.     echo round(microtime(true) - $startTime, 4) .' seconds';      
    36.     }      
    37.     socket_close($socket);      
    38. ?>     

     转自http://blog.csdn.net/gumanren/article/details/5752394

  • 相关阅读:
    Python常用函数
    MySQL常用操作
    Python与JAVA的异同
    token
    用户cookie和会话session、sessionID的关系
    Jenkins应用
    Python3 logging模块
    python 多线程threading模块
    引用的声明周期结束时,并不会调用析构函数,只有本体的声明周期结束时,才会调用析构函数
    行为像指针的类的对象每次作为参数传入函数或者传出函数时都要小心
  • 原文地址:https://www.cnblogs.com/liuwenbohhh/p/4364729.html
Copyright © 2011-2022 走看看