Commands ping unless you do not have anyone present. It is used to diagnose network connectivity. With it you can check, Is there a connection between two hosts, what is the quality of this connection and the delays that occur. Additionally, if the host does not give the address in the form of address IP we can diagnose, whether the names are resolved correctly by the server DNS. The above-mentioned possibilities are sometimes needed during programming systems in the network. Sometimes we want to see, if a host is responsible, what is the quality of the connection to the host, whether we are able to properly resolve the domain name to the address IP. To this end. Net implements the command PING. Created a class with the same name.

Objects of this class is used as easily as built-in system to PING. The basic usage scenario can be written as the following code:

Ping ping = new Ping();
PingReply reply = ping.Send("www.jankowskimichal.pl");
if (reply.Status == IPStatus.Success)
  {
     Console.WriteLine("Address: {0}", reply.Address.ToString());
     Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
     Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
     Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
     Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
  }
else
  {
     Console.WriteLine(reply.Status);

By analyzing the code, we see, that the first test we send the package to the address. In our case, the address of the blog. In response, we get an object PingReply, that holds together all the necessary information, which are then displayed on the console.

An experienced programmer should immediately notice a mistake. While it may not be a known bug, not only efficiently written code. Well, after sending a query to answer it may take some time. In this embodiment, the program waits for the answer, although it could use this time to do other useful things.

In order to improve the performance of this solution, call the command PING in an asynchronous. We should send the command PING. Then move on to other things, and in case of coming back to the command response PING and handle it the way you wanted and send the information that we have finished processing the command PING and we can go to the further implementation of the program. To do this, define a method, which is executed after the coming of response to command PING. In it, you should include code, which has to process the reply received. In the example, this code will only wrote out the basic information:

private void ping_PingCompleted(object sender, PingCompletedEventArgs e)
    {
      //Sprawdzamy, czy operacja nie została przerwana
      if (e.Cancelled)
      {
        System.Console.WriteLine("Ping was cancelled...");
      }
      else
      {
        //Sprawdzamy, czy nie wystąpił błąd
        if (e.Error != null)
        {
          System.Console.WriteLine("An error occurred: " + e.Error);
        }
        else
        {
          //Wyświetlamy odpowiedź
          if (e.Reply != null)
          {
            if (e.Reply.Status == IPStatus.Success)
            {
              System.Console.WriteLine("Reply from " + e.Reply.Address.ToString()
                + ": bytes=" + e.Reply.Buffer.Length
                + " time=" + e.Reply.RoundtripTime
                + " TTL=" + e.Reply.Options.Ttl);
            }
            else
            {
              System.Console.WriteLine("Ping was unsuccessful: " + e.Reply.Status);
            }
          }
          else
          {
            System.Console.WriteLine("There was no response.");
          }
        }
      }
      //Przywrócenie wykonywania wątku głównego
      ((AutoResetEvent)e.UserState).Set();

Having defined a method that supports the response received for command PING We go way to send commands PING. As before, you should start by defining the command PING. In this example, you can see how to configure different options of this command:

  • timeout polecenia,
  • data to be sent with the command PING,
  • TTL command,
  • possibility of packet fragmentation.

The only difference compared to the previous version is the way to send a package. In the former case we used the method Send(…). However, in this method is used SendAsync(…). Just keep in mind, that the packet should define a method, to be executed at the end of the command PING. And also stop the execution of this thread handle until the command PING.

  AutoResetEvent autoResetEvent = new AutoResetEvent(false);

  //Tworzymy obiekt ping
  Ping ping = new Ping();

  //Podpinamy metodę, która ma się wykonać po zakończeniu polecenia ping
  ping.PingCompleted += new PingCompletedEventHandler(ping_PingCompleted);

  //Tworzymy 32 bajty do wysłania
  string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
  byte[] buffer = Encoding.ASCII.GetBytes(data);

  //Ustawiamy timeout na 10 sekund
  int timeout = 10000;

  //Ustawiamy inne opcje:
  //- TTL na 32
  //- brak możliwości fragmentacji pakietu
  PingOptions options = new PingOptions(32, true);

  //Wysyłamy ping
  ping.SendAsync("www.jankowskimichal.pl", timeout, buffer, options, autoResetEvent);

  //Wstrzymujemy główny wątek do momentu otrzymania odpowiedzi
  autoResetEvent.WaitOne();