0 序
1 java.net包概述
2 关键类及源码分析
2.1 java.net.InetAddress
public static void main(String[] args) throws UnknownHostException {
// 1.获取本机的InetAddress对象
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost); // LAPTOP-RVFFB7FM/192.168.23.1
// 2.根据机器的名字获取InetAddress对象
InetAddress inetAddress = InetAddress.getByName("C345678");
System.out.println(inetAddress);// LAPTOP-RVFFB7FM/192.168.23.1
// 3.根据域名返回InetAddress对象,比如www.baidu.com
InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress1);// www.baidu.com/120.232.145.185
// 4.根据InetAddress对象,获取对应的ip地址
String address = inetAddress1.getHostAddress();
System.out.println(address);// 120.232.145.185
// 5.通过InetAddress对象,获取对应的主机名
String hostName = inetAddress1.getHostName();
System.out.println(hostName);// www.baidu.com
}
标签:www,java,System,源码,InetAddress,println,类及,out
From: https://www.cnblogs.com/johnnyzen/p/18126108