C# – Test Remote Computer Reachable by Ping

C# – Test Remote Computer Reachable by Ping

   Ping.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Text;
   using System.Net.NetworkInformation;
 
   namespace NetworkMonitor
   {
      public class Ping
      {
         private string HostName;
         private Notification notify;
         private Library lib;
 
         public Ping(string HostName)
         {
            this.HostName = HostName;
            this.notify = new Notification();
 
            this.lib = new Library();
         }
 
         public void pingTest()
         {
            while (true)
            {
               if (this.IsReachable())
                  Console.WriteLine(this.HostName + " Reachable");
               else
                  Console.WriteLine(this.HostName + " Unreachable");
 
               Console.ReadLine();
            }
         }
 
         public bool IsReachable()
         {
 
            System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
            PingReply reply;
 
            int i = 0;
            //int a = 0;
 
            bool change = false;
 
            while (true)
            {
 
               try
               {
                  /* Testing Script
                  if (i == 2 && a == 0)
                  {
                     hostname = "www.yahoo.com.hk";
                     a++;
                  }
                  else if (a == 1 && i == 2)
                  {
                     hostname = "192.168.0.151";
                  }
                  */
 
                  reply = ping.Send(this.HostName);
                  Console.WriteLine(i + " " + reply.Status + " ");
 
                  if (reply.Status == IPStatus.Success)
                  {
 
                     if (i >= 4)
                     {
                        return true;
                     }
                     else if (!change && i != 0)
                     {
                        i = 0;
                     }
                     else
                     {
                        i++;
                     }
 
                     change = true;
 
                  }
                  else
                  {
 
                     if (i >= 4)
                     {
                        return false;
                     }
                     else if (change && i != 0)
                     {
                        i = 0;
                     }
                     else
                     {
                        i++;
                     }
 
                     change = false;
 
                  }
               }
               catch (Exception ex)
               {
                  Console.WriteLine(i + " " + ex.Message);
 
                  if (i >= 4)
                  {
                     return false;
                  }
                  else if (change && i != 0)
                  {
                     i = 0;
                  }
                  else
                  {
                     i++;
                  }
 
                  change = false;
               }
            }
         }
      }
   }