内部IP地址和外部IP地址之间的区别

时间:2011-04-04 20:09:12

标签: c# java networking network-programming network-protocols

有谁能告诉我内部IP地址和外部IP地址有什么区别?如何使用Java,C#或Adobe AIR等任何编程语言?

2 个答案:

答案 0 :(得分:1)

内部IP地址是您网络中的地址:

IPHostEntry heserver = Dns.GetHostEntry(Dns.GetHostName());
IPAddress curAdd = heserver.AddressList[0];
curAdd.ToString();

您的外部IP地址是ISP的地址

string ip = new 
     System.Net.WebClient()
      .DownloadString(("http://www.whatismyip.com/automation/n09230945.asp"));

答案 1 :(得分:1)

您可以使用以下代码(在java中)获取本地IP地址:

public String getLocalIpAddress() {
     try {
         for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
             NetworkInterface ni = en.nextElement();
             for (Enumeration enumIpAddr = ni.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                 InetAddress inetAddress = enumIpAddr.nextElement();
                 if (!inetAddress.isLoopbackAddress()) { //ignore 127.0.0.1
                     return inetAddress.getHostAddress().toString();
                 }
             }
         }
     } catch (SocketException ex) {
     }
     return null;
 }
相关问题