Warning: mysql_connect() [function.mysql-connect]: Access denied for user: 'coolstuffAdmin@64.202.163.2' (Using password: YES) in /home/content/c/o/o/coolstuffAdmin/html/cool/myinterests/Dotnet/dotnet-301205.php on line 6

Warning: mysql_select_db() [function.mysql-select-db]: Can't connect to local MySQL server through socket '/usr/local/mysql-5.0/data/mysql.sock' (2) in /home/content/c/o/o/coolstuffAdmin/html/cool/myinterests/Dotnet/dotnet-301205.php on line 7

Warning: mysql_select_db() [function.mysql-select-db]: A link to the server could not be established in /home/content/c/o/o/coolstuffAdmin/html/cool/myinterests/Dotnet/dotnet-301205.php on line 7

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/content/c/o/o/coolstuffAdmin/html/cool/myinterests/Dotnet/dotnet-301205.php on line 14

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/c/o/o/coolstuffAdmin/html/cool/myinterests/Dotnet/dotnet-301205.php on line 16
error, please contact webmaster
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/content/c/o/o/coolstuffAdmin/html/cool/myinterests/Dotnet/dotnet-301205.php on line 43

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/c/o/o/coolstuffAdmin/html/cool/myinterests/Dotnet/dotnet-301205.php on line 45
error, please contact webmaster Using WebService Behavior to consume online WebServices (My Interests) --- Coolman's Home

Resolving Domain Names and IP Addresses

For some reasons, you may want to do the following things:

1:

2:
Converting Domain Names to IP Addresses

Converting IP Addresses to Domain Names

Sample codes used in demonstration:

Front-end Code
<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>

  <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>
Back-end Code Snippet
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:

IPHostEntry

 

[加到最愛]  [回到首頁]  [我有話說...]

Copyright 2005 Coolman's Home. All rights Reserved
Contact me|Drop me a message|RSS 2.0

printerbottom
view more view more view more view more