如何忽略自己的广播udp数据包 [英] How to ignore your own broadcast udp packets
问题描述
以下,我假设一张网卡.
For the following I'm assuming one network card.
我的程序中有一个组件旨在让子网中的其他人知道它的存在.为此,我实现了一个解决方案,其中无论程序何时启动(以及之后定期),它将向INADDR_BROADCAST
发送广播-监听所需端口的任何人都将记住它来自何处,以供以后使用.
I have a component of my program which is designed to let others in the subnet know of its existence. For this, I've implemented a solution where whenever the program starts up (and periodically afterwards) it sends a broadcast to INADDR_BROADCAST
- whoever listens on the required port will remember where it came from for later use.
问题是我不想记住自己的广播.我认为从理论上讲这很容易-只需找出本地ip并与您在recvfrom
中获得的内容进行比较即可.
The problem with this is that I don't want to remember my own broadcasts. I thought that in theory this would be easy to do - simply find out the local ip and compare to what you get in recvfrom
.
但是,我发现很难获得本地IP:NULL的getaddrinfo
返回127.0.0.1
,主机名的getaddrinfo
返回公共ip.谁能指出我寻找实际子网ip的方向?我想我一定会在这里遗漏一些非常明显的东西,但是...我仍然在遗漏它:)
However, I've found it difficult to get the local IP: getaddrinfo
with NULL returns 127.0.0.1
, getaddrinfo
with the hostname returns the public ip. Can anyone point me in the direction of finding the actual subnet ip ? I think I must be missing something very obvious here but well... I'm still missing it :)
注意:我已经阅读了广播中的其他SO问题,尤其是以下内容: UDP-在所有接口上广播,但是我还没有解决多接口问题.
Note: I've read other SO questions on broadcasts, in particular this one: UDP-Broadcast on all interfaces but I haven't gotten round to the multiple interface issue yet.
推荐答案
在启动时,您可以广播具有随机(但已跟踪)值的其他消息,然后等待该消息发现自己的地址,从那时起启用后,您可以发送普通邮件,而忽略您的来源邮件.
Well at start-up you could broadcast a different message with random (but tracked) value, then wait for that message, to discover your own address, from then on, you can send normal messages, ignoring your sourced messages.
这篇关于如何忽略自己的广播udp数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!