您好,登錄后才能下訂單哦!
使用java怎么根據IP地址計算網段?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
根據IP地址與字段掩碼計算網段最大最小IP
package c04; import java.net.UnknownHostException; public class IPNetworkSegmentCalculation { public static void main(String[] args) throws UnknownHostException { String ip = "192.168.126.2"; String mask = "255.255.255.0"; findMask(); } public static void findMask(){ String mask = "255.255.255.0"; String ip = "192.168.126.2"; String[] ipArray = ip.split("[.]"); String[] maskArray = mask.split("[.]"); String minIp = ""; String maxIp = ""; int subMask = 0; for(int i=0 ;i < maskArray.length ; i++){ int maskTmp = Integer.valueOf(maskArray[i]); int ipTmp = Integer.valueOf(ipArray[i]); int mi = maskTmp&ipTmp; //System.out.println("==============="+maskTmp+" "+ipTmp); if(ipTmp == mi && maskTmp==255){ minIp = minIp + ipTmp+"." ; maxIp = maxIp + ipTmp+"." ; subMask = subMask + 8; }else{ minIp = minIp + mi+"." ; maxIp = maxIp + maxNetSeg(Integer.toBinaryString(maskTmp),mi)+"."; subMask = subMask + subMaskNum(Integer.toBinaryString(maskTmp)); } } minIp = minIp.substring(0,minIp.length()-1); maxIp = maxIp.substring(0,maxIp.length()-1); System.out.println("minip "+minIp); System.out.println("maxip "+maxIp); System.out.println("mask "+subMask); } private static int calculate(int n) { if (n == 0) return 1; return 2 * calculate(n - 1); } private static Integer maxNetSeg(String s1,int mi){ s1 = new StringBuffer(s1).reverse().toString(); if(s1.indexOf("1")!=-1){ int i = s1.indexOf("1"); return mi+calculate(i); }else { return 255; } } private static Integer subMaskNum(String s1){ if(s1.lastIndexOf("1")!=-1){ int i = s1.lastIndexOf("1") + 1; return i; } return 0; } }
Java根據網段計算子網掩碼,起始IP,結束IP
/** * IP的相關計算 * @author zengchaowang * @version 1.0 * */ public class IpCaculate { /** * 根據掩碼位數計算掩碼 * @param maskIndex 掩碼位 * @return 子網掩碼 */ public static String getNetMask(String maskIndex) { StringBuilder mask = new StringBuilder(); Integer inetMask = 0; try { inetMask = Integer.parseInt(maskIndex); } catch (NumberFormatException e) { System.out.println(e.getMessage()); return null; } 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] += 1 << 8 - num2; } for (int i = 0; i < 4; i++) { if (i == 3) { mask.append(array[i]); } else { mask.append(array[i] + "."); } } return mask.toString(); } /** * 根據網段計算起始IP 網段格式:x.x.x.x/x * 一個網段0一般為網絡地址,255一般為廣播地址. * 起始IP計算:網段與掩碼相與之后加一的IP地址 * @param segment 網段 * @return 起始IP */ public static String getStartIp(String segment) { StringBuffer startIp = new StringBuffer(); if (segment == null) { return null; } String arr[] = segment.split("/"); String ip = arr[0]; String maskIndex = arr[1]; String mask = IpCaculate.getNetMask(maskIndex); if (4 != ip.split("\\.").length || mask == null) { return null; } int ipArray[] = new int[4]; int netMaskArray[] = new int[4]; for (int i = 0; i < 4; i++) { try { ipArray[i] = Integer.parseInt(ip.split("\\.")[i]); netMaskArray[i] = Integer.parseInt(mask.split("\\.")[i]); if (ipArray[i] > 255 || ipArray[i] < 0 || netMaskArray[i] > 255 || netMaskArray[i] < 0) { return null; } ipArray[i] = ipArray[i] & netMaskArray[i]; if(i==3){ startIp.append(ipArray[i]+1); }else{ startIp.append(ipArray[i]+"."); } } catch (NumberFormatException e) { System.out.println(e.getMessage()); } } return startIp.toString(); } /** * 根據網段計算結束IP * @param segment * @return 結束IP */ public static String getEndIp(String segment) { StringBuffer endIp=new StringBuffer(); String startIp = getStartIp(segment); if (segment == null) { return null; } String arr[] = segment.split("/"); String maskIndex = arr[1]; //實際需要的IP個數 int hostNumber = 0; int startIpArray[] = new int[4]; try { hostNumber=1<<32-(Integer.parseInt(maskIndex)); for (int i = 0; i <4; i++) { startIpArray[i] = Integer.parseInt(startIp.split("\\.")[i]); if(i == 3){ startIpArray[i] = startIpArray[i] - 1; break; } } startIpArray[3] = startIpArray[3] + (hostNumber - 1); } catch (NumberFormatException e) { System.out.println(e.getMessage()); } if(startIpArray[3] >255){ int k = startIpArray[3] / 256; startIpArray[3] = startIpArray[3] % 256; startIpArray[2] = startIpArray[2] + k; } if(startIpArray[2] > 255){ int j = startIpArray[2] / 256; startIpArray[2] = startIpArray[2] % 256; startIpArray[1] = startIpArray[1] + j; if(startIpArray[1] > 255){ int k = startIpArray[1] / 256; startIpArray[1] = startIpArray[1] % 256; startIpArray[0] = startIpArray[0] + k; } } for(int i = 0; i < 4; i++){ if(i == 3){ startIpArray[i] = startIpArray[i] - 1; } if("" == endIp.toString()||endIp.length()==0){ endIp.append(startIpArray[i]); }else{ endIp.append("." + startIpArray[i]); } } return endIp.toString(); } }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。