public class InetAddressDemo {
public static void main(String[] args) throws UnknownHostException {
// public static InetAddress getByName(String host)|确定主机名称的IP地址。主机名称可以是机器名称,也可以是IP地址
InetAddress address = InetAddress.getByName("logan");
// InetAddress address = InetAddress.getByName("222.195.66.219");
// public String getHostName()| 获取此IP地址的主机名
String name = address.getHostName();
//public String getHostAddress()|返回文本显示中的IP地址字符串
String ip = address.getHostAddress();
System.out.println("主机名:"+ name);
System.out.println("地址:"+ip);
}
}
代码运行如下
为什么在getByName中传入地址后
public class InetAddressDemo { public static void main(String[] args) throws UnknownHostException { // public static InetAddress getByName(String host)|确定主机名称的IP地址。主机名称可以是机器名称,也可以是IP地址 // InetAddress address = InetAddress.getByName("logan"); InetAddress address = InetAddress.getByName("222.195.66.219"); // public String getHostName()| 获取此IP地址的主机名 String name = address.getHostName(); //public String getHostAddress()|返回文本显示中的IP地址字符串 String ip = address.getHostAddress(); System.out.println("主机名:"+ name); System.out.println("地址:"+ip); } }
发现主机名显示的也是地址。希望路过的大佬可以在评论中解答一下,十分感谢!