我是新手程序員,我試圖檢查我自己的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());
}
}
僅供參考,一臺機器可能有多個IP地址,並且IP地址可能隨時間而改變。 – 2014-09-03 22:13:35
這裏的確切問題是什麼?哪一段代碼令人困惑? 「解釋代碼」不是一個很好的問題,特別是對於這樣一個大代碼塊。 – BradleyDotNET 2014-09-03 22:15:05
此外,checkip.dyndns.org報告的IP地址很可能不是您計算機的IP地址,而是您的路由器的IP地址。 – 2014-09-03 22:18:59