java 从java小程序获取正确的本地IP地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1510526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:50:07  来源:igfitidea点击:

Get the correct local IP address from java applet

javanetworkingapplet

提问by EFalco

I would like to determine the local IP address from my java applet. The problem is when there are several IP adresses on the same machine, which has LAN and internet connections (palm, VMWare...).

我想从我的 java 小程序中确定本地 IP 地址。问题是当同一台机器上有多个 IP 地址时,该机器具有 LAN 和 Internet 连接(palm、VMWare...)。

Here is my test :

这是我的测试:

    public static void main(String[] args) {
      try {
        String hostName = InetAddress.getLocalHost().getHostName();
        System.out.println("HostName = " + hostName);
        System.out.println("HostAddressLocal = " +
          InetAddress.getLocalHost().getHostAddress());
        InetAddress[] inetAddresses = InetAddress.getAllByName(hostName);
        for (InetAddress inetAddress : inetAddresses) {
          System.out.println("hostAddress = " + inetAddress.getHostAddress());
        }
      } catch (Exception e) {
          e.printStackTrace();
      }
    }

The result is :

结果是:

    HostName = xxxx
    HostAddressLocal = xx.xx.xx.xx
    hostAddress = 10.10.11.51
    hostAddress = 192.168.23.1
    hostAddress = 192.168.106.1

where xx.xx.xx.xx isn't the correct address. The correct is 10.10.11.51.

其中 xx.xx.xx.xx 不是正确的地址。正确的是 10.10.11.51。



EDIT in response to jarnbjo:

编辑以回应jarnbjo

Your crystal ball say the truth. You've understand my problem. The client can connect through a proxy so I can not use your first point. If I execute this code below on my computer :

你的水晶球说的是实话。你已经明白我的问题了。客户端可以通过代理连接,所以我不能使用你的第一点。如果我在我的电脑上执行下面的这段代码:

    Socket s = new Socket("www.w3c.org", 80); 
    InetAddress ip = s.getLocalAddress(); 
    System.out.println("Internet IP = " + ip.toString()); 
    s.close(); 

I have this result :

我有这个结果:

    Internet IP = /127.0.0.1 

And not 10.10.11.51

而不是 10.10.11.51

回答by jarnbjo

As you've already discovered, a computer may very well have several network interfaces with different IP addresses and it's a little bit difficult to guess which one you consider to be "correct", as they are all actually correct.

正如您已经发现的那样,一台计算机很可能有多个具有不同 IP 地址的网络接口,并且很难猜测您认为哪个是“正确的”,因为它们实际上都是正确的。

My crystal ball suggest me that you mean the IP address, which the client is using to connect to the server, from which the applet was loaded. If so, you have at least two possibilities:

我的水晶球告诉我,你的意思是 IP 地址,客户端使用它连接到服务器,从中加载小程序。如果是这样,您至少有两种可能性:

  • On the server, you can embed the applet on a dynamically generated HTML page and add the client's IP address as an applet parameter. At least if you're not doing HTTP over a proxy, the web server should be able to determine the client's IP address and pass it on to the applet.

  • In the applet, you can open a TCP socket to the web server from which you loaded the applet and check which local address is being used for the connection:

  • 在服务器上,您可以将小程序嵌入动态生成的 HTML 页面,并将客户端的 IP 地址添加为小程序参数。至少如果您不通过代理执行 HTTP,Web 服务器应该能够确定客户端的 IP 地址并将其传递给小程序。

  • 在小程序中,您可以打开一个到您加载小程序的 Web 服务器的 TCP 套接字,并检查哪个本地地址用于连接:

.

.

Socket s = new Socket("www", 80);
InetAddress ip = s.getLocalAddress();
s.close();

回答by Alexey Sviridov

In bottom of getHostName() C function gethostbyname(). They initially looking to /etc/hosts, then try resolve through DNS. So, if you add 10.10.11.51 myhostname to /etc/hosts getHostName() should detect it correctly In windows there is analogue to /etc/hosts, AFAIR in \WINDOWS\System32\Servises or so...

在 getHostName() C 函数 gethostbyname() 的底部。他们最初寻找 /etc/hosts,然后尝试通过 DNS 解析。因此,如果您将 10.10.11.51 myhostname 添加到 /etc/hosts getHostName() 应该正确检测到它在 Windows 中有类似于 /etc/hosts,AFAIR 在 \WINDOWS\System32\Servises 左右......

This is ONLY name resolution problem.

这只是名称解析问题。

In your code you first get host name (hostName = InetAddress.getLocalHost().getHostName();) this function return your computer name setted when installing system was installed. Then you get all IP of concrete host name (InetAddress.getAllByName(hostName)) - this return all IP resolved for this hostname

在您的代码中,您首先获得主机名 (hostName = InetAddress.getLocalHost().getHostName();) 此函数返回安装系统时设置的计算机名称。然后您获得具体主机名的所有 IP (InetAddress.getAllByName(hostName)) - 这将返回为此主机名解析的所有 IP

Simple example

简单的例子

1 /etc/hosts like this

1 /etc/hosts 像这样

127.0.0.1   localhost
127.0.1.1   fred-desktop

your code return

你的代码返回

HostName = fred-desktop
HostAddressLocal = 127.0.1.1
hostAddress = 127.0.1.1

2 change /etc/hosts to look like

2 改变 /etc/hosts 看起来像

127.0.0.1   localhost
127.0.1.1   fred-desktop
192.168.1.1 fred-desktop
20.20.20.20 fred-desktop

your code will return

您的代码将返回

HostName = fred-desktop
HostAddressLocal = 127.0.1.1
hostAddress = 127.0.1.1
hostAddress = 192.168.1.1
hostAddress = 20.20.20.20

fred-desktop - name of my ubuntu box.

fred-desktop - 我的 ubuntu 盒子的名称。