A Ping Command similar to WindowsProgram.
Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. net;
Using system. net. Sockets;
Namespace Ping
{
/// <Summary>
/// Summary of form1.
/// </Summary>
///
Public class form1: system. Windows. Forms. Form
{
Private system. Windows. Forms. Label label1;
Private system. Windows. Forms. textbox textbox1;
Private system. Windows. Forms. Button button1;
Private system. Windows. Forms. RichTextBox richtextbox1;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private system. componentmodel. Container components = NULL;
Public form1 ()
{
//
// Required for Windows Form Designer support
//
Initializecomponent ();
//
// Todo: add Any constructor after initializecomponent callsCode
//
}
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void dispose (bool disposing)
{
If (disposing)
{
If (components! = NULL)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. label1 = new system. Windows. Forms. Label ();
This. textbox1 = new system. Windows. Forms. Textbox ();
This. button1 = new system. Windows. Forms. Button ();
This. richtextbox1 = new system. Windows. Forms. RichTextBox ();
This. suspendlayout ();
//
// Label1
//
This. label1.location = new system. Drawing. Point (8, 16 );
This. label1.name = "label1 ";
This. label1.size = new system. Drawing. Size (80, 16 );
This. label1.tabindex = 0;
This. label1.text = "Target Host Name :";
//
// Textbox1
//
This. textbox1.location = new system. Drawing. Point (96, 16 );
This. textbox1.name = "textbox1 ";
This. textbox1.size = new system. Drawing. Size (152, 21 );
This. textbox1.tabindex = 1;
This. textbox1.text = "127.0.0.1 ";
//
// Button1
//
This. button1.location = new system. Drawing. Point (256, 16 );
This. button1.name = "button1 ";
This. button1.size = new system. Drawing. Size (96, 23 );
This. button1.tabindex = 2;
This. button1.text = "ping ";
This. button1.click + = new system. eventhandler (this. button#click );
//
// Richtextbox1
//
This. richtextbox1.location = new system. Drawing. Point (8, 48 );
This. richtextbox1.name = "richtextbox1 ";
This. richtextbox1.size = new system. Drawing. Size (344,184 );
This. richtextbox1.tabindex = 3;
This. richtextbox1.text = "";
//
// Form1
//
This. autoscalebasesize = new system. Drawing. Size (6, 14 );
This. clientsize = new system. Drawing. Size (360,238 );
This. Controls. Add (this. richtextbox1 );
This. Controls. Add (this. button1 );
This. Controls. Add (this. textbox1 );
This. Controls. Add (this. label1 );
This. maximizebox = false;
This. Name = "form1 ";
This. startposition = system. Windows. Forms. formstartposition. centerscreen;
This. Text = "show ping ";
This. resumelayout (false );
}
# Endregion
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main ()
{
Application. Run (New form1 ());
}
// Declare a constant
Const int socket_error =-1;
Const int ICMP_Echo = 8;
Public static int32 serialize (icmppacket packet, byte [] buffer, int32 packetsize, int32 pingdata)
{// Get the packet content, convert it to a byte array, and then calculate the packet length
Int32 cbreturn = 0;
// Convert the datagram structure to an 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. getbytes (packet. identifier );
Byte [] B _seq = bitconverter. getbytes (packet. sequencenumber );
Array. Copy (B _type, 0, buffer, index, B _type.length );
Index + = B _type.length;
Array. Copy (B _code, 0, buffer, index, B _code.length );
Index + = B _code.length;
Array. Copy (B _cksum, 0, buffer, index, B _cksum.length );
Index + = B _cksum.length;
Array. Copy (B _id, 0, buffer, index, B _id.length );
Index + = B _id.length;
Array. Copy (B _seq, 0, buffer, index, B _seq.length );
Index + = B _seq.length;
// Copy data
Array. Copy (packet. Data, 0, buffer, index, pingdata );
Index + = pingdata;
If (index! = Packetsize/* sizeof (icmppacket )*/)
{
Cbreturn =-1;
Return cbreturn;
}
Cbreturn = index;
Return cbreturn;
}
/// <Summary>
/// Checksum Algorithm
/// </Summary>
Public static uint16 checksum (uint16 [] buffer, int size)
{
Int32 cksum = 0;
Int counter;
Counter = 0;
/* Accumulate the ICMP header binary data in 2 bytes */
While (size> 0)
{
Uint16 val = buffer [Counter];
Cksum + = convert. toint32 (buffer [Counter]);
Counter + = 1;
Size-= 1;
}
/* If the ICMP header is an odd number of bytes, the last byte is left. Consider the last byte as
* The high byte of the 2-byte data. The low byte of the 2-byte data is 0. continue to accumulate */
Cksum = (cksum> 16) + (cksum & 0 xFFFF );
Cksum + = (cksum> 16 );
Return (uint16 )(~ Cksum );
}
Private void button#click (Object sender, system. eventargs E)
{// Ping
This. richtextbox1.text = "";
For (INT p = 0; P <10; P ++)
{
Try
{
String hostname = This. textbox1.text;
Int nbytes = 0;
Int dwstart = 0, dwstop = 0;
// Initialize an ICMP socket
Socket socket = new socket (addressfamily. InterNetwork, sockettype. Raw, protocoltype. ICMP );
// Obtain the host name of the target host
Iphostentry serverhe = DNS. gethostbyname (hostname );
Ipendpoint ipepserver = new ipendpoint (serverhe. Addresslist [0], 0 );
Endpoint epserver = (ipepserver );
Iphostentry fromhe = DNS. gethostbyname (DNS. gethostname ());
Ipendpoint ipendpointfrom = new ipendpoint (fromhe. Addresslist [0], 0 );
Endpoint endpointfrom = (ipendpointfrom );
Int packetsize = 0;
Icmppacket packet = new icmppacket ();
// Construct a Datagram
Packet. type = ICMP_Echo;
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];
// Initialize packet. Data
For (INT I = 0; I <pingdata; I ++)
{
Packet. Data [I] = (byte )'#';
}
// Save the length of the datagram
Packetsize = pingdata + 8;
Byte [] icmp_pkt_buffer = new byte [packetsize];
Int32 Index = 0;
// Call the serialize Method
// Total number of bytes
Index = serialize (
Packet,
Icmp_pkt_buffer,
Packetsize,
Pingdata );
If (Index =-1)
{
This. richtextbox1.text + = "The message size is incorrect! \ N ";
Return;
}
// Convert it to an array of the uint16 type and obtain half of the datagram Length
Double double_length = convert. todouble (INDEX );
Double dtemp = math. Ceiling (double_length/2 );
Int cksum_buffer_length = convert. toint32 (dtemp );
// Generate a byte array
Uint16 [] cksum_buffer = new uint16 [cksum_buffer_length];
// Initialize the uint16 type array
Int icmp_header_buffer_index = 0;
For (INT I = 0; I <cksum_buffer_length; I ++)
{
Cksum_buffer [I] =
Bitconverter. touint16 (icmp_pkt_buffer, icmp_header_buffer_index );
Icmp_header_buffer_index + = 2;
}
// Call checksum to return the check and
Uint16 u_cksum = checksum (cksum_buffer, cksum_buffer_length );
// Check and message
Packet. checksum = u_cksum;
Byte [] sendbuf = new byte [packetsize];
// Check the packet size again
Index = serialize (
Packet,
Sendbuf,
Packetsize,
Pingdata );
// Report an error if any.
If (Index =-1)
{
This. richtextbox1.text + = "The message size is incorrect! \ N ";
Return;
}
Dwstart = system. environment. tickcount; // start time
// Send a datagram using Socket
If (nbytes = socket. sendto (sendbuf, packetsize, 0, epserver) = socket_error)
{
This. richtextbox1.text + = "data packets cannot be sent! \ N ";
}
// Initialize the buffer. Accept the buffer
// ICMP header + IP header (20 bytes)
Byte [] receivebuffer = new byte [1, 256];
Nbytes = 0;
// Accept byte streams
Bool recd = false;
Int timeout = 0;
// Cyclically check the response time of the target host
While (! Recd)
{
Nbytes = socket. receivefrom (receivebuffer, 256, 0, ref endpointfrom );
If (nbytes = socket_error)
{
This. richtextbox1.text + = "the target host has no response! \ N ";
Recd = true;
Break;
}
Else if (nbytes> 0)
{
Dwstop = system. environment. tickcount-dwstart;
This. richtextbox1.text + = "data from host:" + this. textbox1.text + ", number of received Bytes:" + nbytes + ", time consumed:" + dwstop + "Ms \ n ";
Recd = true;
Break;
}
Timeout = system. environment. tickcount-dwstart;
If (timeout> 1000)
{
This. richtextbox1.text + = "connection timeout! \ N ";
Recd = true;
}
}
// Close the socket
Socket. Close ();
}
Catch (exception ERR)
{
MessageBox. Show ("failed to ping the target host! Error message: "+ err. message," message prompt ", messageboxbuttons. OK, messageboxicon. information );
Return;
}
}
}
}
Public class icmppacket
{
Public byte type; // Message Type
Public byte subcode; // subcode type
Public uint16 checksum; // checking and
Public uint16 identifier; // identifier
Public uint16 sequencenumber; // sequence number
Public byte [] data; // data
} // ICMP Packet
}