2014-09-03 78 views
-3

我是新手程序員,我試圖檢查我自己的IP地址。問題是我不喜歡複製粘貼的東西,我真的需要了解我在做什麼。在谷歌的幫助下,我認爲我理解了大部分內容,但仍然存在一些不太明確的內容。我會非常感謝解釋。我沒有得到的東西都指向了評論。此外,我會很高興有人可以檢查我的想法(在評論中)是否正確。檢查您自己的IP地址

using UnityEngine; 
using System.Collections; 
using System.Net; 
/* 
* The System.Net namespace provides a simple programming interface for many of the protocols used on networks today. 
* The WebRequest and WebResponse classes form the basis of what are called pluggable protocols, an implementation of network services that enables you to develop applications that use Internet resources without worrying about the specific details of the individual protocols. 
* Classes in the System.Net namespace can be used to develop Windows Store apps or desktop apps. When used in a Windows Store app, classes in the System.Net namespace are affected by network isolation feature, part of the application security model used by the Windows Developer Preview. 
* The appropriate network capabilities must be enabled in the app manifest for a Windows Store app for the system to allow network access by a Windows Store app. For more information, see the Network Isolation for Windows Store Apps. 
*/ 
using System.IO; 
//The System.IO namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support. 
public class PokazIP : MonoBehaviour { 

    public string GetLocalIP() 
    { 
     string localIP = "?";//New variable of string type 
     IPHostEntry host;//New variable of IPHostEntry type learn more: http://msdn.microsoft.com/en-us/library/system.net.iphostentry%28v=vs.110%29.aspx 
     //IPHostEntry -> Provides a container class for Internet host address information. One of its properties is AddressList. 
     //AddressList -> Gets or sets a list of IP addresses that are associated with a host. 
     //Learn more http://msdn.microsoft.com/en-us/library/system.net.iphostentry.addresslist%28v=vs.110%29.aspx 
     host = Dns.GetHostEntry(Dns.GetHostName()); 
     /*Dns. a class of System.Net(System.Net.Dns) which provides simple domain name resolution functionality 
     learn more: http://msdn.microsoft.com/en-us/library/system.net.dns%28v=vs.110%29.aspx 
     .GetHostEntry(hostNameOrAddress -> Type: System.String -> The host name or IP address to resolve.) 
     * method of Dns class which resolves a host name or IP address to an IPHostEntry instance. (Actually I dont really understand this discription.) 
     * Return Value -> Type: System.Net.IPHostEntry -> An IPHostEntry instance that contains address information about the host specified in hostNameOrAddress. 
     * learn more: http://msdn.microsoft.com/en-us/library/ms143998%28v=vs.110%29.aspx 
     .GetHostName() method of Dns class which gets the host name of the local computer. 
     * Return Value -> Type: System.String -> A string that contains the DNS host name of the local computer. 
     * learn more: http://msdn.microsoft.com/en-us/library/system.net.dns.gethostname%28v=vs.110%29.aspx 
     */ 
     foreach (IPAddress ip in host.AddressList)//**New variable called ip of type IPAddress. host.AddressList is made of IPAddress objects???** 
     //Properties of IPAdress: Address and AddressFamily 
     //learn more http://msdn.microsoft.com/en-us/library/system.net.ipaddress%28v=vs.110%29.aspx 
     { 
      if (ip.AddressFamily.ToString() == "InterNetwork")//**Whats InterNetwork? Why do we look for that in ip?** 
      { 
       localIP = ip.ToString();//obvious 
       break;//**so if it finds "InterNetwork" it is not looking further 'coz its not needed?** 
      } 
     } 
     return localIP;//returns IP 
    } 
    public string GetPublicIP() 
     /* 
     * This funcion is using checkip server to tell you yours IP. 
     * I guess it is because for some reason we cant do it from our computer. Why? 
     */ 
    { 
     string direction = "";//New variable of type string 
     WebRequest request = WebRequest.Create("http://checkip.dyndns.org/"); 
     /* 
     * WebRequest.Create Method (String) 
     * Initializes a new WebRequest instance for the specified URI scheme. 
     * http://msdn.microsoft.com/en-us/library/bw00b1dc%28v=vs.110%29.aspx 
     * WebRequest is a class of System.Net 
     * Makes a request to a Uniform Resource Identifier (URI). This is an abstract class. <- I dont really get it to many unknown things :S 
     * http://msdn.microsoft.com/en-us/library/system.net.webrequest%28v=vs.110%29.aspx 
     */ 
     using (WebResponse response = request.GetResponse()) 
     /* 
     * Why "using"? 
     * WebResponse is a class of System.Net 
     * Provides a response from a Uniform Resource Identifier (URI). This is an abstract class. <- I dont really get it to many unknown things :S 
     * http://msdn.microsoft.com/en-us/library/system.net.webresponse%28v=vs.110%29.aspx 
     * GetResponse() is a method of WebRequest 
     * When overridden in a descendant class, returns a response to an Internet request. 
     */ 
     using (StreamReader stream = new StreamReader(response.GetResponseStream())) 
     //Again why using? 
     //Creating new StreamReader object named stream with value response.GetResponseStream() 
     //.GetResponseStream() is WebResponse. method 
     //When overridden in a descendant class, returns the data stream from the Internet resource. 
     { 
      direction = stream.ReadToEnd(); 
      //StreamReader.ReadToEnd Method 
      //Reads all characters from the current position to the end of the stream. 
     } 
     //This is how the web page looks like 
     //<html><head><title>Current IP Check</title></head><body>Current IP Address: 89.71.166.131</body></html> 
     int first = direction.IndexOf("Address: ") + 9;//"Address: " it is 9 chars 
     int last = direction.LastIndexOf("</body>"); 
     direction = direction.Substring(first, last - first); 

     return direction; 
    } 
    void OnGUI()//Displaying it on the "screen" 
    { 
     GUI.Label(new Rect(80f, 50f, 100f, 25f), GetLocalIP()); 
     GUI.Label(new Rect(80f, 100f, 100f, 25f), GetPublicIP()); 
    } 
} 
+2

僅供參考,一臺機器可能有多個IP地址,並且IP地址可能隨時間而改變。 – 2014-09-03 22:13:35

+3

這裏的確切問題是什麼?哪一段代碼令人困惑? 「解釋代碼」不是一個很好的問題,特別是對於這樣一個大代碼塊。 – BradleyDotNET 2014-09-03 22:15:05

+0

此外,checkip.dyndns.org報告的IP地址很可能不是您計算機的IP地址,而是您的路由器的IP地址。 – 2014-09-03 22:18:59

回答

1

「InterNetwork」是指IPv4參見System.Net.Sockets.AddressFamily。

John Saunders說的問題是您的計算機可能有多個ip,例如,您可能有一個連接到網絡的wifi卡和通過以太網電纜連接到另一個網絡的以太網卡。

第二種方法:「GetPublicIP()」,嘗試連接到遠程服務器以查找您的IP。 今天大多數有互聯網的房屋都使用路由器連接到互聯網,這就是爲什麼你無法從你的電腦(你的路由器擁有公共IP而不是你的電腦)獲得外部或公共IP的原因。

+0

嗯,這不是因爲有路由器,這是問題。如果路由器配置爲使用NAT或某種其他形式的地址映射,以便內部和外部IP地址不同,則問題將出在此處。 – 2014-09-03 22:16:17

+0

的確如此,但我沒有看到有路由器的意義呢? – user1509229 2014-09-03 22:18:01

+0

路由器的目的是將流量從一個子網路由到另一個靠近目的地的路由。 – 2014-09-03 23:04:08

-1

首先說實話,很少有人會閱讀你的所有評論並回答他們。這可能更適合於https://meta.stackoverflow.com/。他們專注於高層次的對話,而不是實際的代碼問題。

但要回答您的一個重複問題。我看到你經常問「爲什麼要用」。那麼當代碼離開using語句時,C#中的using語句會自動關閉連接。所以你會經常看到它與連接相關的對象(即sqlconnection或streamreader等)一起使用。如果沒有使用,用戶需要顯式處理這些對象來關閉連接。另外作爲一個附註,即使代碼在using語句中失敗,它仍然會處理/關閉正在使用的對象。

+2

-1:不,這類問題不適合[meta]。 [meta]是關於[so]的問題。 – 2014-09-03 23:04:56

+0

哦,猜猜我錯了,謝謝你讓我知道。我認爲有一些堆棧交換站點致力於設計問題/學習者。 – mgmedick 2014-09-04 14:09:21