网络编程_InetAddress


package com.qiliang.dmeo14_网络编程.Demo01;

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

public class InetAddressTest {
    public static void main(String[] args) {
        try {
            // 查询本机地址
            InetAddress inetAddresse1 = InetAddress.getByName("127.0.0.1");
            System.out.println(inetAddresse1); //  /127.0.0.1

            InetAddress inetAddresse3 = InetAddress.getByName("localhost");
            System.out.println(inetAddresse3); // localhost/127.0.0.1

            InetAddress inetAddresse4 = InetAddress.getLocalHost();
            System.out.println(inetAddresse4); // SKY-20180427LZX/192.168.74.1

            // 查询网络IP地址
            InetAddress inetAddresse2 = InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddresse2); //  www.baidu.com/36.152.44.95

            // 常用方法
            System.out.println(inetAddresse2.getAddress()); // [B@1540e19d
            System.out.println(inetAddresse2.getCanonicalHostName());
                // 规范的名字 36.152.44.95
            System.out.println(inetAddresse2.getHostAddress());
                // ip 36.152.44.95
            System.out.println(inetAddresse2.getHostName());
                // 域名,或者自己电脑的名字  www.baidu.com
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
上一篇:Java网络编程一


下一篇:java获取本机IP