IP地址

IP地址:InetAddress(没有构造器,通过静态方法返回) java.net包下

  • 唯一定位一台网络上的计算机

  • 127.0.0.1:本机localhost

  • ip地址的分类

    • IPV4/IPV6
      • IPV4:192.168.194.1 32位
      • IPV6: 2409:8a55:227a:83c0:d028:ff84:6d2c:dee8 128位
    • 公网(互联网)/私网(局域网)
      • ABCD类地址
      • 192.168.xx.xx专门给组织内部使用
  • 域名:

    • ping ip/域名
      IP地址,InetAddress类的使用_java

    • ipconfig

测试InetAddress:

package net.study;

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

// 测试IP
public class TestInetAddress {
    public static void main(String[] args) {
        try {
            // 查询本机地址
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            System.out.println(inetAddress);
            InetAddress inetAddress2 = InetAddress.getByName("localhost");
            System.out.println(inetAddress2);
            InetAddress inetAddress3 = InetAddress.getLocalHost();
            System.out.println(inetAddress3);

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

            // 常用方法
            System.out.println(inetAddress.getAddress());
            System.out.println(inetAddress.getCanonicalHostName());  // 规范的名字
            System.out.println(inetAddress.getHostAddress());  // ip
            System.out.println(inetAddress.getHostName());  // 域名,或者自己电脑的名字



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