찾다
php教程php手册在PHP中使用socket_raw

class Net_Ping
{
  var $icmp_socket;
  var $request;
  var $request_len;
  var $reply;
  var $errstr;
  var $time;
  var $timer_start_time;
  function Net_Ping()
  {
   $this->icmp_socket = socket_create(AF_INET, SOCK_RAW, 1);
   socket_set_block($this->icmp_socket);
  }
 
  function ip_checksum($data)
  {
     for($i=0;$i     {
         if($data[$i+1]) $bits = unpack('n*',$data[$i].$data[$i+1]);
         else $bits = unpack('C*',$data[$i]);
         $sum += $bits[1];
     }
    
     while ($sum>>16) $sum = ($sum & 0xffff) + ($sum >> 16);
     $checksum = pack('n1',~$sum);
     return $checksum;
  }

  function start_time()
  {
   $this->timer_start_time = microtime();
  }
 
  function get_time($acc=2)
  {
   // format start time
   $start_time = explode (" ", $this->timer_start_time);
   $start_time = $start_time[1] + $start_time[0];
   // get and format end time
   $end_time = explode (" ", microtime());
   $end_time = $end_time[1] + $end_time[0];
   return number_format ($end_time - $start_time, $acc);
  }

  function Build_Packet()
  {
   $data = "abcdefghijklmnopqrstuvwabcdefghi"; // the actual test data
   $type = "\x08"; // 8 echo message; 0 echo reply message
   $code = "\x00"; // always 0 for this program
   $chksm = "\x00\x00"; // generate checksum for icmp request
   $id = "\x00\x00"; // we will have to work with this later
   $sqn = "\x00\x00"; // we will have to work with this later

   // now we need to change the checksum to the real checksum
   $chksm = $this->ip_checksum($type.$code.$chksm.$id.$sqn.$data);

   // now lets build the actual icmp packet
   $this->request = $type.$code.$chksm.$id.$sqn.$data;
   $this->request_len = strlen($this->request);
  }
 
  function Ping($dst_addr,$timeout=5,$percision=3)
  {
   // lets catch dumb people
   if ((int)$timeout    if ((int)$percision   
   // set the timeout
   socket_set_option($this->icmp_socket,
     SOL_SOCKET,  // socket level
     SO_RCVTIMEO, // timeout option
     array(
       "sec"=>$timeout, // Timeout in seconds
       "usec"=>0  // I assume timeout in microseconds
       )
     );

   if ($dst_addr)
   {
     if (@socket_connect($this->icmp_socket, $dst_addr, NULL))
     {
    
     } else {
       $this->errstr = "Cannot connect to $dst_addr";
       return FALSE;
     }
     $this->Build_Packet();
     $this->start_time();
     socket_write($this->icmp_socket, $this->request, $this->request_len);
     if (@socket_recv($this->icmp_socket, &$this->reply, 256, 0))
     {
       $this->time = $this->get_time($percision);
       return $this->time;
     } else {
       $this->errstr = "Timed out";
       return FALSE;
     }
   } else {
     $this->errstr = "Destination address not specified";
     return FALSE;
   }
  }
}

$ping = new Net_Ping;
$ping->ping("www.google.ca");

if ($ping->time)
  echo "Time: ".$ping->time;
else
  echo $ping->errstr;



성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
1 몇 달 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

안전한 시험 브라우저

안전한 시험 브라우저

안전한 시험 브라우저는 온라인 시험을 안전하게 치르기 위한 보안 브라우저 환경입니다. 이 소프트웨어는 모든 컴퓨터를 안전한 워크스테이션으로 바꿔줍니다. 이는 모든 유틸리티에 대한 액세스를 제어하고 학생들이 승인되지 않은 리소스를 사용하는 것을 방지합니다.

SublimeText3 Linux 새 버전

SublimeText3 Linux 새 버전

SublimeText3 Linux 최신 버전

맨티스BT

맨티스BT

Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.

WebStorm Mac 버전

WebStorm Mac 버전

유용한 JavaScript 개발 도구