< C++, C# & VB .NET Asynchronous Name Resolution Examples | Main | Chap 8: Client Sockets Communication >

 


 

Chapter 7 Part 4:

IP Addressing and DNS

 

 

What do we have in this chapter 7 Part 4?

  1. Reverse Name Lookup

  2. C++ IP Resolution Program Example

  3. C# IP Resolution Program Example

  4. VB .NET IP Resolution Program Example

 

 

Reverse Name Lookup

 

So far, we’ve seen how to resolve a name to an IP address, but what if you have an IP address and want to find what name is associated with the address? DNS provides reverse name lookup to perform this type of lookup. The .NET Framework Dns class performs DNS reverse name lookup using GetHostByAddress(IPAddress). This function returns any names associated with the IP address in an IPHostEntry object if an IP is associated with a name. If a name can’t be found, a SocketException will be thrown. The IPv6 protocol in Windows XP and Windows Server 2003 does not register its reverse lookup information with DNS, which means that a reverse query on an IPv6 address will fail.

In DNS, more than one name can be associated with an IP address. As a result, the IPHostEntry object returned by GetHostByAddress() can return multiple names. The HostName field of IPHostEntry will contain the primary host name associated with the IP address. The Aliases field will contain any additional names that might also be associated with an IP. The following code fragment demonstrates how to perform a reverse name lookup:

 

C#

 

try

{

    IPHostEntry IPHost = Dns.GetHostEntry("10.1.2.3");

    Console.WriteLine("The Primary Host name is: " + IPHost.HostName.ToString());

 

    // Print out any Aliases that are found

    if (IPHost.Aliases.Length > 0)

    {

        Console.WriteLine("Additional names are:");

        foreach(string Alias in IPHost.Aliases)

        {

            Console.WriteLine(Alias);

        }

    }

}

catch (Exception e)

{

    Console.WriteLine("GetHostEntry() failed with error: " + e.Message);

}

 

Visual Basic .NET

 

Try

Dim IPHost As IPHostEntry = Dns.GetHostEntry("10.1.2.3")

 

    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("Additional names found are: ")

Dim CurAlias As String

 

        For Each CurAlias In IPHost.Aliases

            Console.WriteLine(CurAlias)

        Next

    End If

Catch e As Exception

    Console.WriteLine("GetHostEntry() failed with error: " + e.Message)

End Try

 

Performing DNS reverse name lookup using GetHostByAddress() can take some time, so it might cause your application to block on the call. Blocking on this call can make your application appear unresponsive, as described with forward name lookup. Therefore, two asynchronous counterpart methods, BeginHostByAddress() and EndHostByAddress(), are available for reverse name lookup that use the .NET Framework asynchronous pattern.

The following example is a sample that demonstrates how to synchronously perform reverse name lookup using the Dns class.

 

C++ IP Resolution Program Example

 

Create a new CLR console application and you can use ResolveIPCP as the project and solution names if you want.

 

C++ IP Resolution Program Example - a new CLR console application project creation in  VS 2008

 

Add the following code.

 

// ResolveIPCP.cpp : main project file.

/// <summary>

/// This sample demonstrates how to perform reverse name lookup using

/// the DNS service. Reverse name lookup is finding what names are

/// associated with an IP address. The sample will print the primary

/// host name found for an IP address followed by any additional names

/// if they are found. To run this sample, simply run ResolveIP and

/// supply an IP address as a command line parameter.

/// </summary>

 

#include "stdafx.h"

 

using namespace System;

using namespace System::Net;

 

[STAThread]

int main(array<System::String ^> ^args)

{

    if (args->Length < 1)

            {

                        Console::WriteLine("Usage: {0} <IP address to query>", Environment::CommandLine);

                        Console::WriteLine("Usage: {0} 1.2.1.4>", Environment::CommandLine);

                        return 0;

            }

 

            try

            {

                        IPHostEntry^ IPHost = Dns::GetHostEntry(args[0]->ToString());

                        Console::WriteLine("GetHostEntry() is OK...");

                        Console::WriteLine("The Primary Host name is: " + IPHost->HostName->ToString());

 

                        // Print out any Aliases that are found

                        if (IPHost->Aliases->Length > 0)

                        {

                                    Console::WriteLine("\nAdditional names found are:");

                                    for each(String^ Alias in IPHost->Aliases)

                                    {

                                                Console::WriteLine(Alias);

                                    }

                        }

                        Console::WriteLine("\nNo Aliases found...");

            }

            catch (Exception^ e)

            {

                        Console::WriteLine("GetHostEntry() failed with error: " + e->Message);

            }

 

    return 0;

}

 

Build and run the project. The following is the output sample.

 

C++ IP Resolution Program Example - a sample output when program run from the command prompt

 

C# IP Resolution Program Example

 

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

 

C# IP Resolution Program Example - a new console application project creation in Visual Studio 2008 IDE

 

Add the following code.

 

using System;

using System.Net;

 

namespace ResolveIPChap6CS

{

    /// <summary>

    /// Summary description for ResolveIP.

    /// </summary>

    class Program

    {

        /// <summary>

        /// This sample demonstrates how to perform reverse name lookup using

        /// the DNS service. Reverse name lookup is finding what names are

        /// associated with an IP address. The sample will print the primary

        /// host name found for an IP address followed by any additional names

        /// if they are found. To run this sample, simply run ResolveIP and

        /// supply an IP address as a command line parameter.

        /// </summary>

        [STAThread]

        static void Main(string[] args)

        {

            if (args.Length < 1)

                        {

                                    Console.WriteLine("Usage: Executable_file_name <IP address to query>");

                                    return;

                        }

 

                        try

                        {

                                    IPHostEntry IPHost = Dns.GetHostEntry(args[0].ToString());

                                    Console.WriteLine("GetHostEntry() is OK...");

                                    Console.WriteLine("The Primary Host name is: " + IPHost.HostName.ToString());

 

                                    // Print out any Aliases that are found

                                    if (IPHost.Aliases.Length > 0)

                                    {

                                                Console.WriteLine("Additional names found are:");

                                                foreach(string Alias in IPHost.Aliases)

                                                {

                                                            Console.WriteLine(Alias);

                                                }

                                    }

                                  Console.WriteLine("No Aliases found...");

                        }

                        catch (Exception e)

                        {

                            Console.WriteLine("GetHostEntry() failed with error: " + e.Message);

                        }

        }

    }

}

 

The following are the sample outputs for ipv4 and ipv6.

 

C# IP Resolution Program Example - a sample output with IPv4 protocol

 

C# IP Resolution Program Example - a sample output with IPv6 protocol

 

 

VB .NET IP Resolution Program Example

 

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

 

VB .NET IP Resolution Program Example - a new console application project creation in Visual Studio 2008 IDE

 

Add the following code.

 

Imports System

Imports System.Net

 

Module Module1

    ' This sample demonstrates how to perform reverse name lookup using

    ' the DNS service. Reverse name lookup is finding what names are

    ' associated with an IP address. The sample will print the primary

    ' host name found for an IP address followed by any additional names

    ' if they are found. To run this sample, simply run ResolveIP and

    ' supply an IP address as a command line parameter.

    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 <IP address to query>")

            Exit Sub

        End If

 

        Try

            Dim IPHost As IPHostEntry = Dns.GetHostEntry(args(1).ToString())

 

            Console.WriteLine("GetHostEntry() is OK...")

            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("Additional names found are:")

                Dim CurAlias As String

 

                For Each CurAlias In IPHost.Aliases

                    Console.WriteLine(CurAlias)

                Next

            End If

            Console.WriteLine("No Aliases were found...")

        Catch e As Exception

            Console.WriteLine("GetHostEntry() failed with error: " + e.Message)

        End Try

    End Sub

End Module

 

The following is the sample output.

 

VB .NET IP Resolution Program Example - a sample output when the program run from the command prompt

 

There are many more features that can be implemented other than the name resolution and services. You can find those services in the nslookup program (dig program in Linux/Unix) and screenshots are shown below.

 

The nslookup command example

 

The nslookup command in action

 

 

 


 

< C++, C# & VB .NET Asynchronous Name Resolution Examples | Main | Chap 8: Client Sockets Communication >