|
|
C++ Asynchronous Name Resolution Program Example
Create a new CLR console application and you can use AsyncResolveNameCP as the project and solution names if you want.
Add the following code. |
|
// AsyncResolveNameCP.cpp : main project file. /// <summary> /// This sample is designed to resolve a name asynchronously using DNS /// forward lookup techniques and demonstrates how to do this using the /// .Net Framework class System.Net.Dns. To run this application, simply /// provide a name to resolve as a command line parameter. If the name /// is found in DNS, the program will print out the primary host name /// along with all IP addresses that are associated with the name. /// The program will also print any alias names that are associated /// with the name being queried. /// </summary>
#include "stdafx.h"
using namespace System; using namespace System::Net; using namespace System::Net::Sockets;
static void ProcessDnsResults(IAsyncResult^ ar) { try { IPHostEntry^ IPHost = Dns::EndGetHostEntry(ar); // Print out the host name that was queried Console::WriteLine(); Console::WriteLine("The primary host name is: " + IPHost->HostName->ToString()); // Print out any aliases that are found if (IPHost->Aliases->Length > 0) { Console::WriteLine("\nAliases found are:"); for each (String^ Alias in IPHost->Aliases) { Console::WriteLine(Alias); } }
Console::WriteLine("\nNo Aliases found..."); Console::WriteLine("\nIP addresses found are:");
int IPv4Count = 0; int IPv6Count = 0;
// Print out all the IP addresses that are found for each (IPAddress^ Address in IPHost->AddressList) { if (Address->AddressFamily == AddressFamily::InterNetwork) { IPv4Count++; Console::WriteLine("IPv4 Address #" + IPv4Count.ToString() + " is " + Address->ToString()); } else if (Address->AddressFamily == AddressFamily::InterNetworkV6) { IPv6Count++; Console::WriteLine("IPv6 Address #" + IPv6Count.ToString() + " is " + Address->ToString()); } } } catch (Exception^ e) { Console::WriteLine("GetHostEntry() failed with error: " + e->Message); } finally { Console::WriteLine("Finished querying DNS."); } }
/// <summary> /// The main entry point for the application. /// </summary> [STAThread] int main(array<System::String ^> ^args) { if (args->Length < 1) { Console::WriteLine("Usage: {0} <name to resolve>", Environment::CommandLine); Console::WriteLine("Example: {0} www.kambing.com", Environment::CommandLine); Console::WriteLine(" {0} tenouk.com", Environment::CommandLine); return 0; } Console::WriteLine("Instantiating the AsyncCallBack()..."); AsyncCallback^ AsyncDnsCallback = gcnew AsyncCallback(ProcessDnsResults);
try { Console::WriteLine("Executing the BeginGetHostEntry()..."); Dns::BeginGetHostEntry(args[0]->ToString(), AsyncDnsCallback, nullptr); } catch (Exception^ e) { Console::WriteLine("BeginGetHostEntry() failed with error: " + e->Message); } Console::WriteLine("Press any key to stop this program..."); Console::Read(); Console::WriteLine("Read() is OK..."); return 0; } |
Build and run the project. The following is the output sample.

Create a new console application project. You can use the project and solution name as shown in the following Figure if you want.

Add the following code.
|
using System; using System.Net; using System.Net.Sockets;
/// <summary> /// This sample is designed to resolve a name asynchronously using DNS /// forward lookup techniques and demonstrates how to do this using the /// .Net Framework class System.Net.Dns. To run this application, simply /// provide a name to resolve as a command line parameter. If the name /// is found in DNS, the program will print out the primary host name /// along with all IP addresses that are associated with the name. /// The program will also print any alias names that are associated /// with the name being queried. /// </summary> namespace AsyncResolveNameChap6CS { class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[ ] args) { if (args.Length < 1) { Console.WriteLine("Usage: Executable_file_name <name to resolve>"); return; }
AsyncCallback AsyncDnsCallback = new AsyncCallback(ProcessDnsResults);
try { Dns.BeginGetHostEntry(args[0].ToString(), AsyncDnsCallback, null); Console.WriteLine("BeginGetHostEntry() is OK..."); } catch (Exception e) { Console.WriteLine("BeginGetHostEntry() failed with error: " + e.Message); } Console.WriteLine("Press any key to stop this program..."); Console.Read(); Console.WriteLine("Read() is OK..."); }
static void ProcessDnsResults(IAsyncResult ar) { try { IPHostEntry IPHost = Dns.EndGetHostEntry(ar);
// Print out the host name that was queried Console.WriteLine(); Console.WriteLine("The primary host name is: " + IPHost.HostName.ToString()); // Print out any aliases that are found if (IPHost.Aliases.Length > 0) { Console.WriteLine("Aliases found are:"); foreach (string Alias in IPHost.Aliases) { Console.WriteLine(Alias); } } Console.WriteLine("No Aliases found..."); Console.WriteLine("IP addresses found are:");
int IPv4Count = 0; int IPv6Count = 0;
// Print out all the IP addresses that are found foreach (IPAddress Address in IPHost.AddressList) { if (Address.AddressFamily == AddressFamily.InterNetwork) { IPv4Count++; Console.WriteLine("IPv4 Address #" + IPv4Count.ToString() + " is " + Address.ToString()); } else if (Address.AddressFamily == AddressFamily.InterNetworkV6) { IPv6Count++; Console.WriteLine("IPv6 Address #" + IPv6Count.ToString() + " is " + Address.ToString()); } } } catch (Exception e) { Console.WriteLine("GetHostEntry() failed with error: " + e.Message); } finally { Console.WriteLine("Finished querying DNS."); } } } } |
The following is the sample output.
![]() |
|
Create a new console application project. You can use the project and solution name as shown in the following Figure if you want.

Add the following code.
|
Imports System Imports System.Net Imports System.Net.Sockets
' This sample is designed to resolve a name asynchronously using DNS ' forward lookup techniques and demonstrates how to do this using the ' .Net Framework class System.Net.Dns. To run this application, simply ' provide a name to resolve as a command line parameter. If the name ' is found in DNS, the program will print out the primary host name ' along with all IP addresses that are associated with the name. ' The program will also print any alias names that are associated ' with the name being queried. Module Module1 ' The main entry point for the application. Sub Main() ' Parse command line arguments if any Dim args As String() = Environment.GetCommandLineArgs() If (args.Length < 2) Then Console.WriteLine("Usage: Executable_file_name <name to resolve>") Exit Sub End If
Dim AsyncDnsCallback As AsyncCallback = New AsyncCallback(AddressOf ProcessDnsResults)
Try Dns.BeginGetHostEntry(args(1).ToString(), AsyncDnsCallback, Nothing) Console.WriteLine("BeginGetHostEntry() is OK...") Catch e As Exception Console.WriteLine("BeginGetHostByName failed with error: " + e.Message) End Try
Console.WriteLine("Press any key to stop this program...") Console.Read() Console.WriteLine("Read() is OK...") End Sub
Sub ProcessDnsResults(ByVal ar As IAsyncResult) Try Dim IPHost As IPHostEntry = Dns.EndGetHostEntry(ar) ' Print out the host name that was queried Console.WriteLine() Console.WriteLine("The primary host name is: " + IPHost.HostName.ToString())
' Print out any aliases that are found If (IPHost.Aliases.Length > 0) Then Console.WriteLine("Aliases found are:") Dim CurAlias As String For Each CurAlias In IPHost.Aliases Console.WriteLine(CurAlias) Next End If Console.WriteLine("No Aliases found...") Console.WriteLine("IP addresses found are:") Dim IPv4Count As Integer = 0 Dim IPv6Count As Integer = 0
' Print out all the IP addresses that are found Dim Address As IPAddress For Each Address In IPHost.AddressList If (Address.AddressFamily = AddressFamily.InterNetwork) Then IPv4Count = IPv4Count + 1
Console.WriteLine("IPv4 Address #" + IPv4Count.ToString() + " is " + Address.ToString())
ElseIf (Address.AddressFamily = AddressFamily.InterNetworkV6) Then IPv6Count = IPv6Count + 1 Console.WriteLine("IPv6 Address #" + IPv6Count.ToString() + " is " + Address.ToString()) End If Next Catch e As Exception Console.WriteLine("GetHostEntry() failed with error: " + e.Message) Finally Console.WriteLine("Finished querying DNS.") End Try End Sub End Module |
The following is the sample output.
