加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java——InetAddress类

发布时间:2020-12-15 05:34:29 所属栏目:Java 来源:网络整理
导读:一、 InetAddress : 互联网协议 (IP) 地址;java.net 包; 二、获取对象方法: //没有构造方法; ( 1 ) static InetAddress?getLocalHost();获取本机IP 地址; //InetAddress lh = InetAddress.getLocalHost(); ( 2 ) static InetAddress?getByName(St

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读