如何在 C# 中跨本地网络进行 UDP 多播?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/767215/
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
How to do a UDP multicast across the local network in c#?
提问by Simon
I am trying to get some simple UDP communication working on my local network.
我正在尝试在我的本地网络上进行一些简单的 UDP 通信。
All i want to do is do a multicast to all machines on the network
我想要做的就是对网络上的所有机器进行多播
Here is my sending code
这是我的发送代码
public void SendMessage(string message)
{
var data = Encoding.Default.GetBytes(message);
using (var udpClient = new UdpClient(AddressFamily.InterNetwork))
{
var address = IPAddress.Parse("224.100.0.1");
var ipEndPoint = new IPEndPoint(address, 8088);
udpClient.JoinMulticastGroup(address);
udpClient.Send(data, data.Length, ipEndPoint);
udpClient.Close();
}
}
and here is my receiving code
这是我的接收代码
public void Start()
{
udpClient = new UdpClient(8088);
udpClient.JoinMulticastGroup(IPAddress.Parse("224.100.0.1"), 50);
receiveThread = new Thread(Receive);
receiveThread.Start();
}
public void Receive()
{
while (true)
{
var ipEndPoint = new IPEndPoint(IPAddress.Any, 0);
var data = udpClient.Receive(ref ipEndPoint);
Message = Encoding.Default.GetString(data);
// Raise the AfterReceive event
if (AfterReceive != null)
{
AfterReceive(this, new EventArgs());
}
}
}
It works perfectly on my local machine but not across the network.
它可以在我的本地机器上完美运行,但不能跨网络运行。
-Does not seem to be the firewall. I disabled it on both machines and it still did not work.
- 似乎不是防火墙。我在两台机器上都禁用了它,但它仍然无法正常工作。
-It works if i do a direct send to the hard coded IP address of the client machine (ie not multicast).
- 如果我直接发送到客户端机器的硬编码 IP 地址(即不是多播),它就可以工作。
Any help would be appreciated.
任何帮助,将不胜感激。
采纳答案by Alnitak
Does your local network hardware support IGMP?
您的本地网络硬件是否支持IGMP?
It's possible that your switch is multicast aware, but if IGMP is disabled it won't notice if any attached hardware subscribes to a particular multicast group so it wouldn't forward those packets.
您的交换机可能具有多播感知能力,但如果禁用 IGMP,它不会注意到是否有任何连接的硬件订阅了特定的多播组,因此它不会转发这些数据包。
To test this, temporarily connect two machines directly together with a cross-over cable. That should (AFAICR) always work.
要对此进行测试,请使用交叉电缆临时将两台机器直接连接在一起。这应该(AFAICR)总是有效的。
Also, it should be the serverhalf of the code that has the TTL argument supplied to JoinMulticastGroup()
, not the client half.
此外,应该是将TTL 参数提供给 的代码的服务器部分JoinMulticastGroup()
,而不是客户端部分。
回答by Jonathan C Dickinson
I can't see a TTLspecified anywhere in the code. Remember that TTL was originally meant to be in unit seconds, but is has become unit hops. This means that by using a clever TTL you could eliminate passing through the router. The default TTL on my machine is 32 - I think that should be more than adequate; but yours may actually be different (UdpClient.Ttl) if your system has been through any form of a security lockdown.
我看不到代码中任何地方指定的TTL。请记住,TTL 最初旨在以秒为单位,但现在已成为单位跃点。这意味着通过使用巧妙的 TTL,您可以避免通过路由器。我机器上的默认 TTL 是 32 - 我认为这应该绰绰有余;但如果您的系统已经通过任何形式的安全锁定,您的系统实际上可能会有所不同(UdpClient.Ttl)。
I can't recommend the TTL you need - as I personally need to do a lot of experimentation.
我不能推荐您需要的 TTL - 因为我个人需要做很多实验。
If that doesn't work, you could have a look at these articles:
如果这不起作用,你可以看看这些文章:
All-in-all it looks like there has been success with using Sockets and not UdpClients.
总而言之,使用 Sockets 而不是 UdpClients 看起来已经取得了成功。
Your chosen multicast group could also be local-only. Try another one.
您选择的多播组也可以是仅限本地的。尝试另一个。
Your physical network layer could also be causing issues. I would venture to question switches and direct (x-over) connections. Hubs and all more intelligent should handle them fine. I don't have any literature to back that, however.
您的物理网络层也可能导致问题。我会冒险质疑开关和直接(x-over)连接。集线器和更聪明的人应该可以很好地处理它们。然而,我没有任何文献支持这一点。
回答by Uri Maimon - Nominal
I've just spent 4 hours on something similar (I think), the solution for me was:
我刚刚花了 4 个小时在类似的事情上(我认为),我的解决方案是:
client.Client.Bind(new IPEndPoint(IPAddress.Any, SSDP_PORT));
client.JoinMulticastGroup(SSDP_IP,IP.ExternalIPAddresses.First());
client.MulticastLoopback = true;
Using a specific (first external) IP address on the multicast group.
在多播组上使用特定(第一个外部)IP 地址。