InetAddress类
●相关方法
1.获取本机InetAddress对象 getLocalHost
2.根据指定主机名/域名获取ip地址对象 getByName
3.获取InetAddress对象的主机名 getHostName
4.获取InetAddress对象的地址 getHostAddress
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author INSKYBE
* 2022/11/4
* 演示InetAddress 类的使用
*/
public class API_ {
public static void main(String[] args) throws UnknownHostException {
//获取本机的InetAddress对象
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);//INSKYBE/10.67.111.255
//根据指定主机名,获取 InetAddress 对象
InetAddress host1 = InetAddress.getByName("INSKYBE");
System.out.println("host1: " + host1);//host1: INSKYBE/10.67.111.255
//根据域名,返回一个InetAddress 对象,
InetAddress host2 = InetAddress.getByName("www.baidu.com");
System.out.println("host2: " + host2);//host2: www.baidu.com/112.80.248.75
//通过InetAddress 对象,获取对应的地址
String hostAddress = host2.getHostAddress();
System.out.println("host2 对应ip = " + hostAddress);//host2 对应ip = 112.80.248.76
//通过InetAddress对象,获取对应的主机名/或者域名
String hostName = host2.getHostName();
System.out.println("host2对应的主机名/域名=" + hostName);//host2对应的主机名/域名=www.baidu.com
}
}