Java校验IP和子网掩码(并判断IP和子网掩码是否匹配)

校验IP或者网关


    private static boolean isIP(){
        String ip="192.168.10.1";
        String maskReg = "^(((1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9]).){1}((1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d).){2})((1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)/(?:[1-9]|[12][0-9]|3[012])){1}$";
        Pattern ipPattern = Pattern.compile(maskReg);
        Matcher matcher = ipPattern.matcher(ip);
        return matcher.matches();
    }

校验子网掩码

 public static boolean isMask(String mask){
        String[] split = mask.split("\\.");
        StringBuilder sb = new StringBuilder();
        for (String s : split) {
            if(s.trim().equals("")){
                return false;
            }
            int i = Integer.parseInt(s);
            //如果有数字大于255,则直接返回false
            if (i > 255) {
                return false;
            }
            String binary = Integer.toBinaryString(i);
            //如果长度小于8,则在前面补0
    

校验IP和子网掩码是否匹配 (还需要网关)

个人想法:判断是否匹配就是通过先判断掩码是否合法,再判断网关和IP是否在一个网段内(通过网关和IP分别和掩码做按位与运算,判断运算结果是否相等)

   public static boolean check() throws AddressStringException {

        String ip = "192.168.1.195";
        String mask = "255.255.255.240";
        String gateWay = "192.168.1.196";
        List<String> list=new ArrayList<>();
        list.add(ip);list.add(mask);list.add(gateWay);
        List<String> binaryList=new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
          binaryList.add(new IPAddressString(list.get(i)).toAddress().toBinaryString());
        }
          String ipBinary = binaryList.get(0);
        String maskBinary = binaryList.get(1);
        String gateWayBinary = binaryList.get(2);

        System.out.println(ipBinary);
        System.out.println(maskBinary);
        System.out.println(gateWayBinary);
				//校验子网掩码 也可以用上面的方法,主要是判断掩码二进制是不是前面全是1,后面全是0
        boolean key = true;
        int zeroIdx = -1;
        for (int i = 0; i < maskBinary.length(); i++) {
            if ('0' == maskBinary.charAt(i)){
                if (zeroIdx == -1){
                    zeroIdx = i;
       }
                if (key){
                    key = false;
                }
            }
            if ('1' == maskBinary.charAt(i) && !key){
                return false;
            }
        }

        System.out.println("zeroIdx = " + zeroIdx);
        System.out.println(32-zeroIdx);
        String zero ="0";
        String one="1";
        for(int i = 0; i <31-zeroIdx ; i++) {
            zero+="0";
            one+="1";
        }
        System.out.println(zero);
        System.out.println(ipBinary.substring(zeroIdx, 32));
         if (ipBinary.substring(zeroIdx,32).equals(zero)||ipBinary.substring(zeroIdx,32).equals(one)
        ||gateWayBinary.substring(zeroIdx,32).equals(zero)||gateWayBinary.substring(zeroIdx,32).equals(one)
        ||ipBinary.equals(gateWayBinary)){
            return false;
        }
        String ipAndMask = ipBinary.substring(0,zeroIdx) + maskBinary.substring(zeroIdx,32);
        String gatewayAndMask = gateWayBinary.substring(0,zeroIdx) + maskBinary.substring(zeroIdx,32);
        return ipAndMask.equals(gatewayAndMask);

个人拙见,如果有更好的方法请评论!

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要使用C语言对IP地址的合法性进行判断,可以使用正则表达式或者字符串处理函数进行校验。以下是一个示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> bool isValidIPAddress(const char* ipAddress) { // 分割IP地址 char* token = strtok(ipAddress, "."); int num; // 检查每个分割的部分是否是合法的数字 while (token != NULL) { num = atoi(token); if (num < 0 || num > 255) { return false; } // 检查数字前后是否有多余的0 char temp[4]; sprintf(temp, "%d", num); if (strcmp(temp, token) != 0) { return false; } token = strtok(NULL, "."); } return true; } int main() { const char* ipAddress = "192.168.0.1"; if (isValidIPAddress(ipAddress)) { printf("IP地址合法\n"); } else { printf("IP地址不合法\n"); } return 0; } ``` 要使用C语言对子网掩码进行合法性判断,可以使用与IP地址类似的方法,检查每个分割部分是否是0或者255,并且检查位数是否正确。以下是一个示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> bool isValidSubnetMask(const char* subnetMask) { // 分割子网掩码 char* token = strtok(subnetMask, "."); int num; bool allOnes = false; // 检查每个分割的部分是否是合法的数字 while (token != NULL) { num = atoi(token); if (num < 0 || num > 255) { return false; } // 检查数字前后是否有多余的0 char temp[4]; sprintf(temp, "%d", num); if (strcmp(temp, token) != 0) { return false; } // 检查位数是否正确 if (!allOnes && num != 255) { if (num == 0) { allOnes = true; } else { return false; } } else if (allOnes && num != 0) { return false; } token = strtok(NULL, "."); } return true; } int main() { const char* subnetMask = "255.255.255.0"; if (isValidSubnetMask(subnetMask)) { printf("子网掩码合法\n"); } else { printf("子网掩码不合法\n"); } return 0; } ``` 这些示例代码仅供参考,具体的判断逻辑可以根据实际需求进行调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值