zoukankan      html  css  js  c++  java
  • JAVA和C#检测IP地址段是否交叉和获取地址段IP列表的方法

    一、说明

      我们经常编程时,需要对一个DIDR地段计算其可用IP地址,或者验证某个IP是否被包含在一个地址段中。

    二、工具

      1、Java 可以使用 cidr-ip-trie库解决。

        https://github.com/veqryn/cidr-ip-trie

    package com.test.utils;
    
    import com.github.veqryn.net.Cidr4;
    import org.junit.Test;
    
    public class CIDRTest {
        @Test
        public void verifyCidr() {
            com.github.veqryn.net.Cidr4 cidr1 = new Cidr4("192.168.1.0/28");
            com.github.veqryn.net.Cidr4 cidr2 = new Cidr4("192.168.0.0/30");
            boolean isInRange = cidr1.isInRange(cidr2, true);
            System.out.println(isInRange);
            System.out.println("---------------");
    
            System.out.println(cidr1.getLowBinaryInteger(true));
            System.out.println(cidr1.getHighBinaryInteger(true));
            System.out.println("---------------");
            System.out.println(cidr2.getLowBinaryInteger(true));
            System.out.println(cidr2.getHighBinaryInteger(true));
            System.out.println("---------------");
    
    
            System.out.println(cidr1.getAddressRange());
            System.out.println(cidr2.getAddressRange());
    
            String[] addresses1 = cidr1.getAllAddresses(true);
            for (String s : addresses1) {
                System.out.println(s);
            }
            System.out.println("---------------");
            String[] addresses2 = cidr2.getAllAddresses(true);
            for (String s : addresses2) {
                System.out.println(s);
            }
        }
    }

      2、C# 可以使用IPAddressRange库解决。

        https://www.nuget.org/packages/IPAddressRange/

     
  • 相关阅读:
    使用JDK创建webService
    eclipse换工作空间要做的事情
    JAVA输出表格(适配中英文)
    linux下如何用GDB调试c++程序
    C++编译的四个步骤
    linux下如何设置root密码(第一次)
    chp01、Dreamweaver介绍
    服务器端程序
    1_计算机网络概述
    Oracle Java JDBC: Get Primary Key Of Inserted Record
  • 原文地址:https://www.cnblogs.com/songxingzhu/p/9811291.html
Copyright © 2011-2022 走看看