Java基础 -> IP,端口InetAddress类

   /**
     * ip地址:对应的地址类:InetAddress
     * ip不好记,出现了域名访问网站
     * 本地地址:127.0.0.1 对应:localhost
     */
    @Test
    public void test1() throws Exception {
        /**
         * static InetAddress[]	getAllByName(String host)
         * 根据主机的名称,根据系统上配置的名称服务返回其IP地址数组。
         * static InetAddress	getByAddress(byte[] addr)
         * 给定原始IP地址返回 InetAddress对象。
         * static InetAddress	getByAddress(String host, byte[] addr)
         * 根据提供的主机名和IP地址创建InetAddress。
         * static InetAddress	getByName(String host)
         * 根据主机名称确定主机的IP地址。
         */
        //InetAddress没有构造器,有getXxx方法创建实例类
        InetAddress inetAddress = InetAddress.getByName("192.168.10.1");
        System.out.println(inetAddress);///192.168.10.1---------多了一个/
        //ip不好记,出现了域名访问网站
        InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com");
        System.out.println(inetAddress1);//www.baidu.com/163.177.151.109
        //本地地址:127.0.0.1 对应:localhost
        InetAddress inetAddress2 = InetAddress.getByName("127.0.0.1");
        System.out.println(inetAddress2);///127.0.0.1
        //本地ip
        //static InetAddress	getLocalHost()
        //返回本地主机的地址。
        InetAddress inetAddress3 = InetAddress.getLocalHost();
        System.out.println(inetAddress3);//USER-20200906AI/10.212.10.194
        /**
         * String	getHostAddress()
         * 返回文本表示中的IP地址字符串。
         * String	getHostName()
         * 获取此IP地址的主机名。
         */
        String hostName = inetAddress1.getHostName();
        String hostAddress = inetAddress1.getHostAddress();
        System.out.println(hostName);//www.baidu.com
        System.out.println(hostAddress);//163.177.151.109
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值