获得Java小程序正确的本地IP地址 [英] Get the correct local IP adress from java applet

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

问题描述

我想确定从我的Java小程序的本地IP地址。问题是,当有同一台机器,其中有局域网和互联网连接的几个IP不会忽略
(掌,VMWare的...)。

I would like to determine the local IP adress 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不是正确ADRESS。正确的是10.10.11.51。

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


编辑响应于<一href=\"http://stackoverflow.com/questions/1510526/get-the-correct-local-ip-adress-from-java-applet/1510642#1510642\">jarnbjo

您的水晶球说的是事实。你明白我的问题。客户端可以通过代理连接,所以我不能用你的第一个点。如果我在我的电脑下面执行此code:

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

和不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地址作为一个applet参数。至少如果你不这样做HTTP通过代理,网络服务器应能确定客户端的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.

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

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