IP
IP地址:InetAddress
-
唯一定位一台网络上的计算机
-
127.0.0.1 : 本机localhost
-
ip地址的分类
-
ipv4 / ipv6
-
ipv4 : 127.0.0.1 ,4个字节组成 ,一位 0~255,42亿(30亿都在北美,亚洲4亿,2011年就用尽)
-
ipv6 : 128位,8个无符号整数
2001:0bb2:aaaa:2aaa:0000:0000:1213:0001
-
-
公网(互联网)-私网(局域网)
-
ABCD类地址(相连两个之间折半)
-
192.168.xx.xx,专门给组织内部使用的
-
-
-
域名:记忆IP问题
-
IP: www.vip.com
-
package com.xue.lesson01;
import java.net.InetAddress;
import java.net.UnknownHostException;
//测试IP
public class IP {
public static void main(String[] args) {
InetAddress InetAddress01 = null;
InetAddress InetAddress02 = null;
InetAddress InetAddress03 = null;
InetAddress InetAddress04 = null;
try {
//查询本机地址
InetAddress01 = InetAddress.getByName("127.0.0.1");
InetAddress02 = InetAddress.getByName("localhost");
InetAddress03 = InetAddress.getLocalHost();
//查询网站IP地址
InetAddress04 = InetAddress.getByName("www.baidu.com");
} catch (UnknownHostException e) {
e.printStackTrace();
}
System.out.println(InetAddress01);
System.out.println(InetAddress02);
System.out.println(InetAddress03);
System.out.println(InetAddress04);
//常用方法
System.out.println(InetAddress04.getAddress());
System.out.println(InetAddress04.getCanonicalHostName());//规范的名字
System.out.println(InetAddress04.getHostAddress());//IP
System.out.println(InetAddress04.getHostName());//域名,或者自己电脑的名字
}
}