问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/9655279/java-detect-self-when-broadcasting-udp-packets