结果
上面的代码示例将产生以下结果。
Host name is: 100.67.125.74.bc.googleusercontent.com
Ip address is: 74.125.67.100
以下是从 IP 地址查找主机名的示例
import java.net.InetAddress;
import java.net.UnknownHostException;
public class NewClass1 {
public static void main(String[] args) {
InetAddress ip;
String hostname;
try {
ip = InetAddress.getLocalHost();
hostname = ip.getHostName();
System.out.println("Your current IP address : " + ip);
System.out.println("Your current Hostname : " + hostname);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
上面的代码示例将产生以下结果。
Your current IP address : localhost/127.0.0.1
Your current Hostname : localhost