Using Socket in PHP

xiaoxiao2021-03-06  38

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 > 16) $ sum = ($ sum & 0xfff) ($ 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 <= 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-> errstr = "cannot connect to $ dst_addr"; return false;} $ this-> build_packet (); $ 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"; returnaf false;}}}

$ ping = new net_ping; $ ping-> ping ("

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


New Post(0)