Java 子网划分器:高效计算子网掩码、IP 范围,支持超网功能
package 子网划分器;
import java.io.*;
public class SubnetCalculator {
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);
//截取x2
int index2 = ip.indexOf('.', index1 + 1);
String st2 = ip.substring(index1 + 1, index2);
int x2 = Integer.parseInt(st2);
//截取x3
int index3 = ip.indexOf('.', index2 + 1);
String st3 = ip.substring(index2 + 1, index3);
int x3 = Integer.parseInt(st3);
//截取x4
int x4 = Integer.parseInt(ip.substring(index3 + 1));
//判断IP地址类型并输出可划分的最大子网数
int maxSubnets = 0;
if (x1 >= 1 && x1 <= 126) { //A类
maxSubnets = (int) Math.pow(2, 24 - 2) - 2;
} else if (x1 >= 128 && x1 <= 191) { //B类
maxSubnets = (int) Math.pow(2, 16 - 2) - 2;
} else if (x1 >= 192 && x1 <= 223) { //C类
maxSubnets = (int) Math.pow(2, 8 - 2) - 2;
}
System.out.println("本IP地址能划分的最大子网数为: " + maxSubnets);
//输入要划分的子网数:s
System.out.println("请输入要划分的子网数:");
System.out.print("→:");
String subnet_num = br.readLine();
s = Integer.parseInt(subnet_num);
br.close();
//求所输入子网数对应的二进制位数:n
for (n = 1; (Math.pow(2, n) - 2) < s; n++) ;
System.out.println("--------------------------------------");
//求子网掩码
int sum = 0;
for (int i = 0; i < n; i++) {
sum = sum + (int) Math.pow(2, (7 - i));
}
System.out.println("子网掩码为:" + maskToString(sum));
//求每个子网段的主机数
int maxHosts = 0;
if (x1 >= 1 && x1 <= 126) { //A类
maxHosts = (int) Math.pow(2, (32 - n)) - 2;
} else if (x1 >= 128 && x1 <= 191) { //B类
maxHosts = (int) Math.pow(2, (16 - n)) - 2;
} else if (x1 >= 192 && x1 <= 223) { //C类
maxHosts = (int) Math.pow(2, (8 - n)) - 2;
}
System.out.println("每个子网段的主机数:" + maxHosts);
//输出各子网IP范围
System.out.println("各子网IP范围如下:");
int subnet_id = 0;
int[] subnetAddress = {x1, x2, x3, 0}; //子网地址,最后一个元素先占位,后面再根据需要填充
int[] broadcastAddress = {x1, x2, x3, 0}; //广播地址,最后一个元素先占位,后面再根据需要填充
for (int i = 1; i <= s; i++) {
subnet_id++;
int sub_id = (int) Math.pow(2, (8 - n)) * (i - 1);
subnetAddress[3] = sub_id;
broadcastAddress[3] = sub_id + (int) Math.pow(2, (8 - n)) - 1;
System.out.print("<" + subnet_id + ">" + " ");
System.out.println(addressToString(subnetAddress) + " --- " + addressToString(broadcastAddress));
}
System.out.println("子网划分完毕");
}
//将32位子网掩码转换为点分十进制字符串
private static String maskToString(int mask) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
int num = 0;
for (int j = 0; j < 8; j++) {
num += ((mask & 1) << (7 - j));
mask >>= 1;
}
sb.append(num);
if (i < 3) {
sb.append(".");
}
}
return sb.toString();
}
//将IP地址数组转换为点分十进制字符串
private static String addressToString(int[] address) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 4; i++) {
sb.append(address[i]);
if (i < 3) {
sb.append(".");
}
}
return sb.toString();
}
}
原文地址: https://www.cveoy.top/t/topic/ocIc 著作权归作者所有。请勿转载和采集!