Java网络编程-16

网络层级

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

//测试IP
public class Demo {
    public static void main(String[] args) throws UnknownHostException {
        //查询本机地址
        InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
        System.out.println(inetAddress);

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

        //常用方法
        System.out.println(inetAddress2.getHostAddress());//IP
        System.out.println(inetAddress2.getHostName());//域名
    }
}