IP地址(测试)

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

            //查看网站iP地址
            InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress4);

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


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

 


版权声明:本文为weixin_51797050原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。