ip地址

发布时间 2023-03-22 21:12:53作者: 惊鸿宴远赴人间
package edu.wtbu;

import java.net.InetAddress;
import java.net.UnknownHostException;

//测试ip
/*
ip地址:IntAddress
唯一定位一台计算机
127.0.0.1:本机localhost
IP地址的分类
ipv4:127.0.0.1,4个字节组成,0~255,42亿个,30亿都在北美,亚洲4亿,2011年用尽
ipv6:128位,8个无符号整数,如2001:0bb2:aaaa:cccc:1aaa:2cccc:3bbb:1314

公网(互联网),私网(局域网)
ABCD类分类:A:0~127 B:128~196 C:196~228 D:228~255
192.168.xx.xx,专门给组织内部使用

域名:记忆ip问题
ip:www.vip.com

*/
public class Demo01 {
public static void main(String[] args) {
try {
//查询本机地址
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
InetAddress inetAddress3 = InetAddress.getByName("localhost");
System.out.println(inetAddress3);
InetAddress inetAddress4 = InetAddress.getLocalHost();
System.out.println(inetAddress4);

//查询网站ip地址
InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress2);

//常用方法
//System.out.println(inetAddress2.getAddress());获得一组地址,一般不用
System.out.println(inetAddress2.getCanonicalHostName());//获得规范名
System.out.println(inetAddress2.getHostAddress());//获得主机ip地址
System.out.println(inetAddress2.getHostName());//获得主机名
} catch (UnknownHostException e) {
throw new RuntimeException(e);
}
}
}