简体   繁体   English

检查列表的元素是否在 java 中的两个 ip 地址之间

[英]chek if the elements of the list is between two ip addresses in java

I want to write a function that receives two variables of type String representing two IP addresses and a third variable representing a group of IP addresses.我想写一个 function 接收两个字符串类型的变量,代表两个 IP 地址和第三个变量代表一组 IP 地址。 The function returns all addresses between IP1 and IP2 from the given set of addresses, taking into account the order. function 从给定的地址集中返回 IP1 和 IP2 之间的所有地址,并考虑顺序。

this is the function signature:这是 function 签名:

public static String[] get_available_IPs(String ip1, String ip2, String[] addresses)

I tried to split each ip to compare them in the end but I didn't get any result我试图拆分每个 ip 以最后比较它们,但我没有得到任何结果

This is the code that I wrote while i'm trying to solve this problem.这是我在尝试解决此问题时编写的代码。 I don't think that it will help you to solve the problem but I just want you to take a look of what I did.我不认为它会帮助你解决问题,但我只是想让你看看我做了什么。

String[] ip_numbers1 = ip1.split("\\.");
    String[] ip_numbers2 = ip2.split("\\.");
    String[][] ip_numbers3 = new String[4][addresses.length];
    
    boolean[] flags = new boolean[4];
    
    for (int i = 0; i < addresses.length; i++) {
        for (int j = 0; j < 4; j++) {
            String[] new1 = addresses[j].split("\\.");
            for (int k = 0; k < 4; k++) {
                ip_numbers3[j][k] = new1[k];
            }
        }
    }
    
    for (int i = 0; i < ip_numbers3.length; i++) {
        if (Integer.parseInt(ip_numbers1[0]) <= Integer.parseInt(ip_numbers3[i][0])
                && Integer.parseInt(ip_numbers2[0]) >= Integer.parseInt(ip_numbers3[i][0])) {
            flags[i] = true;
        }
    }

sample inputs and outputs:样本输入和输出:

input:
ip1 = '192.168.1.1'
ip2 = '220.150.1.0'
addresses = ['193.168.10.20','221.155.1.5','194.200.1.5','192.168.1.2']

output:
['194.200.1.5','193.168.10.20','192.168.1.2']

input:
ip1 = '191.168.1.1'
ip2 = '222.155.1.5'
addresses = ['193.168.10.20','221.155.1.5','194.200.1.5','192.168.1.1']

output:
['221.155.1.5','194.200.1.5','193.168.10.20','192.168.1.1']

Strings in java are Comparable . java 中的字符串是Comparable This means you can order them with ip1.compareTo(ip) .这意味着您可以使用ip1.compareTo(ip)订购它们。 The best part is that it works even with the .最好的部分是它甚至可以与. in the ips.在ips中。

Here is a code sample that tells if an ip is in range of two others (not inclusive).这是一个代码示例,它告诉 ip 是否在另外两个(不包括)范围内。

public static Boolean check(String ip1, String ip2, String test){
    return ip1.compareTo(test) < 0 && ip2.compareTo(test) > 0;
}

You can test it here: https://www.jdoodle.com/iembed/v0/vRZ你可以在这里测试它: https://www.jdoodle.com/iembed/v0/vRZ

Since IP address are made of 4 "bytes" (numbers between 0 and 255), an IP can be encoded into an unsigned integer, and integer are definitely easier to use for operation involving comparison. Since IP address are made of 4 "bytes" (numbers between 0 and 255), an IP can be encoded into an unsigned integer, and integer are definitely easier to use for operation involving comparison.

There is no true unsigned integer in Java, but you can use a long. Java 中没有真正的无符号 integer,但可以使用 long。 Below are examples of methods to transform an IP to its long representation (and the inverse operation):以下是将 IP 转换为其长表示(和逆运算)的方法示例:

private static long ipToLong(String ipAsString) {
    return Arrays.stream(ipAsString.split("\\."))
                 .map(Long::parseLong)
                 .reduce(0L, (built, b) -> (built << 8) | b);
}

private static String longToIp(long ipAsLong) {
    return IntStream.of(24,16,8,0)
            .mapToLong(n -> ((ipAsLong)>>n)&0xff)
            .mapToObj(String::valueOf)
            .collect(Collectors.joining("."));
}

With this, IP1 will be before IP2 if the long representation of IP1 is lower than the one of IP2.这样,如果 IP1 的长表示低于 IP2 的长表示,则 IP1 将在 IP2 之前。 For instance to filter IP address:例如过滤 IP 地址:

public static String[] select(String ipStart, String ipEnd, String[] addresses) {
    final var start = ipToLong(ipStart);
    final var end = ipToLong(ipEnd);

    final Predicate<String> isInRange = ip -> {final var value = ipToLong(ip);return value>=start && value<=end;};

    return Arrays.stream(addresses).filter(isInRange).toArray(String[]::new);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM