
Navigator : Home > Tutorials > Network Tutorials > ...
Pinging using ASP.NET 2.0 and C# .NET
This tutorial will show you how to ping a hostname/ip using the .NET System.Net class, ASP.NET 2.0 and C#.NET
The .NET Framework offers a number of types that makes accessing resources on the network easy to use.
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
To perform a simple ping, we will need to use the System.Net, System.Net.Network.Information, System.Text namespaces.
using System.Net; using System.Net.NetworkInformation; using System.Text; |
We'll put our code in the btnSubmit_Click() event.
When the btnSubmit_Click() event fires it creates a new Ping object. We can then execute the Send method of this object to send a ping to the host specified in our text box. Executing this method also returns a PingReply object which we can use to gather information such as the Address, Roundtrip Time, TTL, and Buffer Size.
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; } } |
The front end .aspx page looks something like this:
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
<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> <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> <br /> <asp:label ID="lblStatus" runat="server"></asp:label></td> </tr> </table> |
The flow for the code behind page is as follows:
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; } } } |
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
Looking for the VB.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!