简体   繁体   English

从Java applet获取正确的本地IP地址

[英]Get the correct local IP address from java applet

I would like to determine the local IP address from my java applet. 我想从我的java applet中确定本地IP地址。 The problem is when there are several IP adresses on the same machine, which has LAN and internet connections (palm, VMWare...). 问题是当同一台机器上有多个IP地址时,它们有LAN和互联网连接(掌上电脑,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. 其中xx.xx.xx.xx不是正确的地址。 The correct is 10.10.11.51. 正确的是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

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. 我的水晶球告诉我你的意思是客户端用来连接服务器的IP地址,applet是从该地址加载的。 If so, you have at least two possibilities: 如果是这样,您至少有两种可能性:

  • 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. 在服务器上,您可以将applet嵌入到动态生成的HTML页面中,并将客户端的IP地址添加为applet参数。 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. 至少如果您没有通过代理进行HTTP,Web服务器应该能够确定客户端的IP地址并将其传递给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: 在applet中,您可以打开一个TCP套接字到您加载applet的Web服务器,并检查用于连接的本地地址:

.

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

In bottom of getHostName() C function gethostbyname(). 在getHostName()的底部C函数gethostbyname()。 They initially looking to /etc/hosts, then try resolve through DNS. 他们最初寻找/ etc / hosts,然后尝试通过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... 所以,如果你将10.10.11.51 myhostname添加到/ etc / hosts中,getHostName()应该正确检测到它在windows中有类似于/ etc / hosts,\\ WINDOWS \\ System32 \\ Servises中的AFAIR等等......

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. 在您的代码中,您首先获得主机名(hostName = InetAddress.getLocalHost()。getHostName();)此函数返回安装系统时设置的计算机名称。 Then you get all IP of concrete host name (InetAddress.getAllByName(hostName)) - this return all IP resolved for this hostname 然后你得到具体主机名的所有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盒子的名字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM