Monday, April 4, 2011

Resolving Hostname

I know, it is kind of lame. But I'll post it anyway.

using System;                   // for String and Console
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;               // for Dns, IpHostEntry, IPAddress
using System.Net.Sockets;       // for SocketException

namespace SocketCoba01
{
    class Program
    {
        static void PrintHostInfo(string host)
        {
            try
            {
                IPHostEntry hostInfo;

                hostInfo = Dns.GetHostEntry(host);  //karena Dns.resolve sudah obsolete 
                //(baca dokumentasi .NET)

                //display the primary hostname
                Console.WriteLine("\tCanonical Name: " + hostInfo.HostName);

                //display list of IP adresses for this host
                Console.WriteLine("\tIP Addresses: ");
                foreach (IPAddress ip in hostInfo.AddressList)
                {
                    Console.WriteLine("\t\t{0}", ip.ToString());
                }

                Console.WriteLine();

                //display list of all aliases for this host
                Console.WriteLine("\tAliases: ");
                foreach (String alias in hostInfo.Aliases)
                {
                    Console.WriteLine("\t\t{0}", alias);
                }
            }
            catch (Exception) 
            {
                Console.WriteLine(".:. Unable to resolve host: " + host + "\n");
            }
 
        }
        static void Main(string[] args)
        {
            try
            {
                String LocalHostName = Dns.GetHostName();
                Console.WriteLine("Local host");
                Console.WriteLine("\tHost name: {0}",LocalHostName);

                PrintHostInfo(LocalHostName);                
            }
            catch (Exception) 
            {
                Console.WriteLine("error @Main");
            }
            Console.ReadKey();
        }

       
    }
}


No comments:

Post a Comment