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

查看:23
本文介绍了无需连接互联网即可获取本地 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 类,它(通过一些工作)也返回所需的 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.

另外,我还没有找到从返回的地址中过滤它的方法(通过使用 InetAddress-object 的 isXX()-methods),除此之外使用 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 ^^.

P.S.:检查您的某些 break 是否应该是 continues.

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

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

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