JAVA: Detect self when broadcasting UDP Packets

大兔子大兔子 提交于 2019-12-23 15:01:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!