07 November 2010

Ping a Server from .NET

Everyone will know, what is PING? But ever thoughts from where this word comes and what it means. As we all know Ping is an acronym for the words 'Packet Internet Groper'. The name comes from active sonar terminology. Submarine - Navy uses this method to when they looking for objects under the sea. That means send out sonar waves and then waits for a return wave when it bounces off something, such as another sub, whale, ocean floor etc. This, in turn, was adopted from bats and dolphins! The “PING” is the sound that sonar makes!
Ping is a computer network administration utility used to test the reach ability of a host on an Internet Protocol (IP) network and to measure the round-trip time for messages sent from the originating host to a destination computer. A small packet is sent through the network to a particular IP address. This packet contains 64 bytes - 56 data bytes and 8 bytes of protocol reader information. The computer will send the packet and then waits for a return packet. If the connections are good and the target computer is up, a good return packet will be received.
PING is also a useful utility for programmers. Often it is a good practice to see if an endpoint is "up" before attempting to send a message. Also, it can be very useful to estimate the response time from the host. Generally if the server is available, it sends the response immediately. We have a requirement in our application where we have thick client in .net and need to talk to another application through web url. Here by sharing the code samples.
public bool  Ping()
{
   using (Ping pingSender = new Ping())
   {
     PingOptions pingOptions = null;
     bool pingResults = false;
     PingReply pingReply = null;
     IPAddress ipAddress = null;
     int numberOfPings = 4, pingTimeout = 1000, byteSize = 32, sentPings = 0;
     byte[] buffer = new byte[byteSize];
     string ipAddressString = "10.22.24.174";
     pingOptions = new PingOptions();
     ipAddress = IPAddress.Parse(ipAddressString);
     for (int i = 0; i < numberOfPings; i++)
     {
       sentPings++;
       pingReply = pingSender.Send(ipAddress, pingTimeout, buffer, pingOptions);
        if (pingReply.Status == IPStatus.Success)
        {
           pingResults = true;
        }
      }
    return pingResults;
   }
}
The PING sends an ICMP (Internet Control Message Protocol) echo request to the server and waits to receive the echo back. If the value of Status is success, the PingReply is successful.
Also in few case may be server will be up and may be we have to perticulerly ping the url, in that case we can go ahead and use the below code:
public bool IsUrlAvailable(string url)
{
  try
  {
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    using (HttpWebResponse rsp = (HttpWebResponse)req.GetResponse())
    {
      if (rsp.StatusCode == HttpStatusCode.OK)
      {
         return true;
      }
     }
    }
    catch (WebException)
    {
        // Eat it because all we want to do is return false
    }
    return false;
}