Bojensen Blogs

Ping an IP address with C#

   1: Ping p = new System.Net.NetworkInformation.Ping();

   2: PingReply r;

   3:  

   4: String s = "10.10.10.10"; //Replace input from your text box for IP address here

   5:  

   6: r = p.Send(s); 

   7:  

   8:                 Response.Buffer = false;

   9:  

  10:                 if (r.Status == IPStatus.Success)

  11:                 {

  12:                     

  13:                     lblpingstatus.Text = "Ping to " + s.ToString() + "[" + r.Address.ToString() + "] successful - " + r.Buffer.Length.ToString() + " bytes in " + r.RoundtripTime.ToString() + " ms." + "n";

  14:  

  15: }

or

   1: using System;

   2: using System.Data;

   3: using System.Configuration;

   4: using System.Web;

   5: using System.Web.Security;

   6: using System.Web.UI;

   7: using System.Web.UI.WebControls;

   8: using System.Web.UI.WebControls.WebParts;

   9: using System.Web.UI.HtmlControls;

  10: using System.Net;

  11: using System.Net.NetworkInformation;

  12: using System.Text;

  13:  

  14: public partial class _Default : System.Web.UI.Page 

  15: {

  16:     protected void Page_Load(object sender, EventArgs e)

  17:     { 

  18:  

  19:     }

  20:     

  21:     protected void btnSubmit_Click(object sender, EventArgs e)

  22:     {

  23:         try {

  24:             lblStatus.Text = null;

  25:             Ping ping = new Ping();

  26:             PingReply pingreply = ping.Send(txtHost.Text);

  27:             txtPing.Text += "Address: " + pingreply.Address + "r";

  28:             txtPing.Text += "Roundtrip Time: " + pingreply.RoundtripTime + "r";

  29:             txtPing.Text += "TTL (Time To Live): " + pingreply.Options.Ttl + "r";

  30:             txtPing.Text += "Buffer Size: " + pingreply.Buffer.Length.ToString() + "r";

  31:         }

  32:         catch (Exception err) {

  33:             lblStatus.Text = err.Message;

  34:         }

  35:     }

  36: }

Comments are closed.