C#.net

Ping IP address using c#.net



I am posting here the code snippet to Ping a given IP address using c#.net

 

C#.net Section   -Sample.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

public partial class _Default : System.Web.UI.Page 
{
protected void Page_Load(object sender, EventArgs e) {  }protected void btnSubmit_Click(object sender, EventArgs e) {
try { lblStatus.Text = null; Ping ping = new Ping(); PingReply pingreply = ping.Send(txtHost.Text); txtPing.Text += "Address: " + pingreply.Address + "\r"; txtPing.Text += "Roundtrip Time: " + pingreply.RoundtripTime + "\r"; txtPing.Text += "TTL (Time To Live): " + pingreply.Options.Ttl + "\r"; txtPing.Text += "Buffer Size: " + pingreply.Buffer.Length.ToString() + "\r"; } catch (Exception err) {
lblStatus.Text = err.Message;
}
}
}

Here is the design section

Sample.aspx

<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr> 
<td width="100" align="right" bgcolor="#eeeeee" class="header1">Hostname/IP:</td> <td align="center" bgcolor="#FFFFFF">  <asp:TextBox ID="txtHost" runat="server"></asp:TextBox> &nbsp;&nbsp; <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" /></td>
</tr> <tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">Ping Results:</td> <td align="center" bgcolor="#FFFFFF"> <asp:TextBox ID="txtPing" runat="server" Height="66px" TextMode="MultiLine" Width="226px"></asp:TextBox>&nbsp;<br /> <asp:label ID="lblStatus" runat="server"></asp:label></td>
</tr>
</table>

Share

Thanks for reading my blog. If you like what I write, why not subscribe to my feed?

If you are busy, I can send the latest post to your email. Just subscribe to my email updates.

 

Enter your email address:

Delivered by FeedBurner

Discussion

No comments for “Ping IP address using c#.net”

Post a comment