How to Ping a Server in ASP.NET
by GetCodeSnippet.com • May 20, 2013 • Microsoft .NET C# (C-Sharp), Microsoft ASP.NET, Microsoft VB.NET • 0 Comments
Your obvious choice is to check the server which you want to connect, is up and running. The following piece of code is a simple way to ping your desire server in your ASP.NET application. You can use it to check the server periodically and make your choice according to server status.
Let’s see the code. You can also download the code at the bottom.
C# Code to Ping Server
1 2 3 4 5 6 7 8 9 10 11 12 13 |
string address = ""; System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping(); try { System.Net.NetworkInformation.PingReply reply = ping.Send(address); Response.Write("Status: " + reply.Status.ToString() + "<br/>"); Response.Write("RoundTrip Time: " + reply.RoundtripTime.ToString()); } catch (Exception ex) { Response.Write(ex.Message); } |
VB.NET Code to Ping Server
1 2 3 4 5 6 7 8 9 10 11 |
Dim address As String = "" Dim ping As New System.Net.NetworkInformation.Ping() Try Dim reply As System.Net.NetworkInformation.PingReply = ping.Send(address) Response.Write("Status: " & reply.Status.ToString() & "<br/>") Response.Write("RoundTrip Time: " & reply.RoundtripTime.ToString()) Catch ex As Exception Response.Write(ex.Message) End Try |
First we need to create a Ping class object and then we need to call send() method of Ping class providing our desired IP address and assign it to PingReply class object. Now we can get server information by using PingReply class object.