如何在Java中找到所有本地绑定的IP地址? [英] How can I find all locally bound IP addresses in Java?

查看:76
本文介绍了如何在Java中找到所有本地绑定的IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想拥有所有本地绑定的IP地址,基本上是java.net.InetAddress的数组.我知道我可以用主机名调用InetAddress.getAllByName(),但是我想知道是否有更直接的方法不涉及DNS和/或主机名知识.

I would like to have all locally bound IP addresses, basically an array of java.net.InetAddress. I know I can call InetAddress.getAllByName() with the host name, but I was wondering if there is a more direct way that doesn't involve DNS and/or knowledge of the host name.

推荐答案

java.net.NetworkInterface.getNetworkInterfaces()将为您提供可用网络接口的枚举.然后,您可以在每个NetworkInterface上调用getInetAddresses()以获取绑定到该接口的地址.

java.net.NetworkInterface.getNetworkInterfaces() will give you an enumeration of the available network interfaces. You can then invoke getInetAddresses() on each NetworkInterface to get the addresses bound to this interface.

这篇关于如何在Java中找到所有本地绑定的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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