Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ping in c#

Tags:

c#

ping

When I Ping a remote system with windows it says there is no reply, but when I ping with c# it says success. Windows is correct, the device is not connected. Why is my code able to successfully ping when Windows is not?

Here is my code :

Ping p1 = new Ping(); PingReply PR = p1.Send("192.168.2.18"); // check when the ping is not success while (!PR.Status.ToString().Equals("Success")) {     Console.WriteLine(PR.Status.ToString());     PR = p1.Send("192.168.2.18"); } // check after the ping is n success while (PR.Status.ToString().Equals("Success")) {     Console.WriteLine(PR.Status.ToString());     PR = p1.Send("192.168.2.18"); } 
like image 898
Black Star Avatar asked Aug 03 '12 18:08

Black Star


People also ask

What is C in ping command?

-c. Specifies the number of packets. This option is useful when you get an IP trace log. You can capture a minimum of ping packets.

What is use of ping command?

ping is the primary TCP/IP command used to troubleshoot connectivity, reachability, and name resolution. Used without parameters, this command displays Help content. You can also use this command to test both the computer name and the IP address of the computer.


2 Answers

using System.Net.NetworkInformation;      public static bool PingHost(string nameOrAddress) {     bool pingable = false;     Ping pinger = null;      try     {         pinger = new Ping();         PingReply reply = pinger.Send(nameOrAddress);         pingable = reply.Status == IPStatus.Success;     }     catch (PingException)     {         // Discard PingExceptions and return false;     }     finally     {         if (pinger != null)         {             pinger.Dispose();         }     }      return pingable; } 
like image 192
JamieSee Avatar answered Oct 01 '22 18:10

JamieSee


Using ping in C# is achieved by using the method Ping.Send(System.Net.IPAddress), which runs a ping request to the provided (valid) IP address or URL and gets a response which is called an Internet Control Message Protocol (ICMP) Packet. The packet contains a header of 20 bytes which contains the response data from the server which received the ping request. The .Net framework System.Net.NetworkInformation namespace contains a class called PingReply that has properties designed to translate the ICMP response and deliver useful information about the pinged server such as:

  • IPStatus: Gets the address of the host that sends the Internet Control Message Protocol (ICMP) echo reply.
  • IPAddress: Gets the number of milliseconds taken to send an Internet Control Message Protocol (ICMP) echo request and receive the corresponding ICMP echo reply message.
  • RoundtripTime (System.Int64): Gets the options used to transmit the reply to an Internet Control Message Protocol (ICMP) echo request.
  • PingOptions (System.Byte[]): Gets the buffer of data received in an Internet Control Message Protocol (ICMP) echo reply message.

The following is a simple example using WinForms to demonstrate how ping works in c#. By providing a valid IP address in textBox1 and clicking button1, we are creating an instance of the Ping class, a local variable PingReply, and a string to store the IP or URL address. We assign PingReply to the ping Send method, then we inspect if the request was successful by comparing the status of the reply to the property IPAddress.Success status. Finally, we extract from PingReply the information we need to display for the user, which is described above.

    using System;     using System.Net.NetworkInformation;     using System.Windows.Forms;      namespace PingTest1     {         public partial class Form1 : Form         {             public Form1()             {                 InitializeComponent();             }              private void button1_Click(object sender, EventArgs e)             {                 Ping p = new Ping();                 PingReply r;                 string s;                 s = textBox1.Text;                 r = p.Send(s);                  if (r.Status == IPStatus.Success)                 {                     lblResult.Text = "Ping to " + s.ToString() + "[" + r.Address.ToString() + "]" + " Successful"                        + " Response delay = " + r.RoundtripTime.ToString() + " ms" + "\n";                 }             }              private void textBox1_Validated(object sender, EventArgs e)             {                 if (string.IsNullOrWhiteSpace(textBox1.Text) || textBox1.Text == "")                 {                     MessageBox.Show("Please use valid IP or web address!!");                 }             }         }     } 
like image 34
Ashraf Sada Avatar answered Oct 01 '22 19:10

Ashraf Sada