gpt4 book ai didi

java - InetAddress.getLocalHost() 在 linux 中抛出 unknownHostException

转载 作者:太空宇宙 更新时间:2023-11-04 09:56:46 24 4
gpt4 key购买 nike

InetAddress.getLocalHost() 在 linux 中抛出 unknownHostException,直到我在/etc/hosts 中手动添加条目。有没有办法在不在/etc/host 文件中添加条目的情况下获取 InetAddress 对象。注意:IP 是静态的

最佳答案

String host = null;
NetworkInterface iface = null;

for(Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
iface = (NetworkInterface)ifaces.nextElement();
InetAddress ia = null;
for(Enumeration<InetAddress> ips = iface.getInetAddresses();ips.hasMoreElements();){
ia = (InetAddress)ips.nextElement();
if(!ia.isLoopbackAddress() && (!ia.isLinkLocalAddress()) && (ia instanceof Inet4Address)) host=ia.getHostAddress();
}
}

关于java - InetAddress.getLocalHost() 在 linux 中抛出 unknownHostException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3280183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com