package 子网划分器;

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;

public class SubnetDivider {

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

public static void main(String[] args) throws IOException {
    SubnetDivider subnetDivider = new SubnetDivider();
    subnetDivider.divideSubnet();
}

// 划分子网
public void divideSubnet() throws IOException {
    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();
    // 将输入的IP地址转化为整数数组
    int[] ipArray = parseIp(ip);
    // 根据IP地址类别,计算最大可划分子网数
    int maxSubnetNum = calculateMaxSubnetNum(ipArray[0]);
    System.out.println('本IP地址能划分划分的最大子网数为: ' + maxSubnetNum);
    // 输入要划分的子网数数:s
    System.out.println('请输入要划分的子网数:');
    System.out.print('→:');
    String subnetNumStr = br.readLine();
    subnetNum = Integer.parseInt(subnetNumStr);
    br.close();
    // 求所输入子网数对应的二进制位数:n
    subnetBits = calculateSubnetBits(subnetNum);
    System.out.println('--------------------------------------');
    // 求子网掩码
    int[] subnetMask = calculateSubnetMask(ipArray[0], subnetBits);
    System.out.println('子网掩码为:');
    printIp(subnetMask);
    // 求每网段主机数
    int hostNum = calculateHostNum(ipArray[0], subnetBits);
    System.out.println('每网段主机数:' + hostNum);
    // 输出各子网IP范围
    System.out.println('各子网IP范围如下:');
    printSubnetIps(ipArray, subnetMask, subnetBits);
    System.out.println('子网划分完毕');
}

// 将IP地址字符串转化为整数数组
private int[] parseIp(String ip) {
    int[] ipArray = new int[4];
    String[] ipStrArray = ip.split('\.');
    for (int i = 0; i < 4; i++) {
        ipArray[i] = Integer.parseInt(ipStrArray[i]);
    }
    return ipArray;
}

// 根据IP地址类别,计算最大可划分子网数
private int calculateMaxSubnetNum(int x1) {
    int maxSubnetNum = 0;
    if (x1 > 0 && x1 <= 126) { // A类
        maxSubnetNum = (int) Math.pow(2, 24 - 2) - 2;
    } else if (x1 >= 128 && x1 <= 191) { // B类
        maxSubnetNum = (int) Math.pow(2, 16 - 2) - 2;
    } else if (x1 >= 192 && x1 <= 223) { // C类
        maxSubnetNum = (int) Math.pow(2, 8 - 2) - 2;
    }
    return maxSubnetNum;
}

// 求所输入子网数对应的二进制位数:n
private int calculateSubnetBits(int subnetNum) {
    int subnetBits = 1;
    while (Math.pow(2, subnetBits) - 2 < subnetNum) {
        subnetBits++;
    }
    return subnetBits;
}

// 求子网掩码
private int[] calculateSubnetMask(int x1, int subnetBits) {
    int[] subnetMask = new int[4];
    int sum = 0;
    for (int i = 0; i < subnetBits; i++) {
        sum = sum + (int) Math.pow(2, (7 - i));
    }
    if (x1 > 0 && x1 <= 126) { // A类
        if (subnetBits <= 8) {
            subnetMask[0] = 255;
            subnetMask[1] = sum;
            subnetMask[2] = 0;
            subnetMask[3] = 0;
        } else if (subnetBits > 8 && subnetBits <= 16) {
            subnetMask[0] = 255;
            subnetMask[1] = 255;
            subnetMask[2] = sum;
            subnetMask[3] = 0;
        } else {
            subnetMask[0] = 255;
            subnetMask[1] = 255;
            subnetMask[2] = 255;
            subnetMask[3] = sum;
        }
    } else if (x1 >= 128 && x1 <= 191) { // B类
        if (subnetBits <= 8) {
            subnetMask[0] = 255;
            subnetMask[1] = 255;
            subnetMask[2] = sum;
            subnetMask[3] = 0;
        } else {
            subnetMask[0] = 255;
            subnetMask[1] = 255;
            subnetMask[2] = 255;
            subnetMask[3] = sum;
        }
    } else if (x1 >= 192 && x1 <= 223) { // C类
        subnetMask[0] = 255;
        subnetMask[1] = 255;
        subnetMask[2] = 255;
        subnetMask[3] = sum;
    }
    return subnetMask;
}

// 求每网段主机数
private int calculateHostNum(int x1, int subnetBits) {
    int hostNum = 0;
    if (x1 > 0 && x1 <= 126) { // A类
        hostNum = (int) Math.pow(2, (32 - subnetBits)) - 2;
    } else if (x1 >= 128 && x1 <= 191) { // B类
        hostNum = (int) Math.pow(2, (16 - subnetBits)) - 2;
    } else if (x1 >= 192 && x1 <= 223) { // C类
        hostNum = (int) Math.pow(2, (8 - subnetBits)) - 2;
    }
    return hostNum;
}

// 输出IP地址
private void printIp(int[] ipArray) {
    for (int i = 0; i < 4; i++) {
        System.out.print(ipArray[i] + '.');
    }
    System.out.println();
}

// 输出子网IP范围
private void printSubnetIps(int[] ipArray, int[] subnetMask, int subnetBits) {
    int[] subnetId = new int[4];
    for (int i = 0; i < 4; i++) {
        subnetId[i] = ipArray[i] & subnetMask[i];
    }
    int hostNum = calculateHostNum(ipArray[0], subnetBits);
    int[] broadcastIp = new int[4];
    for (int i = 0; i < 4; i++) {
        broadcastIp[i] = subnetId[i] | (~subnetMask[i] & 255);
    }
    for (int i = 1; i <= subnetNum; i++) {
        int[] netIp = new int[4];
        int[] startIp = new int[4];
        int[] endIp = new int[4];
        for (int j = 0; j < 4; j++) {
            netIp[j] = subnetId[j];
        }
        startIp[3] = netIp[3] + 1;
        endIp[3] = broadcastIp[3] - 2;
        System.out.print('<' + i + '> + ' ');
        printIp(startIp);
        System.out.print('------');
        printIp(endIp);
    }
}

}

Java 子网划分器:高效实现IP地址子网划分功能

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

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