C # Ping utility

Source: Internet
Author: User
Tags socket error

Ping is a veryuseful utility used to determine the speed of a network connection. itestablishes a socket connection with the given hostname and sends a data packetusing ICMP protocol. the host then in reply send back a packet. the time takento send and receive a data packet is calculated in milliseconds. this helps usto know the speed of the connection.

Usage:
Ping Where:
<Hostname>-is the host address of the server to ping
/R-optional switch to ping continuously to the hostname

Download the code (ping.zip)

Code:
1)Ping. CS:-The Ping utility

Namespace saurabhping
{
Using system;
Using system. net;
Using system. net. Sockets;
/// <Summary>
/// The main Ping class
/// </Summary>
Class Ping
{
// Declare some constant variables
Const int socket_error =-1;
Const int ICMP_Echo = 8;
/// <Summary>
/// The starting point of the class
/// It takes the hostname Parameter
/// </Summary>
Public static void main (string [] argv)
{
If (argv. Length = 0)
{
// If user did not enter any parameter inform him
Console. writeline ("Usage: Ping Console. writeline ("Console. writeline ("/R ping the host continuously ");
}
Else if (argv. Length = 1)
{
// Just the hostname provided 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 switch
If (argv [1] = "/R ")
{
// Loop the Ping program
While (true)
{
// Call the method "pinghost" and pass the hostname as a parameter
Pinghost (argv [0]);
}
}
Else
{
// If the user provided some other switch
Pinghost (argv [0]);
}
}
Else
{
// Some error occurred
Console. writeline ("error in arguments ");
}
}

/// <Summary>
/// This method takes the "hostname" of the server
/// And then it Ping's IT and shows the response time
/// </Summary>
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 processing ing 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];
// Initilize the packet. Data
For (INT I = 0; I <pingdata; I ++)
{
Packet. Data [I] = (byte )'#';
}

// Variable to hold the total packet size
Packetsize = pingdata + 8;
Byte [] icmp_pkt_buffer = new byte [packetsize];
Int32 Index = 0;
// Call a method serialize which counts
// The total number of bytes in the packet
Index = serialize (
Packet,
Icmp_pkt_buffer,
Packetsize,
Pingdata );
// Error in packet size
If (Index =-1)
{
Console. writeline ("error in making packet ");
Return;
}

// Now get this critter into a uint16 Array
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 <cksum_buffer_length; I ++ ){
Cksum_buffer [I] =
Bitconverter. touint16 (icmp_pkt_buffer, icmp_header_buffer_index );
Icmp_header_buffer_index + = 2;
}
// Call a method which will return a checksum
Uint16 u_cksum = checksum (cksum_buffer, cksum_buffer_length );
// Save the checksum to the packet
Packet. checksum = u_cksum;

// Get the half size of the packet

// Now that we have the checksum, serialize the packet again
Byte [] sendbuf = new byte [packetsize];
// Again check the packet size
Index = serialize (
Packet,
Sendbuf,
Packetsize,
Pingdata );
// If there is a error report it
If (Index =-1)
{
Console. writeline ("error in making packet ");
Return;
}

Dwstart = system. environment. tickcount; // start timing
// Send the pack over the socket
If (nbytes = socket. sendto (sendbuf, packetsize, 0, epserver) = socket_error)
{
Console. writeline ("socket error cannot send packet ");
}
// Initialize the buffers. The receive buffer is the size of
// ICMP header plus the IP header (20 bytes)
Byte [] receivebuffer = new byte [1, 256];
Nbytes = 0;
// Receive the bytes
Bool recd = false;
Int timeout = 0;

// Loop for checking the time of the server responding
While (! Recd)
{
Nbytes = socket. Receive
From (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. tickcount-dwstart;
If (timeout> 1000)
{
Console. writeline ("time out ");
Recd = true;
}
}

// Close the socket
Socket. Close ();
}
/// <Summary>
/// This method get the packet and calculates the total size
/// Of the pack by converting it to byte array
/// </Summary>
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. getbytes (packet. 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 + = B _code.length;

// Console. writeline ("serialize cksum ");
Array. Copy (B _cksum, 0, buffer, index, B _cksum.length );
Index + = B _cksum.length;

// Console. writeline ("serialize ID ");
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 the 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>
/// This method has the algorithm to make a checksum
/// </Summary>
Public static uint16 checksum (uint16 [] buffer, int size)
{
Int32 cksum = 0;
Int counter;
Counter = 0;

While (size> 0 ){
Uint16 val = buffer [Counter];

Cksum + = convert. toint32 (buffer [Counter]);
Counter + = 1;
Size-= 1;
}

Cksum = (cksum> 16) + (cksum & 0 xFFFF );
Cksum + = (cksum> 16 );
Return (uint16 )(~ Cksum );
}
} // Class ping
/// <Summary>
/// Class that holds the Pack information
/// </Summary>
Public class icmppacket
{
Public byte type; // type of message
Public byte subcode; // type of sub code
Public uint16 checksum; // ones complement checksum of struct
Public uint16 identifier; // identifier
Public uint16 sequencenumber; // sequence number
Public byte [] data;

} // Class icmppacket
}

Related Article

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.