【方法一】static InetAddress getLocalHost():封装本地主机为一个IP地址对象
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);

【方法二】static InetAddress getByName(String host):根据本机名获取本机信息,有重复,默认返回第一个
InetAddress ip = InetAddress.getByName("192.168.13.28");
System.out.println(ip);
InetAddress name = InetAddress.getByName("LAPTOP-71O3944V");
System.out.println(name);

【方法三】string getHostName():获取IP地址对象的主机名
InetAddress localHost = InetAddress.getLocalHost();
String hostName = localHost.getHostName();
System.out.println(hostName);
【方法四】string getHostAddress():获取IP地址对象的字符串形式IP值
InetAddress localHost = InetAddress.getLocalHost();
String ip = localHost.getHostAddress();
System.out.println(ip);

【方法五】boolean isReachable(int millionSeconds):在指定毫秒内,当前主机与对应ip是否能连通
InetAddress baidu = InetAddress.getByName("baidu.com");
boolean reachable = baidu.isReachable(1000);
System.out.println("本机与百度网络连通状态:" + reachable);

首次发布
hezhongying.blog.csdn.net/article/det…