JAVA代码根据IP/掩码位数格式地址段得到起始地址和结束地址,可用IP数量,掩码

JAVA代码根据IP/掩码位数格式地址段得到起始地址和结束地址,可用IP数量,掩码

  • IP地址:
    • 将IP和掩码位数拆分
    • 计算子网掩码
    • 根据子网掩码获取开始IP
    • IP可用数量(包含广播地址)
    • 根据ip地址和掩码获取结束IP

IP地址:

String network ="10.102.90.128/26";

将IP和掩码位数拆分

 String arr[] = network.split("/");
 String ip = arr[0];
 String mask = arr[1];
 int inetMask = Integer.parseInt(mask);
 System.out.println("掩码位数:"+inetMask);

计算子网掩码

  //子网掩码为1占了几个字节
	 int num1 = inetMask / 8;
	 //子网掩码的补位位数
	 int num2 = inetMask%8;
	 int array[] = new int[4];
	 for (int i = 0; i < num1; i++) {
	     array[i] = 255;
	 }
	 for (int i = num1; i < 4; i++) {
	     array[i] = 0;
	 }
	 for (int i = 0; i < num2; num2--) {
	     array[num1] += Math.pow(2, 8-num2);
	 }
	 String netMask =  array[0] + "." + array[1] + "." + array[2] + "." + array[3];
	 System.out.println("子网掩码:"+netMask);

根据子网掩码获取开始IP

 String lowAddr = "";
 int ipArray[] = new int[4];
 int netMaskArray[] = new int[4];
 if(4 != ip.split("\\.").length || "" == netMask){
     System.out.println(ip.split("\\.").length);
 }
 String ipinfo = ip;
 for (int i = 0; i <4; i++) {
      ipArray[i] = Integer.parseInt(ipinfo.split("\\.")[i]);
      String ipAddr= ipinfo.replaceAll("\n", "");
      ipArray[i] = Integer.parseInt(ipAddr.split("\\.")[i]);
      netMaskArray[i] = Integer.parseInt(netMask.split("\\.")[i]);
      if(ipArray[i] > 255 || ipArray[i] < 0 || netMaskArray[i] > 255 || netMaskArray[i] < 0){
          System.out.println("------");
 	   }
 ipArray[i] = ipArray[i]&netMaskArray[i];
 }
  //构造最小地址
  for (int i = 0; i < 4; i++){
      if(i == 3){
          ipArray[i] = ipArray[i] + 1;
      }
      if ("" == lowAddr){
          lowAddr +=ipArray[i];
      } else{
          lowAddr += "." + ipArray[i];
      }
  }
  System.out.println("开始IP(getWay):"+lowAddr);

IP可用数量(包含广播地址)

		int hostNumber = 0;
        int netMaskArray1[] = new int[4];
        for (int i = 0; i < 4 ; i++) {
            netMaskArray1[i] = Integer.parseInt(netMask.split("\\.")[i]);
            if(netMaskArray1[i] < 255){
                hostNumber =  (int) (Math.pow(256,3-i) * (256 - netMaskArray1[i]));
            }
        }
        System.out.println("IP可用数量(包含广播):"+hostNumber);

根据ip地址和掩码获取结束IP

		if (""==lowAddr||hostNumber==0){
            System.out.println("null");
        }
        int lowAddrArray[] = new int[4];
        for (int i = 0; i < 4; i++) {
            lowAddrArray[i] = Integer.parseInt(lowAddr.split("\\.")[i]);
            if(i == 3){
                lowAddrArray[i] = lowAddrArray[i] - 1;
            }
        }
        lowAddrArray[3] = lowAddrArray[3] + (hostNumber - 1);
        if(lowAddrArray[3] >255){
            int k = lowAddrArray[3] / 256;
            lowAddrArray[3] = lowAddrArray[3] % 256;
            lowAddrArray[2] = lowAddrArray[2] + k;
        }
        if(lowAddrArray[2] > 255){
            int  j = lowAddrArray[2] / 256;
            lowAddrArray[2] = lowAddrArray[2] % 256;
            lowAddrArray[1] = lowAddrArray[1] + j;
            if(lowAddrArray[1] > 255){
                int  k = lowAddrArray[1] / 256;
                lowAddrArray[1] = lowAddrArray[1] % 256;
                lowAddrArray[0] = lowAddrArray[0] + k;
            }
        }
        String highAddr = "";
        for(int i = 0; i < 4; i++){
            if(i == 3){
                lowAddrArray[i] = lowAddrArray[i] - 1;
            }
            if("" == highAddr){
                highAddr = lowAddrArray[i]+"";
            }else{
                highAddr += "." + lowAddrArray[i];
            }
        }
        System.out.println("结束IP:"+highAddr);

运行结果:
JAVA代码根据IP/掩码位数格式地址段得到起始地址和结束地址,可用IP数量,掩码_第1张图片

你可能感兴趣的:(Java,Java)