简体   繁体   English

获取本地IP地址的主机名(java)

[英]Getting the Host name of a Local IP address (java)

I use this method in a loop to get the host name of 4 terminal in my local network identified by the ip address terminal[i].getIp() . 我在循环中使用此方法来获取本地网络中4个终端的主机名,该主机由ip地址terminal [i] .getIp()标识

try {
 // Get hostname by textual representation of IP address
 InetAddress addr = InetAddress.getByName(terminal[i].getIp());

 // Get the host name
 String hostname = addr.getHostName();
} catch (UnknownHostException e) {
}

the problem here its that code take so long to return the result (up to 5 seconds) I wonder if there is another more optimized method. 这里的问题是代码要花很长时间才能返回结果(最多5秒),我想知道是否还有另一个更优化的方法。

Try the library from Google Guava, I think there are more optimized 尝试使用Google Guava的库,我认为还有更多优化的库

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/net/InetAddresses.html http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/net/InetAddresses.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM