无需连接到互联网即可获取本地IP地址 [英] Get local IP-Address without connecting to the internet

查看:241
本文介绍了无需连接到互联网即可获取本地IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在尝试在我的本地网络中获取我的机器的IP地址(应该是 192.168.178.41 )。

So, I'm trying to get the IP-Address of my machine in my local network (which should be 192.168.178.41).

我的第一个意图是使用这样的东西:

My first intention was to use something like this:

InetAddress.getLocalHost().getHostAddress();

但它只返回 127.0.0.1 ,这对我来说是正确但不是很有帮助。

but it only returns 127.0.0.1, which is correct but not very helpful for me.

我搜索了一下,发现了这个答案 https://stackoverflow.com/a/2381398/717341 ,它只是创建一个 Socket - 连接到某个网页(例如google。 com)并从套接字获取本地主机地址:

I searched around and found this answer https://stackoverflow.com/a/2381398/717341, which simply creates a Socket-connection to some web-page (e.g. "google.com") and gets the local host address from the socket:

Socket s = new Socket("google.com", 80);
System.out.println(s.getLocalAddress().getHostAddress());
s.close();

这适用于我的机器(它返回 192.168.178.41 ),但它需要连接到互联网才能工作。由于我的应用程序不需要互联网连接,并且每次启动时应用程序尝试连接到谷歌都可能看起来可疑,我不喜欢使用它的想法。

This does work for my machine (it returns 192.168.178.41), but it needs to connect to the internet in order to work. Since my application does not require an internet connection and it might look "suspicious" that the app tries to connect to google every time it is launched, I don't like the idea of using it.

所以,经过一些更多的研究后,我偶然发现了 NetworkInterface -class,其中(有一些工作)也会返回所需的IP地址:

So, after some more research I stumbled across the NetworkInterface-class, which (with some work) does also return the desired IP-Address:

Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()){
    NetworkInterface current = interfaces.nextElement();
    System.out.println(current);
    if (!current.isUp() || current.isLoopback() || current.isVirtual()) continue;
    Enumeration<InetAddress> addresses = current.getInetAddresses();
    while (addresses.hasMoreElements()){
        InetAddress current_addr = addresses.nextElement();
        if (current_addr.isLoopbackAddress()) continue;
        System.out.println(current_addr.getHostAddress());
    }
}

在我的机器上,返回以下内容:

On my machine, this returns the following:

name:eth1 (eth1)
fe80:0:0:0:226:4aff:fe0d:592e%3
192.168.178.41
name:lo (lo)

它找到我的网络接口并返回所需的IP,但我不确定其他地址( fe80:0:0:0:226:4aff:fe0d:592e%3 )是什么意思。

It finds both my network interfaces and returns the desired IP, but I'm not sure what the other address (fe80:0:0:0:226:4aff:fe0d:592e%3) means.

此外,我还没有找到一种方法从返回的地址中过滤它(使用 isXX() - InetAddress -object的方法 - 然后使用RegEx,我发现它非常脏。

Also, I haven't found a way to filter it from the returned addresses (by using the isXX()-methods of the InetAddress-object) other then using RegEx, which I find very "dirty".

除了使用RegEx或互联网之外的任何其他想法?

Any other thoughts than using either RegEx or the internet?

推荐答案

fe80:0:0:0:226:4aff:fe0d :592e是你的ipv6地址; - )。

fe80:0:0:0:226:4aff:fe0d:592e is your ipv6 address ;-).

使用

if (current_addr instanceof Inet4Address)
  System.out.println(current_addr.getHostAddress());
else if (current_addr instanceof Inet6Address)
  System.out.println(current_addr.getHostAddress());

如果您只关心IPv4,那么只需丢弃IPv6案例。但要注意,IPv6是未来的^^。

If you just care for IPv4, then just discard the IPv6 case. But beware, IPv6 is the future ^^.

PS:检查你的某些中断是否应该是继续 s。

P.S.: Check if some of your breaks should have been continues.

这篇关于无需连接到互联网即可获取本地IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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