Thursday 12 July 2012

.Net Tips : Ping an IP Address or Website using C# Examples and VB.Net Examples

Ping message is sent using the ICMP using System.Net.NetworkInformation.Ping class.
Ping Message passing a test packet to specific IP Address, and requests the remote device respond by send back the packet.
In Ping Message a byte array of up to 65,500 data bytes that is sent with the ping request and that should be returned back in the response.
You can send ping using "send" method of Ping class. In the reply of the send method will return a System.Net.NetworkInformation.PingReply object. The Status property of the PingReply object will contain a value of the Status of the ping request.The most common values will be Success and TimedOut. You can set TimeOut value in Send method in milliseconds.
If you sepcify Invalid host or Ping request did not found given host at that time Send method raise Exception "An exception occurred during a Ping request." .You need to handle this exception in Try Catch block.

Here is example of this.
In this example we sent ping to specific IP Address or Website and display it's result on screen.
We send ping request to "www.google.com" and it's success message on screen with it's IP address and time taken by ping request.

ASPX Code :
<asp:TextBox runat="server" ID="txtURL"></asp:TextBox>
<asp:Button runat="server" ID="btnPing" Text="Ping" onclick="btnPing_Click" />
<br />
<b>Status : </b><asp:Label runat="server" ID="lblProductName"></asp:Label>

C# Examples :
    protected void btnPing_Click(object sender, EventArgs e)
    {
        Ping objPing = new Ping();
        
        try
        {
            PingReply objReply = objPing.Send(txtURL.Text, 1000);

            if (objReply.Status == IPStatus.Success)
            {
                lblProductName.Text = string.Format("<b>Success</b> - IP Address:{0} Time:{1}ms", objReply.Address, objReply.RoundtripTime);
            }
            else
            {
                lblProductName.Text = objReply.Status.ToString();
            }
        }
        catch (Exception ex)
        {
            lblProductName.Text = ex.Message;
        }
        
    }
VB.net Examples :
    Protected Sub btnPing_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim objPing As New Ping()

        Try
            Dim objReply As PingReply = objPing.Send(txtURL.Text, 1000)

            If objReply.Status = IPStatus.Success Then
                lblProductName.Text = String.Format("<b>Success</b> - IP Address:{0} Time:{1}ms", objReply.Address, objReply.RoundtripTime)
            Else
                lblProductName.Text = objReply.Status.ToString()
            End If
        Catch ex As Exception
            lblProductName.Text = ex.Message
        End Try
    End Sub

Output : 



This type of C# Tips is very useful in day to day programming life.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.
 

No comments:

Post a Comment