InetAddress和InetSocketAddress

时间:2018-05-01 13:32:01   收藏:0   阅读:344

1、InetAddress(包含IP地址及主机名)

InetAddress is = InetAddress.getLocalHost();  // 通过静态方法获得本机的对象

System.out.println(is.getHostAddress());  // 获取IP地址

System.out.println(is.getHostName());  // 获取主机名

is = InetAddress.getByName("www.qq.com");  // 通过主机名创建对象

is = InetAddress.getByName("59.37.96.63");  // 通过IP地址创建对象

2、InetSocketAddress(在InetAdress的基础上加入端口)

InetSocketAddress isa = new InetSocketAddress("192.168.0.5", 8888);  // 通过IP地址及端口实例化

System.out.println(isa.getHostName());

System.out.println(isa.getPort());  // 获取端口

InetAddress ia = isa.getAddress();  // 获取InetAddress对象

原文:https://www.cnblogs.com/ss-123/p/8975879.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!