package 子网划分器;

import java.io.*;

public class SubnetDivider {

public static void main(String[] args) throws IOException {

    String ip; //用于接收要划分的IP
    int s; //用于接收以要划分的子网数
    int n; //输入子网数对应的二进制位数

    System.out.println("---------------划分子网---------------");

    //输入要划分的IP地址:ip
    System.out.println("请输入要划分的IP地址(格式为:x1.x2.x3.x4):");
    System.out.print("→:");
    InputStreamReader isr = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(isr);
    ip = br.readLine();

    //截取x1
    int index1 = ip.indexOf('.');
    String st1 = ip.substring(0, index1);
    int x1 = Integer.parseInt(st1); //把String类型的st转化成int型

    //截取x2
    int index2 = ip.indexOf('.', index1 + 1);
    String st2 = ip.substring(index1 + 1, index2);
    int x2 = Integer.parseInt(st2); //把String类型的st转化成int型

    //截取x3
    int index3 = ip.indexOf('.', index2 + 1);
    String st3 = ip.substring(index2 + 1, index3);
    int x3 = Integer.parseInt(st3); //把String类型的st转化成int型

    //截取x4
    int x4 = Integer.parseInt(ip.substring(index3 + 1)); //把String类型的st转化成int型

    //判断IP地址类型
    int hostBits = 0; //主机位数
    int subnetBits = 0; //子网位数

    if (x1 >= 1 && x1 <= 126) { //A类
        System.out.println("IP地址类型为:A类");
        hostBits = 24;
        subnetBits = 8;
        int maxSubnets = (int) Math.pow(2, subnetBits) - 2;
        System.out.println("本IP地址能划分的最大子网数为:" + maxSubnets);
    } else if (x1 >= 128 && x1 <= 191) { //B类
        System.out.println("IP地址类型为:B类");
        hostBits = 16;
        subnetBits = 16;
        int maxSubnets = (int) Math.pow(2, subnetBits) - 2;
        System.out.println("本IP地址能划分的最大子网数为:" + maxSubnets);
    } else if (x1 >= 192 && x1 <= 223) { //C类
        System.out.println("IP地址类型为:C类");
        hostBits = 8;
        subnetBits = 24;
        int maxSubnets = (int) Math.pow(2, subnetBits) - 2;
        System.out.println("本IP地址能划分的最大子网数为:" + maxSubnets);
    } else {
        System.out.println("IP地址不合法");
        return;
    }

    //输入要划分的子网数:s
    System.out.println("请输入要划分的子网数:");
    System.out.print("→:");
    String subnetNum = br.readLine();
    s = Integer.parseInt(subnetNum);

    //求所输入子网数对应的二进制位数:n
    for (n = 1; (Math.pow(2, n) - 2) < s; n++)
        ;

    System.out.println("--------------------------------------");

    //求子网掩码
    String subnetMask = getSubnetMask(subnetBits, n);
    System.out.println("子网掩码为:" + subnetMask);

    //求每网段主机数
    int hostNum = (int) Math.pow(2, hostBits - subnetBits) - 2;
    System.out.println("每网段主机数:" + hostNum);

    //输出各子网IP范围
    System.out.println("各子网IP范围如下:");
    for (int i = 1; i <= s; i++) {
        String subnetIP = getSubnetIP(subnetMask, i);
        String startIP = getStartIP(subnetIP);
        String endIP = getEndIP(subnetIP, hostNum);
        System.out.println("<" + i + "> " + startIP + " - " + endIP);
    }

    System.out.println("子网划分完毕");
}

/**
 * 获取子网掩码
 *
 * @param subnetBits 子网位数
 * @param n          输入的子网数对应的二进制位数
 * @return 子网掩码
 */
private static String getSubnetMask(int subnetBits, int n) {
    int mask = (int) (Math.pow(2, subnetBits) - 1) << (32 - subnetBits);
    mask = mask >> (n - subnetBits);
    String subnetMask = (mask >>> 24) + "." + ((mask & 0x00FFFFFF) >>> 16) + "." + ((mask & 0x0000FFFF) >>> 8) + "." + (mask & 0x000000FF);
    return subnetMask;
}

/**
 * 获取子网IP
 *
 * @param subnetMask 子网掩码
 * @param i          子网号
 * @return 子网IP
 */
private static String getSubnetIP(String subnetMask, int i) {
    String[] subnetMaskArray = subnetMask.split("\\.");
    int subnetIP = (Integer.parseInt(subnetMaskArray[0]) << 24) |
            (Integer.parseInt(subnetMaskArray[1]) << 16) |
            (Integer.parseInt(subnetMaskArray[2]) << 8) |
            Integer.parseInt(subnetMaskArray[3]);
    subnetIP = subnetIP + i * (int) Math.pow(2, 32 - subnetMaskArray.length);
    String subnetIPString = (subnetIP >>> 24) + "." + ((subnetIP & 0x00FFFFFF) >>> 16) + "." + ((subnetIP & 0x0000FFFF) >>> 8) + "." + (subnetIP & 0x000000FF);
    return subnetIPString;
}

/**
 * 获取子网起始IP
 *
 * @param subnetIP 子网IP
 * @return 子网起始IP
 */
private static String getStartIP(String subnetIP) {
    String[] subnetIPArray = subnetIP.split("\\.");
    subnetIPArray[3] = String.valueOf(Integer.parseInt(subnetIPArray[3]) + 1);
    String startIP = subnetIPArray[0] + "." + subnetIPArray[1] + "." + subnetIPArray[2] + "." + subnetIPArray[3];
    return startIP;
}

/**
 * 获取子网结束IP
 *
 * @param subnetIP 子网IP
 * @param hostNum  每网段主机数
 * @return 子网结束IP
 */
private static String getEndIP(String subnetIP, int hostNum) {
    String[] subnetIPArray = subnetIP.split("\\.");
    int endIP = (Integer.parseInt(subnetIPArray[0]) << 24) |
            (Integer.parseInt(subnetIPArray[1]) << 16) |
            (Integer.parseInt(subnetIPArray[2]) << 8) |
            Integer.parseInt(subnetIPArray[3]);
    endIP = endIP + hostNum;
    subnetIPArray[0] = String.valueOf((endIP >>> 24) & 0xFF);
    subnetIPArray[1] = String.valueOf((endIP >>> 16) & 0xFF);
    subnetIPArray[2] = String.valueOf((endIP >>>

原文地址: http://www.cveoy.top/t/topic/fmko 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录