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

发布于 2024-08-06 00:54:00 字数 1460 浏览 3 评论 0原文

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


编辑响应 jarnbjo

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

    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

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

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.


EDIT in response to 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黒涩兲箜 2024-08-13 00:54:00

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

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

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

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

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

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.

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:

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

.

Socket s = new Socket("www", 80);
InetAddress ip = s.getLocalAddress();
s.close();
淑女气质 2024-08-13 00:54:00

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

这只是名称解析问题。

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

简单示例

1 /etc/hosts 像这样

127.0.0.1   localhost
127.0.1.1   fred-desktop

你的代码返回

HostName = fred-desktop
HostAddressLocal = 127.0.1.1
hostAddress = 127.0.1.1

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

你的代码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 - 我的 ubuntu 盒子的名称。

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

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

Simple example

1 /etc/hosts like this

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文