随笔 - 1,  文章 - 0,  评论 - 0,  阅读 - 112

比对计算137.0.222.0/23的网络及广播地址,并返回可用IP数

  • 对IP及掩码位进行校验
复制代码
 1 public static void ipcheck(String host, String mask) throws Exception {
 2         if (!host.matches("^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
 3                 + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\." + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
 4                 + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$")) {
 5             throw new Exception("IP地址不合规范");
 6         }
 7         if (!Pattern.compile("[0-9]*").matcher(mask).matches() || Integer.parseInt(mask)
 8                 <= 0 || Integer.parseInt(mask) > 32) {
 9             throw new Exception("子网掩码位不合规范");
10         }
11     }
复制代码
  • 对掩码位进行计算

掩码位总共32位,转换为二进制就是32个1,掩码位23就是[11111111 11111111 11111110 00000000],对不满足32位的进行补0处理

复制代码
 1 /**
 2      * 计算掩码
 3      *
 4      * @param mask
 5      * @return 二进制数组如[11111111, 11111111, 11111111, 11111111]
 6      */
 7     public static String[] maskCode(int mask) {
 8         String[] strs = new String[4];
 9         StringBuffer maskBuff = new StringBuffer();
10         int intergeBit = mask / 8;
11         int remainderBit = mask % 8;
12         for (int i = 0; i < intergeBit; i++) {
13             strs[i] = "11111111";
14         }
15         switch (intergeBit) {
16             case 0:
17                 for (int i = 0; i < remainderBit; i++) {
18                     maskBuff.append("1");
19                 }
20                 strs[0] = remainderBit==0?"00000000":String.format("%-8d", Integer.parseInt(maskBuff.toString())).replace(" ", "0");
21                 strs[1] = "00000000";
22                 strs[2] = "00000000";
23                 strs[3] = "00000000";
24                 break;
25             case 1:
26                 for (int i = 0; i < remainderBit; i++) {
27                     maskBuff.append("1");
28                 }
29                 strs[1] = remainderBit==0?"00000000":String.format("%-8d", Integer.parseInt(maskBuff.toString())).replace(" ", "0");
30                 strs[2] = "00000000";
31                 strs[3] = "00000000";
32                 break;
33             case 2:
34                 for (int i = 0; i < remainderBit; i++) {
35                     maskBuff.append("1");
36                 }
37                 strs[2] = remainderBit==0?"00000000":String.format("%-8d", Integer.parseInt(maskBuff.toString())).replace(" ", "0");
38                 strs[3] = "00000000";
39                 break;
40             case 3:
41                 for (int i = 0; i < remainderBit; i++) {
42                     maskBuff.append("1");
43                 }
44                 strs[3] = remainderBit==0?"00000000":String.format("%-8d", Integer.parseInt(maskBuff.toString())).replace(" ", "0");
45                 break;
46         }
47         return strs;
48     }
复制代码
  • 计算网络号

将IP与掩码进行与计算,得到网络号

与运算

复制代码
 1 /**
 2      * 计算网络
 3      *
 4      * @param host
 5      * @param mask
 6      * @return 10.0.0.1
 7      */
 8     public static String getNetWork(String host, String[] mask) {
 9         String[] hostArray = host.split("\\.");
10         String[] networkArray = getIp(mask).split("\\.");
11         StringBuffer netWork = new StringBuffer();
12         for (int i = 0; i < hostArray.length; i++) {
13             if (i == 0) {
14                 netWork.append(Integer.parseInt(hostArray[i]) & Integer.parseInt(networkArray[i]));
15             } else {
16                 netWork.append(".").append(Integer.parseInt(hostArray[i]) & Integer.parseInt(networkArray[i]));
17             }
18         }
19         return netWork.toString();
20     }
复制代码
  • 计算广播地址

根据网络地址与掩码地址,转换为二进制后,掩码后面几位为0就是几位主机号,将网络地址的二进制数据截取到主机位,再拼接主机位,(注意此处要将0改成1)

复制代码
/**
     * 计算广播地址
     *
     * @param host
     * @param mask
     * @return
     */
    public static String[] getRedio(String host, String mask) {
        StringBuffer networkbuff = new StringBuffer().append(getBinary(host).replaceAll("\\.", ""));
        StringBuffer maskbuff = new StringBuffer().append(getBinary(mask).replaceAll("\\.", ""));
        int hostlength = maskbuff.lastIndexOf("1");
        String[] redio = new String[4];
        StringBuffer redioBuff = new StringBuffer(networkbuff.substring(0, hostlength + 1));
        redioBuff.append(maskbuff.substring(hostlength + 1).replaceAll("0", "1"));
        redio[0] = redioBuff.substring(0, 8);
        redio[1] = redioBuff.substring(8, 16);
        redio[2] = redioBuff.substring(16, 24);
        redio[3] = redioBuff.substring(24, 32);
        return redio;
    }
复制代码
  • 可用IP数为2的(32-掩码位)次方-网络地址与广播地址

网络地址与广播地址之间为可用网络地址

 

不正之处,敬请批评指正

 

posted on   青草余生  阅读(112)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示