Using Sockets in PHP

Source: Internet
Author: User
Tags echo message explode strlen unpack

<?php

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 <strlen ($data); $i + + 2)
{
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'll have to work with this later
$SQN = "\x00\x00"; We'll 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 <= 0) $timeout =5;
   if ((int) $percision <= 0) $percision = 3;
  
  //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-&GT;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 ("<a href=" http://www.google.ca "target=_blank>www.google.ca</a>");

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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.