IP地址

ip地址

  • 唯一定位一台网络上计算机
  • 127.0.0.1: 本机localhost
  • ip地址的分类
    • ipv4 ipv6
      • IPV4 127.0.0.1 四个字节组成 0~255 42亿 ;30亿都在北美,亚洲4亿.2011年就用尽
      • IPV6 128位,8个无符号整数 abcde
    • 公网(互联网)-私网(局域网)
      • ABCD类地址
      • 192.168.xx.xx专门给组织内部使用的
  • 域名(解决记忆问题!)
    • IP: www.vlp.com(一个域名)
package lesson01;

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

//测试IP
public class TestInetAdress {
    public static void main(String[] args) {
        try {
            InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");//本机的ip
            System.out.println(inetAddress1);

            InetAddress inetAddress2 = InetAddress.getByName("localhost");//本机的ip
            System.out.println(inetAddress2);

            InetAddress inetAddress3 = InetAddress.getLocalHost();//本机的ip
            System.out.println(inetAddress3);
            //查询网站是ip地址
            InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress4);

            //常用方法
            System.out.println(inetAddress4.getAddress());
            System.out.println(inetAddress4.getCanonicalHostName());//规范的名字
            System.out.println(inetAddress4.getHostAddress());//ip
            System.out.println(inetAddress4.getHostName());//域名

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}