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

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

问题描述

我想从我的 java 小程序中确定本地 IP 地址.问题是当同一台机器上有多个 IP 地址时,有 LAN 和 Internet 连接(掌上电脑、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:

  • 在服务器上,您可以将小程序嵌入动态生成的 HTML 页面,并将客户端的 IP 地址添加为小程序参数.至少,如果您不通过代理执行 HTTP,Web 服务器应该能够确定客户端的 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.

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

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小程序获取正确的本地IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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