InetAddress and InetSocketAddress

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对象 */

System.out.println(is.getHostAddress());

上一篇:JavaScript中的this指向


下一篇:CH103 Cinema 题解报告