上接 Java网络编程中的常用类-InetAddress的使用
此博客讲述了根据域名获取计算机信息时需要使用getByName(“域名”)方法创建InetAddress对象。
import java.net.InetAddress;
public class InetTest2 {
public static void main(String[] args)throws Exception {
//创建根据域名获取计算机信息的InetAddress对象
InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
//返回IP地址
System.out.println(inetAddress.getHostAddress());
//返回计算机名
System.out.println(inetAddress.getHostName());
}
}
小伙伴们把代码敲一敲啊!