Tuesday, 26 July 2011

Pinging with options using ASP.NET 2.0 and VB .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 VB.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.

imports System.Net;
imports System.Net.NetworkInformation;
imports System.Text;

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

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 Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
lblStatus.Text = Nothing
txtPing.Text = Nothing

'Ping Options and Parameters
Dim pingopts As PingOptions = New PingOptions()
pingopts.DontFragment = chkFrag.Checked
pingopts.Ttl = Convert.ToInt32(txtTTL.Text)

'create byte array for buffer
Dim buffer As Byte() = Encoding.ASCII.GetBytes(txtBuffer.Text)

Dim ping As Ping = New Ping()
Dim pingreply As PingReply = ping.Send(txtHost.Text, Convert.ToInt32(txtTimeOut.Text), buffer, pingopts)
txtPing.Text &= "Address: " & pingreply.Address.ToString() & Constants.vbCr
txtPing.Text &= "Roundtrip Time: " & pingreply.RoundtripTime.ToString() + Constants.vbCr
txtPing.Text &= "TTL (Time To Live): " & pingreply.Options.Ttl.ToString() + Constants.vbCr
txtPing.Text &= "Buffer Size: " & pingreply.Buffer.Length.ToString() & Constants.vbCr

Catch err As Exception
lblStatus.Text = err.Message
End Try
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

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>

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.

The flow for the code behind page is as follows:

Imports System.Net
Imports System.Net.NetworkInformation
Imports System.Text
Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Try
lblStatus.Text = Nothing
txtPing.Text = Nothing

'Ping Options and Parameters
Dim pingopts As PingOptions = New PingOptions()
pingopts.DontFragment = chkFrag.Checked
pingopts.Ttl = Convert.ToInt32(txtTTL.Text)

'create byte array for buffer
Dim buffer As Byte() = Encoding.ASCII.GetBytes(txtBuffer.Text)

Dim ping As Ping = New Ping()
Dim pingreply As PingReply = ping.Send(txtHost.Text, Convert.ToInt32(txtTimeOut.Text), buffer, pingopts)
txtPing.Text &= "Address: " & pingreply.Address.ToString() & Constants.vbCr
txtPing.Text &= "Roundtrip Time: " & pingreply.RoundtripTime.ToString() + Constants.vbCr
txtPing.Text &= "TTL (Time To Live): " & pingreply.Options.Ttl.ToString() + Constants.vbCr
txtPing.Text &= "Buffer Size: " & pingreply.Buffer.Length.ToString() & Constants.vbCr

Catch err As Exception
lblStatus.Text = err.Message
End Try
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub
End Class

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment