ip地址:InetAddress
- 唯一定位一台网络上的计算机
- 127.0.0.1:本机localhost
ip地址的分类
ipv4/ipv6
- IPV4:4个字节组成,0~255,约42亿个
IPV6:128位,8个无符号整数,例如
240e:1234:abcd:0010:2b58:9900:add2:b156
公网(互联网)--私网(局域网)
- ABCD段
package com.network.lesson01;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @Author suaxi
* @Date 2020/11/20 16:54
*/
public class TestInetAddress {
public static void main(String[] args) {
try {
//查询本机地址
InetAddress host1 = InetAddress.getByName("localhost");
System.out.println(host1);
//查询网站ip地址
InetAddress host2 = InetAddress.getByName("www.baidu.com");
System.out.println(host2);
//常用方法
System.out.println(host2.getAddress());
System.out.println(host2.getCanonicalHostName()); //规范的名字
System.out.println(host2.getHostAddress()); //ip
System.out.println(host2.getHostName()); //域名或自己电脑的名字
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
评论 (0)