Java 版本的 InetAddress Inet4Address or Inet6Address 基本用法

35 篇文章 1 订阅

Java中InetAddress的基本用法, 可用于判断地址为IPv4 or IPv6

InetAddress address = InetAddress.getByName(host);
System.out.println("IP: " + address.getHostAddress());
int length = address.getAddress().length;
System.out.println("IP Address length: " + length);
//根据byte数组长度判断这个IP地址是IPv4/IPv6地址: length为4表示IPv4, 为16表示Ipv6

//使用instanceof判断这个IP地址是IPv4/IPv6地址
if (address instanceof Inet4Address) {
    System.out.println("This is IPv4 address according to instanceof !");
} else if (address instanceof Inet6Address) {
    System.out.println("This is IPv6 address according to instanceof !");
}
package com.hulk.http.util;

import com.mobile.emm.debugger.Log;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

/**
 * InetAddress单元测试
 * @author: zhanghao
 * @Time: 2021-06-19 16:43
 */
public class InetAddressTest {

    private static final String TAG = "InetAddressTest";

    public static void main(String[] args) {
        System.out.println("InetAddressTest.main: args= " + Arrays.toString(args));
        try {
            //测试v4域名
            test("www.baidu.com");
            //"www.baidu.com"
            test("220.181.38.150");
            test("www.qianxin.com");

            //IPV6测试
            test("www.neu6.edu.cn");
            test("2001:da8:9000:e013:0:0:199:4");;

            //测试内网地址,如果域名地址无法解析,
            test("10.195.155.217");
            test("www.hahaha.hulk.com");
        } catch (Exception e) {
            System.err.println("test failed: " + e);
        }
    }

    public static void test(String host) {
        System.out.println("\n>>>>test host: " + host);
        InetAddress address = null;
        try {
            address = InetAddress.getByName(host);
        } catch (Exception e) {
            Log.e(TAG, "test UnknownHostException: " + e);
        }
        if (address == null) {
            System.err.println("test: Failed, address is null, for host " + host);
            return;
        }
        System.out.println("IP: " + address.getHostAddress());
        //根据byte数组长度判断这个IP地址是IPv4/IPv6地址!
        int length = address.getAddress().length;
        System.out.println("IP Address length: " + length);
        switch (length) {
            case 4:
                System.out.println("This is IPv4 address according to byte array length !");
                break;
            case 16:
                System.out.println("This is IPv6 address according to byte array length !");
                break;
            default:
                System.out.println("Unknown address length " + length);
                break;
        }
        //使用instanceof判断这个IP地址是IPv4/IPv6地址
        if (address instanceof Inet4Address) {
            System.out.println("This is IPv4 address according to instanceof !");
        } else if (address instanceof Inet6Address) {
            System.out.println("This is IPv6 address according to instanceof !");
        }
    }
}

以上代码运行结果如下:

com.mobile.emm.http.util.InetAddressTest
InetAddressTest.main: args= []

>>>>test net host: www.baidu.com
IP: 220.181.38.150
IP Address length: 4
This is IPv4 address according to byte array length !
This is IPv4 address according to instanceof !

>>>>test net host: 220.181.38.150
IP: 220.181.38.150
IP Address length: 4
This is IPv4 address according to byte array length !
This is IPv4 address according to instanceof !

>>>>test net host: www.qianxin.com
IP: 61.160.224.57
IP Address length: 4
This is IPv4 address according to byte array length !
This is IPv4 address according to instanceof !

>>>>test net host: www.neu6.edu.cn
IP: 2001:da8:9000:e013:0:0:199:4
IP Address length: 16
This is IPv6 address according to byte array length !
This is IPv6 address according to instanceof !

>>>>test net host: 2001:da8:9000:e013:0:0:199:4
IP: 2001:da8:9000:e013:0:0:199:4
IP Address length: 16
This is IPv6 address according to byte array length !
This is IPv6 address according to instanceof !

>>>>test net host: 10.195.155.217
IP: 10.195.155.217
IP Address length: 4
This is IPv4 address according to byte array length !
This is IPv4 address according to instanceof !

>>>>test net host: www.hahaha.hulk.com   //地址无法解析会抛出异常
test failed: java.net.UnknownHostException: www.hahaha.hulk.com: Name or service not known

Process finished with exit code 0
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值