Resolving Domain Names and IP Addresses
For some reasons, you may want to do the following things:
2:
Converting IP Addresses to Domain Names
Sample codes used in demonstration:
<html> <head> <title>Resolving Domain Names and IP Addresses</title> </head> <body> <form runat="server"> <asp:label id="Label0" style="LEFT: 200px; POSITION: absolute; TOP: 64px" runat="server" ForeColor="Navy" Font-Bold="True">Enter:</asp:label> <asp:label id="Label1" style="LEFT: 200px; POSITION: absolute; TOP: 328px" runat="server" ForeColor="Navy" Font-Bold="True">Aliases:</asp:label> <asp:label id="Label2" style="LEFT: 200px; POSITION: absolute; TOP: 208px" runat="server" ForeColor="Navy" Font-Bold="True">IP Address:</asp:label> <asp:label id="Label3" style="LEFT: 200px; POSITION: absolute; TOP: 112px" runat="server" ForeColor="Navy" Font-Bold="True">Primary host name for the server: </asp:label>Back-end Code Snippet<asp:textbox id="tbDomain" style="LEFT: 264px; POSITION: absolute; TOP: 64px" runat="server"></asp:textbox> <asp:button id="btnResolve" style="LEFT: 464px; POSITION: absolute; TOP: 64px" runat="server" Text="Resolve"></asp:button><asp:label id="lblHostName" style="LEFT: 200px; POSITION: absolute; TOP: 152px" runat="server"></asp:label> <asp:label id="lblIP" style="LEFT: 200px; POSITION: absolute; TOP: 240px" runat="server"></asp:label> <asp:label id="lblAliases" style="LEFT: 200px; POSITION: absolute; TOP: 360px" runat="server"></asp:label> <asp:label id="lblError" style="LEFT: 200px; POSITION: absolute; TOP: 440px" runat="server" ForeColor="Red"></asp:label></form> </body> </html>
private void btnResolve_Click(object sender, System.EventArgs e)
{
// Declare/Create an IPHostEntry object
IPHostEntry objIPHostEntry;
int i;
lblHostName.Text = "";
lblIP.Text = "";
lblAliases.Text = "";
lblError.Text = "";
try
{
// Dns class from System.Net.Dns calls the method Resolve
// to resolve the inputted IP/Domain
objIPHostEntry = Dns.Resolve(tbDomain.Text.ToString());
lblHostName.Text=objIPHostEntry.HostName;
for(i = 0;i< objIPHostEntry.AddressList.Length;i++)
{
lblIP.Text+=objIPHostEntry.AddressList[i].ToString()+"<br />";
}
for(i = 0;i< objIPHostEntry.Aliases.Length;i++)
{
lblAliases.Text+=objIPHostEntry.Aliases[i].ToString()+"<br />";
}
}
catch (Exception ex)
{
lblError.Text=ex.Message;
}
}
Explanation:
In the above Front-end codes, we first create four labels (Label0 to Label3) for displaying the prompt text and another four labels (lblHostName, lblIP, lblAliases and lblError) for displaying the result.
lblHostName for displaying the Primary host name for the server, lblIP for displaying the primary IP addresses, lblAliases for displaying the Domain aliases, lblError for displaying the possible error messages. |
In the HTML page, we also need a TextBox for user to input the IP / Domain name for resolving and a submit button to submit the request. Both codes are highlighted in red in the above Front-end codes.
Once the user has submitted the request, the Back-end codes will be runned in the server.
Please be reminded that the above Back-end codes are just some snippets, you have to make sure all the necessary namespaces especially "System.Net" is imported by typing using System.Net; at the top of the c# file.
In dotnet, there is an object which has properties such as HostName, AddressList and Aliases. We declare this object by IPHostEntry objIPHostEntry;
After declaring this object, we use Dns.Resolve(tbDomain.Text.ToString()); to resolve the IP address or Domain Name inputted by the user.
The Dns.Resolve() method will accept a parameter (tbDomain.Text.ToString()) which is inputted in the TextBox by user and return an instance of the class IPHostEntry.
Finally, we get the Host Name, IP Addresses and Domain Aliases by the properties of the objIPHostEntry (an instance of IPHostEntry).
// Get the Primary host name for the server and display in Label, lblHostName lblHostName.Text=objIPHostEntry.HostName; // Get the list of IP addreses and display in Label, lblIP lblIP.Text+=objIPHostEntry.AddressList[i].ToString()+"<br />"; // Get the list of Domain Aliases and display in Label, lblAliases lblAliases.Text+=objIPHostEntry.Aliases[i].ToString()+"<br />"; |
Conclusion:
In the Front-end side, you design the layout and has a TextBox for inputting the IP address/Domain Name and a Button for submitting the request.
In the Back-end side, you call System.Net.Dns.Resolve() method to return the instance of IPHostEntry which has properties such as HostName, AddressList and Aliases.
If you are doing everything well, you should have the similar picture as this:









