Write your own ping tool

xiaoxiao2021-03-06  39

Write your own ping tool aspcool reprint www.aspcool.com Time: 2001-4-11 10:00:25 Readings: 2235

Write your own ping tools: ping is a tool for detecting network connection speeds. The following article will be introduced in C # to use System.Net.Sockets to create a ping tool. -------------------------------------------------- ------------------------------ ping is a tool for detecting network connection speed, it will be given A socket connection is created between the remote host name and sends a packet of ICMP protocol format, and then the remote host responds, send back a packet, and we can determine the connection by calculating the time interval of sending to the received packet. speed. How to use Ping [/ r] Host Name [/ R] optional properties determine if a continuous ping remote host.

Here is the code: ///ping.cs namespace saurabhping {using system; using system.net; using system.net.sockets; ///

/// Main class: ping /// Class PING {// Declaration Several constant const Int socket_error = -1; const ICMP_ECHO = 8; /// /// public static void main (string [] argv ) {IF (argv.Length == 0) {// if User Did Not Enter Any Parameter Inform Him Console.WriteLine ("Usage: ping / r"); console.writeline (" the name of "); console.writeline (" / r ping the host continline ");} else if (argv.Length == 1) {/ just the hostname provthed by the user // call the method "Pinghost" and pass the hostname as a parameter pinghost (argv [0]);} else if (argv.Length == 2) {// The user provided the hostname and the switchiff (argv [1] == "/ R ") {// loop the ping program while (true) {// call the method" pinghost "and pass the Hostna ME as a parameter pinghost;}} else {// if the user provided some other switch pinghost (argv [0]);}}} else {// Some error Occurred Console.writeline ("Error In Arguments ");}} /// /// main method, used to get IP, /// and calculate response time /// public static void pinghost (string host) {// declare the Iphostentry iphostentry serverhe, fromhe; int nbytes = 0; int dwstart = 0, dwstop = 0; // initilize a socket of the type icmp socket socket =

new Socket (AddressFamily.AfINet, SocketType.SockRaw, ProtocolType.ProtICMP); // Get the server endpoint try {serverHE = DNS.GetHostByName (host);} catch (Exception) {Console.WriteLine ( "Host not found"); // fail return;} // Convert the server IP_EndPoint to an EndPoint IPEndPoint ipepServer = new IPEndPoint (serverHE.AddressList [0], 0); EndPoint epServer = (ipepServer); // Set the receiving endpoint to the client machine fromHE = DNS.GetHostByName (DNS.GetHostName ()); IPEndPoint ipEndPointFrom = new IPEndPoint (fromHE.AddressList [0], 0); EndPoint EndPointFrom = (ipEndPointFrom); int PacketSize = 0; IcmpPacket packet = new IcmpPacket (); // Construct The packet to send packet.type = icmp_echo; // 8 packet.subcode = 0; packet.checksum = uint16.parse ("0"); packet.Identifier = uint16.parse ("45"); packet.sequencenumber = uint16 .PARSE ("0"); int pingdata = 32; // sizeof (icmppacket) - 8; packet.data = new byte [pingdata]; // ini Tilize the packet.data for (int i = 0; i

} // now get this critter into a UInt16 array // Get the Half size of the Packet Double double_length = Convert.ToDouble (Index); Double dtemp = Math.Ceil (double_length / 2); int cksum_buffer_length = Convert.ToInt32 (dtemp ); // Create a Byte Array UInt16 [] cksum_buffer = new UInt16 [cksum_buffer_length]; // Code to initialize the Uint16 array int icmp_header_buffer_index = 0; for (int i = 0; i

// loop for checking the time of the server responding while (recd!) {NBytes = socket.ReceiveFrom (ReceiveBuffer, 256, 0, ref EndPointFrom); if (nBytes == SOCKET_ERROR) {Console.WriteLine ( "Host not Responding" ); Recd = true; break;} else if (nbytes> 0) {dwstop = system.environment.tickcount - dwstart; // stop timing console.writeline ("reply from" epserver.tostring () "in" DWSTOP "MS: BYtes Received" nbytes); recd = true; break;} timeout = system.environment; if (timeout> 1000) {console.writeline ("time out"); recd = true;} } // close the socket socket.close ();} ///

/// this method get the packet and calculats the total size /// of the pack by communication it to byte array /// Public Static Int32 Serialize (ICMPPACKET PACKET, BYTE [] Buffer, INT32 Packetsize, INT32 PingData) {INT32 CBRETURN = 0; // Serialize The Struct Into the array int index = 0; BYTE [] B_TYPE = New Byte [1]; B_Type [0] = (packet.type); byte [] b_code = new byte [1]; b_code [0] = (packet.subcode); byte [] b_cksum = bitconverter .Getbytes (packet.checksum); byte [] b_id = bitconverter.get.IDentifier; Byte [] B_seq ​​= bitconverter.getbytes (Packet.SequenceNumber); // console.writeline ("Serialize Type"; array. Copy (B_Type, 0, Buffer, INDEX, B_TYPE.LENGTH); Index = B_Type.Length; // Console.Writeline ("Serialize Code"); Array.copy (B_Code, 0, Buffer, INDEX, B_CODE.LENGTH) INDEX =

转载请注明原文地址:https://www.9cbs.com/read-72597.html

New Post(0)