4

I have a thread that is Broadcasting UDP packets. I have another thread that is listening for the same broadcasts. This is so my application can detect other instances on the network. However I have a problem that when one instance broadcasts it picks up itself and thinks it is itself another node. My question is how can you RELIABLY determine that a packet has come from yourself so it can be ignored? I say reliably because I know you can call setLoopbackMode(true) on your sockets to prevent loopback but according to the api documentation this approach does not always work?

Jack Allan
  • 14,554
  • 11
  • 45
  • 57
  • 1
    Note: It seems that setting the loopback mode works fine on windows 7 but running under Ubuntu the setLoopbackMode hint appears to do nothing – Jack Allan Mar 11 '12 at 13:32

1 Answers1

4

Check for the source IP address. If its equivalent to your own address, discard the package. Make sure you check for all possible source addresses if you cannot determine which interface you are sending from.

Jonas Schäfer
  • 20,140
  • 5
  • 55
  • 69
  • How do I determine the IP addresses of my interfaces to do this elimination? – Jack Allan Mar 11 '12 at 13:32
  • 2
    How can you solve the problem when two such programs are running on the same computer? – Matthieu Nov 18 '13 at 08:57
  • @Matthieu They cannot bind to the same port at the same time (with UDP at least), thus they can either not run at the same time or are using a different port. The latter would mean that you would not receive the packet (assuming that you're using the same socket for sending and receiving). – Jonas Schäfer Nov 18 '13 at 19:12
  • 1
    Well, you can call `setReuseAddress(true)` to have them both bound to the same port but you can't establish both-way communication like that, as both would receive their own datagrams without knowing which one is which. I'll figure out something else. Thanks! – Matthieu Nov 21 '13 at 11:40