Well, if you have the broadcasting worked out and only need to change the last byte of the address to 255, you can do the following:
String getBroadcastAddress( final String address ) throws UnknownHostException { InetAddress addr = InetAddress.getByName( address ); if( InetAddress instanceof Inet4Address ) { byte[] bytes = addr.getAddress(); bytes[3] = 255; return InetAddress.getByAddress( bytes ).getHostAddress(); } else { ... deal with ipv6 } }
Pulling the broadcast address directly from the NetworkInterface, results in this code:
public static String getBroadcast() throws SocketException { System.setProperty("java.net.preferIPv4Stack", "true"); for( Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements(); ) { NetworkInterface nif = interfaces.nextElement(); if( !nif.isLoopback() ) { for( InterfaceAddress addr : nif.getInterfaceAddresses() ) { return addr.getBroadcast().toString().substring(1); } } } return null; }
You may have to sift through the NetworkInterface to find the one corresponding to the internal ip.