Glossary Item Box
Using the Dns component will involve either:
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
Send comments on this topic.
Documentation version 1.1.2.0.
© 2008 Dart Communications. All rights reserved.