Tuesday, 26 July 2011

Pinging with options using ASP.NET 2.0 and C# .NET


This tutorial will show you how to include ping options when pinging 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 display an external webpage we will need to use the System.IO, System.Net, and System.Text namespaces.

using System.Net;
using System.Net.NetworkInformation;
using System.Text;

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

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. In addition, we define ping options using the PingOptions class.

protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
lblStatus.Text = null;
txtPing.Text = null;

//Ping Options and Parameters
PingOptions pingopts = new PingOptions();
pingopts.DontFragment = chkFrag.Checked;
pingopts.Ttl = Convert.ToInt32(txtTTL.Text);

//create byte array for buffer
byte[] buffer = Encoding.ASCII.GetBytes(txtBuffer.Text);

Ping ping = new Ping();
PingReply pingreply = ping.Send(txtHost.Text,Convert.ToInt32(txtTimeOut.Text),buffer,pingopts);
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 migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!

The front end .aspx page looks something like this:

<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;&nbsp;
</td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">Ping Options:</td>
<td align="center" bgcolor="#FFFFFF">
TTL (Time To Live): &nbsp;<asp:TextBox ID="txtTTL" runat="server" Width="24px"></asp:TextBox>&nbsp;<br />
<asp:CheckBox ID="chkFrag" runat="server" Text="Dont Fragment" />&nbsp;<br />
Buffer:
<asp:TextBox ID="txtBuffer" runat="server" Width="175px"></asp:TextBox>&nbsp;<br />
Timeout:
<asp:TextBox ID="txtTimeOut" runat="server" Width="24px"></asp:TextBox><br />
<asp:button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" /></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>

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

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;
txtPing.Text = null;

//Ping Options and Parameters
PingOptions pingopts = new PingOptions();
pingopts.DontFragment = chkFrag.Checked;
pingopts.Ttl = Convert.ToInt32(txtTTL.Text);

//create byte array for buffer
byte[] buffer = Encoding.ASCII.GetBytes(txtBuffer.Text);

Ping ping = new Ping();
PingReply pingreply = ping.Send(txtHost.Text,Convert.ToInt32(txtTimeOut.Text),buffer,pingopts);
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;
}
}
}

Looking for the VB.NET 2005 Version? Click Here! /p>

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment