Jack Allan
Jack Allan

Reputation: 15014

JAVA: Detect self when broadcasting UDP Packets

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?

Upvotes: 4

Views: 2175

Answers (1)

Jonas Schäfer
Jonas Schäfer

Reputation: 20738

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.

Upvotes: 4

Related Questions