Using ping in c #

When I ping a remote system with windows, she says that there is no answer, but when I ping with C #, he talks about success. Windows is correct, the device is not connected. Why can my code successfully ping when Windows is not working?

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"); } 
+80
c # ping
Aug 03 2018-12-18T00:
source share
3 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; } 
+197
Aug 03 2018-12-12T00:
source share

The use of ping in C # is achieved using the Ping.Send(System.Net.IPAddress) method Ping.Send(System.Net.IPAddress) , which executes a ping request to the provided valid IP address or URL and receives a response called the Internet Messaging Protocol (ICMP) Packet which contains a 20-byte header that contains response data from a verified server that received a connection check request, the .Net framework System.Net.NetworkInformation contains a class called PingReply Class, which has properties designed to convert an ICMP response and deliver Useful pinged server network information, such as:

  • IPStatus : Gets the address of the host that sends the Internet echo reply using the Control Message Protocol (ICMP).
  • IP Address : Gets the number of milliseconds spent sending the Internet. The Control Message Protocol (ICMP) echo request and the corresponding ICMP echo reply message.
  • RoundtripTime (System.Int64): Gets the parameters used to transmit the response to the ICMP echo protocol. inquiry.
  • PingOptions (System.Byte []): Gets a buffer of data received in a response echo message using the Internet Control Message Protocol (ICMP).

The following is a simple example of using WinForms to demonstrate how ping works in c #. By providing a valid IP address in textBox1 and pressing button1 , we instantiate the Ping class , the local variable PingReply . ], a string of a local variable to store the IP or URL, then we assign the local variable PingReply that we created for the ping Send method, and then check whether the request was successful by comparing the response status with the IPAddress.Success status property, then we extract from the local PingReply variable information that should be displayed to the user, as 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!!"); } } } } 
+31
Oct 17 '14 at 20:08
source share
 private void button26_Click(object sender, EventArgs e) { System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo(); proc.FileName = @"C:\windows\system32\cmd.exe"; proc.Arguments = "/c ping -t " + tx1.Text + " "; System.Diagnostics.Process.Start(proc); tx1.Focus(); } private void button27_Click(object sender, EventArgs e) { System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo(); proc.FileName = @"C:\windows\system32\cmd.exe"; proc.Arguments = "/c ping " + tx2.Text + " "; System.Diagnostics.Process.Start(proc); tx2.Focus(); } 
-7
Nov 03 '16 at 19:51
source share



All Articles