How to Ping from asp.net application vb code

This tutorial will show you how to ping 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 use.

To perform a simple ping, we will need to use the System.Net, System.Net.Network.Information, System.Text namespaces.

Its a basic example later i will post some customized application


In your vb code do something like this
Imports System.Net
Imports System.Net.NetworkInformation
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
txtPing.Text = showPing("191.254.186.1")
End Sub
Private Function showPing(ByVal ipaddress As String) As String
Try
Dim ping As Ping = New Ping
Dim pingreply As PingReply = ping.Send(ipaddress)
Return "roundtrip time: " + pingreply.RoundtripTime.ToString
Catch e As Exception
Return e.Message
End Try
End Function
End Class

In your aspx page do this
asp:TextBox ID="txtPing" runat="server" Width="621px"asp:TextBox


You can also customize function using this
pingreply.Options.Ttl.Tostring
pingreply.Buffer.Length.ToString()
Here is my another customized version to get real time network status on asp.net page. Use update panel with timer control to get automatic refresh update:

Imports System.Net
Imports System.Net.NetworkInformation
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lblNet.Text = "Sending Garbage Data.. Updating... " & _
" Web Server : Working Fine " & _
" NTPC Mail: " + showPing("191.254.186.230") & _
" SAP-ESS : " + showPing("10.2.0.112") & _
" SAP-R3 : " + showPing("10.2.0.54") & _
" BSNL Broadband Internet: Unable to get Status If not working then you are requested to use common IT internet kiosk." & _
" MPLS Link (Bhatwari): " + showPing("172.29.176.229") & _
" MPLS Link (Dehradun): " + showPing("172.29.176.230") & _
" MPLS Link (Hyderabad): " + showPing("10.2.0.38") & _
" MPLS Link (Noida): " + showPing("10.0.0.48") & _
" VSAT Noida: " + showPing("191.254.125.133") & _
"

The screen refreshes each 10 second. Please do have patience till services get restored. We are already working on it.
"

End Sub
Private Function showPing(ByVal ipaddress As String) As String
Try
Dim ping As Ping = New Ping
Dim garbage As Byte()
Dim enc As System.Text.ASCIIEncoding = New System.Text.ASCIIEncoding
garbage = enc.GetBytes("vinodkotiyaworkinginntpclimited hasdesignedthismoduletogetthepingresponceofvariousserverandroutersandapplicationstogettherealtimedataandtosatisfythisbloodyuserswhofrequentlyusedtocallmewheneversomethinggetsdown
")
' garbage is used to get responce time of more than 1 ms
Dim pingreply As PingReply = ping.Send(ipaddress, 2048, garbage)
If Int(pingreply.RoundtripTime.ToString) > 0 Then
Return " Working Fine with Responce Time of " + pingreply.RoundtripTime.ToString + " mili second for " + ipaddress
Else
Return "Currently Down Due to network problem. Please Have Patience. Restoration work initiated. " + pingreply.RoundtripTime.ToString + " mili second for " + ipaddress
End If
Catch e As Exception
Return e.Message
End Try
End Function
End Class
asp:Label ID="lblNet" runat="server" Width="621px"

Comments