通信----InetAddress类
程序员文章站
2022-05-14 08:06:03
...
InetAddress是用于管理IP的类、没有构造器
1.单例模式
2.根据静态的方法来返回该对象
//获取本机名+本机地址
InetAddress localHost=InetAddress.getLocalHost();
String hostName = localHost.getHostName();
String hostAddress = localHost.getHostAddress();
System.out.println("----------localHost:"+localHost);
System.out.println("主机名:"+hostName);
System.out.println("主机地址:"+hostAddress);
结果:
//获取远程ip
InetAddress bdIp=InetAddress.getByName("www.baidu.com");
String bdhostName=bdIp.getHostName();
String bdHostAddress=bdIp.getHostAddress();
System.out.println("主机名:"+bdhostName);
System.out.println("主机地址:"+bdHostAddress);
System.out.println("==========================================");
InetAddress bdIp2=InetAddress.getByName("14.215.177.38");
String bd2hostName=bdIp2.getHostName();
String bd2HostAddress=bdIp2.getHostAddress();
System.out.println("主机名:"+bd2hostName);
System.out.println("主机地址:"+bd2HostAddress);
结果:
最后一个是主机名与主机地址设置成一样所致的