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!!"); } } } }
Ashraf Abusada Oct 17 '14 at 20:08 2014-10-17 20:08
source share