2

So I'm writing a program that creates a list of all of the IP addresses from the Network address to the Broadcast address and then looping through them and checking if they are available. The code I have works perfectly fine on wired networks, but as soon as I try it on a WiFi network things get wonky. I'll receive a -1 for the prefix length and this causes my code to loop through 0.0.0.0-255.255.255.255 which is just... terrible.

The following code is how I'm getting the NetworkInterfaces. The problem could be that I'm checking the wrong interface or am not checking it correctly.

InetAddress host_address = InetAddress.getLocalHost();
NetworkInterface net_int = NetworkInterface.getByInetAddress(host_address);
int host_subnet = net_int.getInterfaceAddresses().get(0).getNetworkPrefixLength();
out.println(host_address);

From what I understand this should work, but it doesn't. I get the correct prefix on wired connections, but -1 on wireless networks. I feel like this is a rookie mistake but I'm a rookie so ¯\_(ツ)_/¯

Any help would be appreciated, thanks you.

0 Answers0