< Intro To IP Addressing & DNS | Main | C++, C# & VB .NET Asynchronous Name Resolution Examples >

 


 

Chapter 7 Part 2:

IP Addressing and DNS

 

 

What do we have in this chapter 7 Part 2?

  1. C++ Name Resolution Program Example

  2. C# Name Resolution Program Example

  3. VB .NET Name Resolution Program Example

 

 

C++ Name Resolution Program Example

 

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

 

C++ Name Resolution Program Example - creating a new CLR console application project in Visual Studio 2008

 

Add the following code.

 

// ResolveNameChap6CP.cpp : main project file.

/// <summary>

/// This sample is designed to resolve a name 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;

 

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

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

{

    if (args->Length < 1)

    {

        Console::WriteLine("Usage: Executable_file_name <name to resolve>");

        Console::WriteLine("Example: Executable_file_name www.yahoo.com");

        return 0;

    }

 

    try

    {

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

 

        // Print out the host name that was queried

        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:");

            for each (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

        Console::WriteLine("\nPrinting out all the found IP addresses...");

        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);

    }

    return 0;

}

 

Build and run the project. The following are the output samples.

 

C++ Name Resolution Program Example - a sample output

 

C++ Name Resolution Program Example - another sample output with IPv6 protocol

 

C# Name 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# Name Resolution Program Example - creating a new console application project in Visual Studio 2008

 

Add the following code.

 

using System;

using System.Net;

using System.Net.Sockets;

 

namespace ResolveNameChap6CS

{

    /// <summary>

    /// This sample is designed to resolve a name 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>

    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;

            }

 

            try

            {

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

                // Print out the host name that was queried

                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

                Console.WriteLine("Printing out all the found IP addresses...");

                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);

            }

        }

    }

}

 

The following are the sample outputs.

 

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

 

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

 

VB .NET Name 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 Name Resolution Program Example - creating a new console application project in VS 2008

 

Add the following code.

 

Imports System

Imports System.Net

Imports System.Net.Sockets

' This sample is designed to resolve a name 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

 

    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

 

        Try

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

 

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

            ' Print out the host name that was queried

            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

            Console.WriteLine("Printing out all the found IP addresses...")

 

            Dim Address As IPAddress

 

            For Each Address In IPHost.AddressList

                If (Address.AddressFamily = AddressFamily.InterNetwork) Then

                    IPv4Count += 1

                    Console.WriteLine("IPv4 Address #" + IPv4Count.ToString() + " is " + Address.ToString())

                ElseIf (Address.AddressFamily = AddressFamily.InterNetworkV6) Then

                    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)

        End Try

    End Sub

End Module

 

The following are the sample outputs.

 

VB .NET Name Resolution Program Example - a sample output with IPv4 protocol

 

VB .NET Name Resolution Program Example - a sample output with IPv6 protocol

 

 

 


 

< Intro To IP Addressing & DNS | Main | C++, C# & VB .NET Asynchronous Name Resolution Examples >