如何在Java中找到所有本地绑定的IP地址? [英] How can I find all locally bound IP addresses in Java?
问题描述
我想拥有所有本地绑定的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屋!