码迷,mamicode.com
首页 > 编程语言 > 详细

Java——InetAddress类

时间:2019-07-28 17:55:16      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:str   main   get   array   system   public   test   localhost   throw   

一、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
    }
}

 

Java——InetAddress类

标签:str   main   get   array   system   public   test   localhost   throw   

原文地址:https://www.cnblogs.com/Tractors/p/11259464.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!