首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# DNS.GetHostEntry() ..新程序员网络编程新手

C# DNS.GetHostEntry() ..新程序员网络编程新手
EN

Stack Overflow用户
提问于 2013-02-18 00:40:02
回答 1查看 2K关注 0票数 0

我对C#真的很不熟悉,我已经好几年没有用这门语言编程了。我将发布我拥有的代码,其中有构建错误。这就是我想要做的,但我真的不确定该怎么做。我碰壁了,真的不知道该怎么做:

输入地址(以字符串形式)使用适当的函数解析地址打印出完整的主机信息

代码语言:javascript
复制
   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
                {

                    hostinfo = Dns.GetHostEntry("www.sunybroome.edu"); // DNS Name Resolution

                    //
                    // The IP address is now in hostinfo structure
                    // Print out the contents of hostinfo structure
                    // in an easily readable form with labels. For
                    // example, the host name can be output using:


                    Console.WriteLine("Hostname = {0}\n", hostinfo.HostName);
                }

                catch
                {
                    // Print out the exception here...
                }



                try
                {
                    IPHostEntry hostInfo;

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

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

                    //Display list of IP addresses for this host
                    Console.Write("\tIP Addresses:  ");
                    foreach (IPAddress ipaddr in hostInfo.AddressList)
                    {
                        Console.Write(ipaddr.ToString() + " ");
                    }
                    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");
                }
                catch (Exception)
                {
                    Console.WriteLine("\tUnable to resolve host: " + host + "\n");
                }
            }
        }

        static void Main(string[] args)
        {

            //Get and print local host info
            try
            {
                Console.WriteLine("Local Host:");
                String localHostName = Dns.GetHostName();
                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 args)
            {
                Console.WriteLine(arg + ":");
                PrintHostInfo(arg);
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2013-02-18 01:32:24

您需要将host传递给Resolve方法,而不是hostInfo (即包含要解析的主机的字符串):

代码语言:javascript
复制
hostInfo = Dns.Resolve(host);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14923401

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档