java - 如何从给定的 IPAddress 打印所有子网?
问题描述
这是我的输出。
run: Enter the IP address:192.32.4.1
IP in binary is 11000000001000000000010000000001
Enter the number of addresses:64
Number of bits required for address =6
The subnet mask is = 26
First address is = 192.32.4.0 Last address is = 192.32.4.63 BUILD SUCCESSFUL (total time: 14 seconds)
我想打印 和 之间的所有First address is = 192.32.4.0
子网Last address is = 192.32.4.255
。
我尝试了 for 循环,但我也没有得到。
谁能在这段代码中帮助我如何通过打印所有子网来改进这段代码?
我在打印所有子网时遇到了困难,但我很努力,但没有得到它们。
这是我的代码:
import java.util.Scanner;
public class JavaApplication6 {
public static void main(String[] args) {
// TODO code application logic here
Scanner sc = new Scanner(System.in);
System.out.print("Enter the ip address:");
String ip = sc.nextLine();
String split_ip[] = ip.split("\\."); // SPlit the string after every .
String split_bip[] = new String[4]; // split binary ip
String bip = "";
for (int i = 0; i < 4; i++) {
// “18” => 18 => 10010 => 00010010
split_bip[i] = appendZeros(Integer.toBinaryString(Integer.parseInt(split_ip[i])));
bip += split_bip[i];
}
System.out.println("IP in binary is " + bip);
System.out.print("Enter the number of addresses:");
int n = sc.nextInt();
// Calculation of mask
/*
* eg if address = 120, log 120/log 2 gives log to the base 2 => 6.9068, ceil
* gives us upper integer
*/
int bits = (int) Math.ceil(Math.log(n) / Math.log(2));
System.out.println("Number of bits required for address =" + bits);
int mask = 32 - bits;
System.out.println("The subnet mask is = " + mask);
// Calculation of first address and last address
// String fip[] = {"","","",""};
int fbip[] = new int[32];
for (int i = 0; i < 32; i++)
fbip[i] = (int) bip.charAt(i) - 48; // convert cahracter 0,1 to integer 0,1
for (int i = 31; i > 31 - bits; i--) // Get first address by ANDing last n bits with 0
fbip[i] &= 0;
String fip[] = { "", "", "", "" };
for (int i = 0; i < 32; i++)
fip[i / 8] = new String(fip[i / 8] + fbip[i]);
System.out.print("First address is = ");
for (int i = 0; i < 4; i++) {
System.out.print(Integer.parseInt(fip[i], 2));
if (i != 3)
System.out.print(".");
}
System.out.println();
int lbip[] = new int[32];
for (int i = 0; i < 32; i++)
// convert cahracter 0,1 to integer 0,1
// for(int i=31;i>31-bits;i–){ //Get last address by ORing last n bits with 1
for (int i = 31; i > 31 - bits; i--)
lbip[i] = (int) bip.charAt(i) - 48;
lbip[i] |= 1;
String lip[] = { "", "", "", "" };
for (int i = 0; i < 32; i++)
lip[i / 8] = new String(lip[i / 8] + lbip[i]);
System.out.print("Last address is = ");
for (int i = 0; i < 4; i++) {
System.out.print(Integer.parseInt(lip[i], 2));
if (i != 3)
System.out.print(".");
}
System.out.println();
}
static String appendZeros(String s) {
String temp = new String("00000000");
return temp.substring(s.length()) + s;
}
}
解决方案
推荐阅读
- neo4j - neo4j如何显示节点之间的链接特征
- javascript - PHP POST 方法没有从 ajax POST 接收数据
- android - Android - 从定期重复的待处理通知中获取剩余时间
- webstorm - 让 WebStorm 停止在失去焦点时剥离尾随空白
- function - kotlin 中未指定的参数和返回类型
- datastax-enterprise - 如何在同一台服务器上启用图形 + 搜索 + 分析
- ios - Objective C中的多维UILabel数组
- javascript - 带有文本标题的 Javascript 随机链接
- php - 在codeigniter中将数据插入不同的表
- symfony - 缓存驱动配置