Ignoring native UDP translations in Java

In my program, I send UDP broadcasts and respond to them. I need a way to ignore the UDP broadcasts that I send, but to respond to those that are not from my machine.

I tried using: if (NetworkInterface.getByInetAddress(packet.getAddress()) != null) but in some cases it throws IOExceptions (java.net.SocketException: no network interface is bound to that IP address)

Any ideas?

also: getInetAddress () on my socket throws a NullPointerException

+4
source share
4 answers

I think there is a slight discrepancy between javadoc and the actual implementation of NetworkInterface getByInetAddress() . It seems that javadoc suggests that getByInetAddress returns null if no match was found, but the implementation either returns a match or throws a SocketException.

Javoc

 public static NetworkInterface getByInetAddress(InetAddress addr) throws SocketException 

Returns: NetworkInterface or null if there is no network interface with the specified IP address .

Implementation

  public static NetworkInterface getByInetAddress (InetAddress addr) throws SocketException { if (networkInterfaces == null) networkInterfaces = getRealNetworkInterfaces (); for (Enumeration interfaces = networkInterfaces.elements (); interfaces.hasMoreElements (); ) { NetworkInterface tmp = (NetworkInterface) interfaces.nextElement (); for (Enumeration addresses = tmp.inetAddresses.elements (); addresses.hasMoreElements (); ) { if (addr.equals ((InetAddress) addresses.nextElement ())) return tmp; } } throw new SocketException ( "no network interface is bound to such an IP address"); } 

I suggest either catch the exception, or consider it as a response from a third party, or getNetworkInterfaces() it using the getNetworkInterfaces() method.

+1
source

The solution of "industrial power" for this, of course, would be to generate a random UUID for each server and include this identifier (it can be int or maybe long) in each packet. If this identifier matches yours, you can delete it.

I am not happy with this solution, because it spends several bytes in each packet of datagrams. But it is simple and effective.

+4
source

Can't you make equals() between packet.getAddress() a Address object for your own host?

+1
source

This is not exactly the answer to your question. But to handle UDP translations, you should look at JGroups :

JGroups is a toolkit for reliable multicast communication. (Note that this does not necessarily mean IP Multicast, JGroups can also use transports such as TCP). It can be used to create process groups whose members can send messages to each other. Main functions:

  • Create and delete a group. Group members can LAN or WAN
  • Turn groups on and off
  • Membership Detection and Connection / Left / Failure Notification Members
  • Detection and removal of broken members.
  • Sending and receiving messages from group to group (Point-to-multipoint)
  • Sending and receiving messages from member to member (Point-to-point)
+1
source

All Articles