如何忽略自己的广播 udp 数据包 [英] How to ignore your own broadcast udp packets

查看:61
本文介绍了如何忽略自己的广播 udp 数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下我假设有一张网卡.

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 很困难:getaddrinfo with NULL 返回 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屋!

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