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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Cug_wangww/article/details/78772167

网上根据IP/掩码位数获取起始地址的方法大多是JS代码实现.项目需要在后台进行处理.提供一份根据IP/掩码获取起始地址通过java代码实现

//IP  
String ip = "192.168.3.0";  
//掩码位数  
String mask = "24";

1.根据掩码位数获取掩码

/** 
 * 根据掩码位数获取掩码 
 */  
private static String getNetMask(String mask) {  
    int inetMask =Integer.parseInt(mask);  
    if(inetMask > 32){  
        return null;  
    }  
    //子网掩码为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];  
    return netMask;  
}  

2.根据ip地址和掩码获取起始IP

private String getLowAddr(String ipinfo, String netMask) {  
    String lowAddr = "";  
    int ipArray[] = new int[4];  
    int netMaskArray[] = new int[4];  
    if(4 != ipinfo.split("\\.").length || "" == netMask){  
        return null;  
    }  
    for (int i = 0; i < 4; i++) {  
        try{  
            ipArray[i] = Integer.parseInt(ipinfo.split("\\.")[i]);  
        }catch(NumberFormatException e){  
            String ip = ipinfo.replaceAll("\n", "");  
            ipArray[i] = Integer.parseInt(ip.split("\\.")[i]);  
        }  
        netMaskArray[i] = Integer.parseInt(netMask.split("\\.")[i]);  
        if(ipArray[i] > 255 || ipArray[i] < 0 || netMaskArray[i] > 255 || netMaskArray[i] < 0){  
                    return null;  
        }  
        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];  
        }  
    }  
    return lowAddr;  
}  

3.根据ip地址和掩码获取终止IP

private String getHighAddr(String ipinfo, String netMask) {  
    String lowAddr = getLowAddr(ipinfo, netMask);  
    int hostNumber = getHostNumber(netMask);  
    if("" == lowAddr || hostNumber == 0){  
        return 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];  
        }  
    }  
    return highAddr;  
}  

4.ip转换Long

private long ip2long(String ip) {  
    String[] ips = ip.split("[.]");  
    long num = 16777216L * Long.parseLong(ips[0]) + 65536L  
            * Long.parseLong(ips[1]) + 256 * Long.parseLong(ips[2])  
            + Long.parseLong(ips[3]);  
    return num;  
}  

5.实际可用ip数量

private int getHostNumber(String netMask) {  
    int hostNumber = 0;  
    int netMaskArray[] = new int[4];  
    for (int i = 0; i < 4 ; i++) {  
        netMaskArray[i] = Integer.parseInt(netMask.split("\\.")[i]);  
        if(netMaskArray[i] < 255){  
            hostNumber =  (int) (Math.pow(256,3-i) * (256 - netMaskArray[i]));  
            break;  
        }  
    }  
    return hostNumber;  
}  

注意:通过上面获取到的开始和结束IP是第一个可用最后一个可用IP,以掩码位数为24位的为例.一个网段0一般为网络地址,255一般为广播地址.



另外:获取的可用IP数量为256是包含网络地址和广播地址的.这一点一定要清楚.





展开阅读全文

没有更多推荐了,返回首页