For my project I wanted to get a list of all available broadcast addresses so I could broadcast a request and my other application located on other computer in the unspecified network would respond and to get the list I (now using little modified version with contribution of Mike) came up with this:
private ArrayList<InetAddress> getBroadcastAddresses() {
ArrayList<InetAddress> listOfBroadcasts = new ArrayList();
Enumeration list;
try {
list = NetworkInterface.getNetworkInterfaces();
while(list.hasMoreElements()) {
NetworkInterface iface = (NetworkInterface) list.nextElement();
if(iface == null) continue;
if(!iface.isLoopback() && iface.isUp()) {
System.out.println("Found non-loopback, up interface:" + iface);
Iterator it = iface.getInterfaceAddresses().iterator();
while (it.hasNext()) {
InterfaceAddress address = (InterfaceAddress) it.next();
System.out.println("Found address: " + address);
if(address == null) continue;
InetAddress broadcast = address.getBroadcast();
if(broadcast != null) listOfBroadcasts.add(broadcast);
}
}
}
} catch (SocketException ex) {
return new ArrayList<InetAddress>();
}
return site;
}
It works quite well for reqular LAN however when it comes to the WiFi LAN it just skips the second while loop after one step because of having address
equals null even though when I used System.out.println(interfaceItem)
just to view what interfaces are being gone through it wrote wireless LAN's name and my IP corresponding to the network.
EDIT 1: This is the output where 172.16.1.104 is my IP in the wireless network. The problem appears ONLY on my notebook with Wifi. The output is from my notebook where I mostly use wireless and sometimes I use UTP to connect with my friend. There is also one network interface of VirtualBox on my notebook.
Could you tell me what's wrong with it? Thank you!
Note: So it turns out that this might be problem for my notebook in particular and the code works for everybody else in general, I love this kind of problem :-) Seems like a dead end to me but thank for help anyway :-)
Still love you! ;-)