/** * Get broadcast address * */public static String getbroadcastaddress (string subnet, string IP) {string[] ips = ip.split ("\ \."); string[] subnets = subnet.split ("\ \"); StringBuffer sb = new StringBuffer (), for (int i = 0; i < ips.length; i++) {Ips[i] = string.valueof ((~integer.parseint (s Ubnets[i]) | (Integer.parseint (ips[i])); Sb.append (Turntostr (Integer.parseint (ips[i))); if (i! = (ips.length-1)) Sb.append (".");} Return Turntoip (Sb.tostring ());} private static String turntostr (int num) {string str = ""; str = integer.tobinarystring (num); int len = 8-str.length (); for (int i = 0; i < len; i++) {str = "0" + str;} if (Len < 0) str = str.substring (+); return str;} /** * Convert to STR * */private static String Turntoip (String str) {string[] ips = str.split ("\ \."); StringBuffer sb = new StringBuffer (), for (int i = 0; i < ips.length; i++) {Sb.append (Turntoint (ips[i])); Sb.append (".") ;} Sb.deletecharat (Sb.length ()-1); return sb.tostring ();} /** * Convert int * */private static int turntoint (String str) {int toTal = 0;int top = str.length (); for (int i = 0; i < str.length (); i++) {String h = string.valueof (Str.charat (i)); top--;t Otal + = ((int) Math.pow (2, top)) * (Integer.parseint (h));} return total;}
Get the Java implementation of the broadcast address based on the IP address and subnet mask