UDP 最后数据从不同来源接收超时
我有一个 C# Winform,它通过单个端口接受来自多个设备的 UDP 数据包。
设备以设定的时间间隔向我发送 UDP 数据包,我想实现一种方法来了解设备何时停止发送数据包。
我使用单个 UDPClient 并使用接收函数。收到数据后,我将 RemoteIPEndPoint 传递回主窗体以更新值。
最好的方法是什么?
感谢您的帮助!
i have a C# Winform that is accepting UDP Packets from several devices a single port.
The devices sent UDP packets to me at a set interval and i want to implement a way to know when a device has stopped sending packets.
I use a single UDPClient and using the Receive function. When data is received, i pass the RemoteIPEndPoint back to my mainform to update values.
What would be the best way to this?
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个有点抽象的问题,可能有很多解决方案。一种简单快速的解决方案是创建一个哈希表,其中,
HashKey = 远程设备的IP
值 = 从该设备收到最后一个数据包时的时间戳。
现在,每当您收到数据包时,您只需更新哈希表,就像
if (哈希表包含“RemoteEndPoint”)
{
hashTable [remoteEndPoint] = now() // 当前时间。
}
别的
{
// 如果您想将新设备添加/注册到列表中,请在此处执行此操作
除此之外,
您可以运行一个具有一定间隔的计时器来遍历哈希表并检查某个时间戳是否小于(当前时间 - 您设置的间隔),然后您可以说您尚未从该端点收到数据。
This is a little abstract questions and there could be many solutions to that. One simple and quick solution would be to create a HashTable where,
HashKey = IP of Remote device
Value = Timestamp when last packet received from that device.
Now whenever you receive a packet you just update the hashtable like
if (hashTable contains "RemoteEndPoint")
{
hashTable [ remoteEndPoint ] = now() // current Time.
}
else
{
// if you want to add/register new device to your list, do it here
}
Beside that you can just run a Timer with some interval that traverse the HashTable and check if some TimeStamp is less then (currentTime - Your Set Interval), then you can say that you haven't received the data from that end point.