Most of the users will be doing ping to a system to check whether it is running or responding or not. First thing what they will do is start a command promt and type the ping command. But doing it in C# is also very easy. We can implement in our programs and use to check whether system are responding. It can be used in various network application
public partial class frmPingSystem : Form { public frmPingSystem(){InitializeComponent();}private void pingButton_Click(object sender, EventArgs e){int timeout = 12000;Ping ping = new Ping();byte[] buffer = Encoding.ASCII.GetBytes("ooooooooooooooooooooooo");PingOptions options = new PingOptions(64,true);AutoResetEvent waiter = new AutoResetEvent(false);ping.PingCompleted += new PingCompletedEventHandler(ping_PingCompleted);ping.SendAsync(addressTextBox.Text, timeout, buffer, options, null);}void ping_PingCompleted(object sender, PingCompletedEventArgs e){string output = "Status : " + e.Reply.Status.ToString();output += "\r\nAddress : " + e.Reply.Address.ToString();output += "\r\nRound Trip Time : " + e.Reply.RoundtripTime.ToString();output += "\r\nBuffer Length : " + e.Reply.Buffer.Length.ToString();output += "\r\nTime to live : " + e.Reply.Options.Ttl.ToString();output += "\r\nDont Framment : " + e.Reply.Options.DontFragment.ToString();resultTextBox.Text = output;} }
If you want to check the result in the same line where you are sending ping then you must use Send function in place SendAsync