In my program I’m sending out UDP broadcasts and reacting to them. I need a way to ignore the UDP broadcasts that I send out, but react to ones that aren’t from my machine.
I did try using:
if (NetworkInterface.getByInetAddress(packet.getAddress()) != null)
but this generated IOExceptions in some cases (java.net.SocketException: no network interface is bound to such an IP address)
Anyone any ideas?
Also:
getInetAddress() on my socket throws a NullPointerException
I think there’s a little discrepancy between the javadoc and the actual implementation of
NetworkInterface getByInetAddress(). The javadoc seems to suggest that getByInetAddress would return null if no match was found, yet the implementation either returns a match, either throws a SocketException.JavaDoc
Returns: A NetworkInterface or null if there is no network interface with the specified IP address.
Implementation
I suggest to either catch the exception and treat it as an answer from a 3rd party, either re-implement it using the
getNetworkInterfaces()method.