2013-02-17 38 views
0

我真的不熟悉C#,這已經過了幾年,因爲我已經用這種語言編程。我將發佈我有的代碼,它有構建錯誤。這是我想要做的,但我真的不知道如何繼續。我已經碰了壁,真的不知道如何着手:C#DNS.GetHostEntry()..新的網絡編程新程序員

輸入一個地址(字符串) 使用相應的功能 打印出完整的主機信息

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); 
      } 
     } 
    } 
} 
+1

看起來這是給了家庭代碼與標有你應該填寫的地方一起工作。你需要什麼幫助?你有什麼嘗試? – dtb 2013-02-17 16:42:49

+0

「我將發佈我有的代碼,這些代碼存在錯誤。」你能指出異常嗎?我假設你缺少不是框架方法的'PrintHostInfo'方法。 – 2013-02-17 16:48:49

+0

讓我發佈一些更多的代碼,我有..目前的構建錯誤是printhostinfo方法,我不知道如果這將甚至做我所需要的。感謝您的期待,並試圖幫助我真的很感激它! – Cliff 2013-02-17 17:10:09

回答

0

您需要解析地址在host傳遞給Resolve方法,而不是hostInfo(即包含要解析主機字符串):

hostInfo = Dns.Resolve(host); 
+0

好的謝謝!現在我有零構建錯誤。我應該通過DNS.GetHostEntry()完成以下操作。輸入地址(作爲字符串) 使用相應的功能解析地址 打印出完整的主機信息 如果未返回任何錯誤,則全部打印主機信息。 如果返回錯誤,則打印描述性錯誤消息 – Cliff 2013-02-17 17:57:20

+0

好的,我將您的行更改爲此hostInfo = Dns.GetHostEntry(host); ..不管我輸入什麼網站,IP地址都不會改變。 – Cliff 2013-02-17 18:15:37

+0

任何幫助傢伙?我不知道該怎麼辦。我想我可以修改一些已添加的代碼,但我不確定要在註釋部分添加什麼代碼 – Cliff 2013-02-17 19:16:07