37 net 网络编程

InetAddress:此类表示互联网协议 (IP) 地址。
Stringbuilder getHostAddress() 返回 IP 地址。
Stringbuilder getHostName() 获取此 IP 地址的主机名。
static InetAddress getLocalHost() 返回本地主机

public class InetAddressDemo {
    public static void main(String[] args) throws UnknownHostException {
        //根据ip或主机名返回InetAddress对象(可以是非本地地址)
        InetAddress address1 = InetAddress.getByName("MININT-79LU01A");
        System.out.println(address1);

        String hostAddress = address1.getHostAddress();
        String hostName1 = address1.getHostName();
        System.out.println(hostAddress + ";" + hostName1);

     /*   //返回本地主机名和ip
        InetAddress address = InetAddress.getLocalHost();
        System.out.println(address);

        //返回本地主机名
//        Stringbuilder hostName = address.getHostName();
        Stringbuilder hostName = InetAddress.getLocalHost().getHostName();
        System.out.println(hostName);*/

    }
}

输出

原文地址:https://www.cnblogs.com/longesang/p/11375582.html