Java——InetAddress类

一、InetAddress互联网协议 (IP) 地址;java.net包;

二、获取对象方法:

  //没有构造方法;

1static InetAddress getLocalHost();获取本机IP地址;//InetAddress lh = InetAddress.getLocalHost();

2static InetAddress getByName(String host);根据主机名获取IP地址;

3static InetAddress[] getAllByName(String host);根据主机名获取多个IP地址;

public class Test {
    public static void main(String[] args) throws UnknownHostException {
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println(localHost);
        InetAddress byName = InetAddress.getByName("DESKTOP");
        System.out.println(byName);
        InetAddress[] allByName = InetAddress.getAllByName("DESKTOP");
        System.out.println(Arrays.toString(allByName));
    }
}

三、方法:

1String getHostName();获取此IP的主机名;//lh.getHostName();

2String getHostAddress();获取IP的字符串类型;

public class Test {
    public static void main(String[] args) throws UnknownHostException {
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println(localHost.getHostName());  //DESKTOP
        System.out.println(localHost.getHostAddress());  //192.168.0.0
    }
}

 

posted @ 2019-07-28 16:26  开拖拉机的拉风少年  阅读(221)  评论(0编辑  收藏  举报