InetAddress-ip地址对象
作用
需要通过网络进行通信时,IP地址封装的对象,获得ip地址对象
常用方法
getLocalHost()
获取本机IP地址
InetAddress localHost = InetAddress.getLocalHost();//DESKTOP-A0MATN8/192.168.213.1
getByName(String domain)
查询域名对应IP地址,
InetAddress localHost = InetAddress.getByName("localhost");// localhost/127.0.0.1
InetAddress localHost1 = InetAddress.getByName("ruanwenfu");//UnknownHostException: ruanwenfu
InetAddress localHost2 = InetAddress.getByName("1.1.1.1");//传入ip获得ip地址对象,传入字符串无法解析就跑异常 /1.1.1.1
getHostAddress()
返回IP地址字符串
InetAddress localHost = InetAddress.getByName("localhost");
System.out.println(localHost.getHostAddress()); //127.0.0.1
getHostName()
获取主机名
String localHost = InetAddress.getByName("localhost").getHostName();