C# 发送UDP广播,接收多条消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10832770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending UDP broadcast, receiving multiple messages
提问by Paw Baltzersen
I've got 2 programs, 1 for sending an UDP broadcast message and 1 that is listening for this broadcast. My problem is that sometimes when I send a broadcast, the receiver receives 2 messages. Why?
我有 2 个程序,1 个用于发送 UDP 广播消息,1 个用于侦听此广播。我的问题是,有时当我发送广播时,接收器会收到 2 条消息。为什么?
Receiver code:
接收器代码:
public class Receiver {
private readonly UdpClient udp = new UdpClient(15000);
private void StartListening()
{
this.udp.BeginReceive(Receive, new object());
}
private void Receive(IAsyncResult ar)
{
IPEndPoint ip = new IPEndPoint(IPAddress.Any, 15000);
byte[] bytes = udp.EndReceive(ar, ref ip);
string message = Encoding.ASCII.GetString(bytes);
StartListening();
}
}
Sender code:
发件人代码:
public class Sender {
public void Send() {
UdpClient client = new UdpClient();
IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, 15000);
byte[] bytes = Encoding.ASCII.GetBytes("Foo");
client.Send(bytes, bytes.Length, ip);
client.Close();
}
}
采纳答案by Eugene Ryabtsev
Strictly speaking, packet duplication in IP network is allowed behavior of the network and you have to be able to deal with it in your software even if you will somehow get rid of it this time. If you are just wondering about why this happens in your particular case... at a first glance I see nothing wrong with your code. Do you have several IP addresses on Ethernet port of your computer or some such? I think wiresharkmight help get more details about what's going on.
严格来说,IP 网络中的数据包重复是允许网络行为的,即使这次您会以某种方式摆脱它,您也必须能够在您的软件中处理它。如果您只是想知道为什么在您的特定情况下会发生这种情况……乍一看,我认为您的代码没有任何问题。您的计算机的以太网端口上是否有多个 IP 地址?我认为wireshark可能有助于获得有关正在发生的事情的更多详细信息。
回答by Andy
UDP packets aren't reliable, it's totally possible that you'll get the same packet twice or even none at all, when using udp you need to include some kind of unique ID in your data so you can discard errors or request a resend.
UDP 数据包不可靠,您完全有可能两次收到相同的数据包,甚至根本没有收到,使用 udp 时,您需要在数据中包含某种唯一 ID,以便您可以丢弃错误或请求重新发送。
回答by Shiran Abbasi
The reason is when you broadcast you send your message to all the end points in the network. Since you are in the same network, you will receive the same message because it is broadcasted. You can write a custom filter for that.
原因是当您广播时,您将消息发送到网络中的所有端点。由于您在同一个网络中,您将收到相同的消息,因为它是广播的。您可以为此编写自定义过滤器。
回答by Ted W
Keep in mind that (1) UDP packets are VERY reliable for all clients on the same router, and (2) packet duplication can occur when more than one path is available from the server to the client.
请记住,(1) UDP 数据包对于同一路由器上的所有客户端都非常可靠,并且 (2) 当从服务器到客户端的路径超过一条时,可能会发生数据包重复。
I had this problem on a VM, and it was solved by a network guru smarter than me who added a virtual NIC to the VM and had me run "route delete" and "route add" commands.
我在虚拟机上遇到了这个问题,它被一个比我更聪明的网络专家解决了,他向虚拟机添加了一个虚拟网卡,并让我运行“路由删除”和“路由添加”命令。
Specifically (on Windows 10 VM), if the new virtual NIC is 10.10.10.10: route delete 224.0.0.0 mask 240.0.0.0 route add 224.0.0.0 mask 240.0.0.0 10.10.10.10
具体来说(在 Windows 10 VM 上),如果新的虚拟 NIC 是 10.10.10.10: route delete 224.0.0.0 mask 240.0.0.0 route add 224.0.0.0 mask 240.0.0.0 10.10.10.10

