Glossary Item Box

PowerTCP SSL Sockets for .NET

Resolving IP Addresses, Host Names, and Email Addresses

Using the Dns component will involve either:

This topic provides a walk-through of these scenarios.

 

Example 1: Resolve a host name to a dot address.

This is the most standard usage example of the Dns component. Simply set the DNS server to use and pass a hostname into the GetHost method. This method will return a System.Net.IPHostEntry if the host name is resolved, or throw an Exception if the host name cannot be resolved.

[C#]
// Set the DNS server to use.
dns1.Server = "MyDnsServer";

// Resolve the host name.
try
{
   System.Net.IPHostEntry result = dns1.GetHost("www.dart.com");

   // Display host name
   Debug.WriteLine("Host name: " + result.HostName);

   // Display addresses, if any
   foreach(System.Net.IPAddress add in result.AddressList)
      Debug.WriteLine("Address: " + add.ToString());

   // Display aliases, if any
   foreach(string s in result.Aliases)
      Debug.WriteLine("Aliases: " + s);
}
catch(Exception ex)
{
   Debug.WriteLine("Name cannot be resolved. Error: " + ex.Message);
}

[Visual Basic]
' Set the DNS server to use.
Dns1.Server = "MyDnsServer"

' Resolve the host name.
Try
   Dim result As System.Net.IPHostEntry = Dns1.GetHost("www.dart.com")

   ' Display host name
   Debug.WriteLine("Host name: " + result.HostName)

   ' Display addresses, if any
   Dim add As System.Net.IPAddress
   For Each add In result.AddressList
      Debug.WriteLine("Address: " + add.ToString())
   Next

   ' Display aliases, if any
   Dim s As String
   For Each s In result.Aliases
      Debug.WriteLine("Aliases: " + s)
   Next

Catch ex as Exception
   Debug.WriteLine("Name cannot be resolved. Error: " + ex.Message)
End Try

 

Example 2: Resolve a dot address to a host name.

To resolve a dot address to a host name simply set the DNS server to use and pass a dot address into the GetHost method. This method will return a System.Net.IPHostEntry if the dot address is resolved, or throw an Exception if the dot address cannot be resolved.

[C#]
// Set the DNS server to use.
dns1.Server = "MyDnsServer";

// Resolve the dot address.
try
{
   System.Net.IPHostEntry result = dns1.GetHost("209.61.190.88");

   // Display host name
   Debug.WriteLine("Host name: " + result.HostName);

   // Display addresses, if any
   foreach(System.Net.IPAddress add in result.AddressList)
      Debug.WriteLine("Address: " + add.ToString());

   // Display aliases, if any
   foreach(string s in result.Aliases)
      Debug.WriteLine("Aliases: " + s);
}
catch(Exception ex)
{
   Debug.WriteLine("Name cannot be resolved. Error: " + ex.Message);
}

[Visual Basic]
' Set the DNS server to use.
Dns1.Server = "MyDnsServer"

' Resolve the dot address.
Try
   Dim result As System.Net.IPHostEntry = Dns1.GetHost("209.61.190.88")

   ' Display host name
   Debug.WriteLine("Host name: " + result.HostName)

   ' Display addresses, if any
   Dim add As System.Net.IPAddress
   For Each add In result.AddressList
      Debug.WriteLine("Address: " + add.ToString())
   Next

   ' Display aliases, if any
   Dim s As String
   For Each s In result.Aliases
      Debug.WriteLine("Aliases: " + s)
   Next

Catch ex as Exception
   Debug.WriteLine("Name cannot be resolved. Error: " + ex.Message)
End Try

 

Example 3: Resolving an email address.

To resolve an email address to a mail server simply set the DNS server to use and pass a dot address into the GetHost method. This method will return an array of System.Net.IPHostEntry object if the email address is resolved, or throw an Exception if the email address cannot be resolved.

[C#]
// Set the DNS server to use.
dns1.Server = "MyDnsServer";

// Resolve the host name.
try
{
   System.Net.IPHostEntry[] results = dns1.GetHost("209.61.190.88");

   // Iterate through all IPHostEntry objects
   foreach(System.Net.IPHostEntry result in results)
   {            
      // Display host name
      Debug.WriteLine("Host name: " + result.HostName);

      // Display addresses, if any
      foreach(System.Net.IPAddress add in result.AddressList)
         Debug.WriteLine("Address: " + add.ToString());

      // Display aliases, if any
      foreach(string s in result.Aliases)
         Debug.WriteLine("Aliases: " + s);
   }
}
catch(Exception ex)
{
   Debug.WriteLine("Name cannot be resolved. Error: " + ex.Message);
}

[Visual Basic]
' Set the DNS server to use.
Dns1.Server = "MyDnsServer"

' Resolve the email address.
Try
   Dim results = Dns1.GetHost("test@dart.com")

   'Iterate through all IPHostEntry objects
   Dim result As System.Net.IPHostEntry
   For Each result In results

      ' Display host name
      Debug.WriteLine("Host name: " + result.HostName)

      ' Display addresses, if any
      Dim add As System.Net.IPAddress
      For Each add In result.AddressList
         Debug.WriteLine("Address: " + add.ToString())
      Next

      ' Display aliases, if any
      Dim s As String
      For Each s In result.Aliases
         Debug.WriteLine("Aliases: " + s)
      Next
   Next
Catch ex as Exception
   Debug.WriteLine("Name cannot be resolved. Error: " + ex.Message)
End Try

 

In This Section

Resolving IP Addresses, Host Names and Email Addresses
Demonstrates how to resolve IP Addresses, host names and email addresses using the Dns component.
Resolving Asynchronously
Demonstrates how to use the Dns component to asynchronously resolve addresses.
Simple Pinging
Demonstrates how to ping a host.
Pinging Asynchronously
Demonstrates how to ping a host asynchronously.
Performing A Simple Trace Route Operation
Demonstrates how to trace a route to a host.
Performing A Trace Route Asynchronously
Demonstrates how to trace a route to a host asynchronously.

 

 


Send comments on this topic.

Documentation version 1.1.2.0.

© 2008 Dart Communications.  All rights reserved.