JAVA:广播UDP数据包时检测自我 [英] JAVA: Detect self when broadcasting UDP Packets

查看:132
本文介绍了JAVA:广播UDP数据包时检测自我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在广播UDP数据包的线程.我有另一个线程正在监听相同的广播.这样我的应用程序可以检测网络上的其他实例.但是,我有一个问题,当一个实例广播时,它会接听自己并认为它本身就是另一个节点.我的问题是,您如何才能可靠地确定某个数据包来自您自己,因此可以忽略它?我之所以说是可靠的,是因为我知道您可以在套接字上调用setLoopbackMode(true)来防止环回,但是根据api文档,这种方法并不总是有效?

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?

推荐答案

检查源IP地址. 如果它等于您自己的地址,请丢弃该包裹.如果无法确定从哪个接口发送,请确保检查所有可能的源地址.

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.

这篇关于JAVA:广播UDP数据包时检测自我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆