从Java applet获取正确的本地IP地址 [英] Get the correct local IP address from java applet

查看:109
本文介绍了从Java applet获取正确的本地IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的java applet中确定本地IP地址。问题是当同一台机器上有多个IP地址时,它们有局域网和互联网连接
(掌上电脑,VMWare ......)。

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...).

这是我的测试:

    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();
      }
    }

结果是:

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

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

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

编辑以响应 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(); 

我有这样的结果:

    Internet IP = /127.0.0.1 

而不是10.10.11.51

And not 10.10.11.51

推荐答案

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

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:


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

  • 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.

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

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:

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

这篇关于从Java applet获取正确的本地IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆