Showing posts with label Network. Show all posts
Showing posts with label Network. Show all posts

Tuesday, 31 July 2012

You can get IP Address of a fully qualified host name. We can Use GetHostEntry method of the System.Net.Dns class. There are multiple IP Addresses for some host name.

Here is Example for this.
In this example we can get IP address of our given host name. We provide "jayeshsorathia.blogspot.com" as a host name. It will return Multiple IP Addresses.
We will also attach another output screen in which we supplied "www.microsoft.com" as a hostname.

C# Examples :
        string strHostName = "jayeshsorathia.blogspot.com";
        //string strHostName = "www.microsoft.com";
        // Get DNS entry of specified host name
        IPAddress[] addresses = Dns.GetHostEntry(strHostName).AddressList;

        // The DNS entry may contains more than one IP addresses.
        // Iterate them and display each along with the type of address (AddressFamily).
        foreach (IPAddress address in addresses)
        {
            Response.Write(string.Format("{0} = {1} ({2})", strHostName, address, address.AddressFamily));
            Response.Write("<br/><br/>");
        }

VB.net Examples :
        Dim strHostName As String = "jayeshsorathia.blogspot.com"
        'string strHostName = "www.microsoft.com";
        ' Get DNS entry of specified host name
        Dim addresses As IPAddress() = Dns.GetHostEntry(strHostName).AddressList

        ' The DNS entry may contains more than one IP addresses.
        ' Iterate them and display each along with the type of address (AddressFamily).
        For Each address As IPAddress In addresses
            Response.Write(String.Format("{0} = {1} ({2})", strHostName, address, address.AddressFamily))
            Response.Write("<br/><br/>")
        Next

Output (jayeshsorathia.blogspot.com)

Output (www.microsoft.com) :

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.




Thursday, 12 July 2012

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.