使用适当的函数解析地址并打印出完整的主机信息

时间:2013-02-17 20:28:29

标签: c# dns

* 已编辑的代码 *这是我到目前为止所拥有的。无论我放入什么网站,IP地址都是一样的。我需要做些什么来解决主题标题中的所有问题?我感谢任何帮助,因为我真的不知道如何继续!! 编辑代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;

namespace CSDNS
{
    class Program
    {
        static void PrintHostInfo(String host)
        {
            {
                IPHostEntry hostinfo;

                try
                {
                    IPHostEntry hostInfo;

                    //Attempt to resolve DNS for given host or address
                    hostInfo = Dns.GetHostAddresses(host);

                    //Display the primary host name
                    Console.WriteLine("\tCanonical Name: " + hostInfo.HostName);

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

                    //Display list of alias names for this host
                    Console.Write("\tAliases:       ");
                    foreach (String alias in hostInfo.Aliases)
                    {
                        Console.Write(alias + " ");
                    }
                    Console.WriteLine("\n-------------------------------------\n\n");
                }
                catch (Exception)
                {
                    Console.WriteLine("\tUnable to resolve host: " + host + "\n");
                }
            }
        }

        static void Main()
        {
            //Get and print local host info
            try
            {
                Console.WriteLine("Local Host:");
                String localHostName = Dns.GetHostAddresses();
                Console.WriteLine("\tHost Name:      " + localHostName);

                PrintHostInfo(localHostName);
            }
            catch (Exception)
            {
                Console.WriteLine("Unable to resolve local host\n");
            }

            //Get and print info for hosts given on command line 
            foreach (String arg in new[] { "www.sunybroome.edu" })
            {
                Console.WriteLine(arg + ":");
                PrintHostInfo(arg);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

    static void PrintHostInfo(String host)
    {
        {
            IPHostEntry hostinfo;

            try
            {
                //Attempt to resolve DNS for given host or address
                IPAddress[] hostInfo = Dns.GetHostAddresses(host);

                //Display list of IP addresses for this host
                Console.WriteLine("\tIP Addresses:  ");
                foreach (IPAddress ipaddr in hostInfo)
                {
                    Console.WriteLine("\t\t\t{0} ", ipaddr);
                }

                Console.WriteLine("\n-------------------------------------\n\n");
            }
            catch (Exception)
            {
                Console.WriteLine("\tUnable to resolve host: " + host + "\n");
            }
        }
    }

    static void Main()
    {
        //Get and print local host info
        try
        {
            Console.WriteLine("Local Host:");
            String localHostName = Dns.GetHostAddresses("localhost")[0].ToString();
            Console.WriteLine("\tHost Name:      " + localHostName);

            PrintHostInfo(localHostName);
        }
        catch (Exception)
        {
            Console.WriteLine("Unable to resolve local host\n");
        }

        //Get and print info for hosts given on command line 
        foreach (String arg in new[] { "www.sunybroome.edu" })
        {
            Console.WriteLine(arg + ":");
            PrintHostInfo(arg);
        }
    }