Introduction: Here I am going to demonstrate how to get This information includes host names and IP addresses of a particular domain.
What is Dns Class? The Dns class is a static class that retrieves information about a specific host from the Internet Domain Name System (DNS).
The IPHostEntry class associates a Domain Name System (DNS) host name with an array of aliases and an array of matching IP addresses. This can be done either by IP or HostName.
The IPHostEntry class is used as a helper class with the Dns class.
The following example queries the DNS database for information on the host and returns the information in an IPHostEntry instance.
If you have IPAddress, you may query for Hostname as: IPHostEntry Host = Dns.GetHostByAddress( hostIPAddress );
whereas if you have host name and you want to know IP then you must call as:
IPHostEntry Host = Dns.GetHostByName( TextBox1.Text );
Here is the complete listing of the program.
For Simplicity, you may create a new web form in VS.net with name WebForm2.aspx and paste the following code in HTML view:
<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="WebApplication23.WebForm2" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>WebForm2</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#"> <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 176px; POSITION: absolute; TOP: 64px" runat="server"></asp:TextBox> <asp:TextBox id="TextBox2" style="Z-INDEX: 102; LEFT: 176px; POSITION: absolute; TOP: 120px" runat="server" Height="24px"></asp:TextBox> <asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 360px; POSITION: absolute; TOP: 64px" runat="server" Text="Get IP Address List"></asp:Button> <asp:Button id="Button2" style="Z-INDEX: 104; LEFT: 360px; POSITION: absolute; TOP: 120px" runat="server" Text="Get Host Name "></asp:Button> <asp:Label id="Label1" style="Z-INDEX: 105; LEFT: 72px; POSITION: absolute; TOP: 176px" runat="server" Height="144px" Width="329px"></asp:Label> <asp:Label id="Label2" style="Z-INDEX: 106; LEFT: 168px; POSITION: absolute; TOP: 16px" runat="server" Width="201px" Font-Bold="True" Font-Size="Large">Host Entry Demo</asp:Label> <asp:Label id="Label3" style="Z-INDEX: 107; LEFT: 16px; POSITION: absolute; TOP: 64px" runat="server" Width="128px" Font-Bold="True">Enter Host Name:</asp:Label> <asp:Label id="Label4" style="Z-INDEX: 108; LEFT: 56px; POSITION: absolute; TOP: 120px" runat="server" Width="96px" Font-Bold="True">IP Address</asp:Label> <asp:Label id="Label5" style="Z-INDEX: 109; LEFT: 16px; POSITION: absolute; TOP: 176px" runat="server" Font-Bold="True">Result:</asp:Label> </form> </body> </HTML>
Paste the code below in the code behind file of the webform2.
using System; using System.Web; using System.Web.UI; using System.Net; using System.Net.Sockets;
namespace WebApplication23 { /// /// Summary description for WebForm2. /// public class WebForm2 : System.Web.UI.Page { protected System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.TextBox TextBox2; protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.Label Label2; protected System.Web.UI.WebControls.Label Label3; protected System.Web.UI.WebControls.Label Label4; protected System.Web.UI.WebControls.Label Label5; protected System.Web.UI.WebControls.Button Button2; private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here }
#region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.Button1.Click += new System.EventHandler(this.Button1_Click); this.Button2.Click += new System.EventHandler(this.Button2_Click); this.Load += new System.EventHandler(this.Page_Load);
} #endregion
private void Button1_Click(object sender, System.EventArgs e) { try
{
IPHostEntry Host = Dns.GetHostByName( TextBox1.Text );
Label1.Text="IP address List : \n"; for(int index=0; index < Host.AddressList.Length; index++) { Label1.Text=Label1.Text+Host.AddressList[index].ToString() + "\n"; }
}
catch(SocketException ex) { Label1.Text="SocketException caught!!!\n"; Label1.Text=Label1.Text+"Source : " + ex.Source + "\n"; Label1.Text=Label1.Text+"Message : " + ex.Message; } catch(ArgumentNullException ex) { Label1.Text="ArgumentNullException caught!!!\n"; Label1.Text=Label1.Text+"Source : " + ex.Source +"\n"; Label1.Text=Label1.Text+"Message : " + ex.Message+ "\n"; } catch(Exception ex) { Label1.Text="Exception caught!!!\n"; Label1.Text=Label1.Text+"Source : " + ex.Source +"\n"; Label1.Text=Label1.Text+"Message : " + ex.Message + "\n"; }
}
private void Button2_Click(object sender, System.EventArgs e) { try
{
IPAddress hostIPAddress = IPAddress.Parse(TextBox2.Text);
IPHostEntry Host = Dns.GetHostByAddress( hostIPAddress );
Label1.Text ="Host Name:" + Host.HostName;
Label1.Text =Label1.Text + "\nAliases :"; String[] alias = Host.Aliases; for(int index=0; index < alias.Length; index++) { Console.WriteLine(alias[index]); }
}
catch(SocketException ex) { Label1.Text="SocketException caught!!!\n"; Label1.Text=Label1.Text+"Source : " + ex.Source + "\n"; Label1.Text=Label1.Text+"Message : " + ex.Message; } catch(ArgumentNullException ex) { Label1.Text="ArgumentNullException caught!!!\n"; Label1.Text=Label1.Text+"Source : " + ex.Source +"\n"; Label1.Text=Label1.Text+"Message : " + ex.Message+ "\n"; } catch(Exception ex) { Label1.Text="Exception caught!!!\n"; Label1.Text=Label1.Text+"Source : " + ex.Source +"\n"; Label1.Text=Label1.Text+"Message : " + ex.Message + "\n"; } } } }
Enjoy the code.
Summary: We looked at using the Dns class, how to get HostName By IP and IP by HostName.
Your comments/feedback about the article is always welcome.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|