如何在 C# 中跨本地网络进行 UDP 多播?

发布于 2024-07-17 13:15:24 字数 1418 浏览 3 评论 0原文

我正在尝试在我的本地网络上进行一些简单的 UDP 通信。

我想要做的就是对网络上的所有计算机进行多播

这是我的发送代码

    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();
        }
    }

,这是我的接收代码

    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());
            }
        }
    }

它在我的本地计算机上完美运行,但不能跨网络运行。

- 似乎不是防火墙。 我在两台机器上都禁用了它,但它仍然不起作用。

-如果我直接发送到客户端计算机的硬编码IP地址(即不是多播),它就可以工作。

任何帮助,将不胜感激。

I am trying to get some simple UDP communication working on my local network.

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).

Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小清晰的声音 2024-07-24 13:15:24

您的本地网络硬件是否支持 IGMP

您的交换机可能支持多播,但如果禁用 IGMP,则它不会注意到是否有任何连接的硬件订阅了特定的多播组,因此它不会转发这些数据包。

要对此进行测试,请使用交叉电缆临时将两台机器直接连接在一起。 这应该(AFAICR)总是有效的。

另外,向 JoinMulticastGroup() 提供 TTL 参数的代码应该是服务器部分,而不是客户端部分。

Does your local network hardware support 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.

To test this, temporarily connect two machines directly together with a cross-over cable. That should (AFAICR) always work.

Also, it should be the server half of the code that has the TTL argument supplied to JoinMulticastGroup(), not the client half.

岁月染过的梦 2024-07-24 13:15:24

我刚刚花了 4 个小时在类似的事情上(我认为),对我来说的解决方案是:

client.Client.Bind(new IPEndPoint(IPAddress.Any, SSDP_PORT));
client.JoinMulticastGroup(SSDP_IP,IP.ExternalIPAddresses.First());
client.MulticastLoopback = true;

在多播组上使用特定的(第一个外部)IP 地址。

I've just spent 4 hours on something similar (I think), the solution for me was:

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.

铜锣湾横着走 2024-07-24 13:15:24

我在代码中的任何位置都看不到指定的 TTL 。 请记住,TTL 最初的单位是秒,但现在已变成单位跳数。 这意味着通过使用巧妙的 TTL,您可以消除通过路由器的情况。 我的机器上的默认 TTL 是 32 - 我认为这应该足够了; 但如果您的系统经过任何形式的安全锁定,您的系统实际上可能会有所不同(UdpClient.Ttl)。

我不能推荐您需要的 TTL - 因为我个人需要做大量的实验。

如果这不起作用,您可以查看以下文章:

总而言之,看起来使用 Sockets 而不是 UdpClients 已经取得了成功。

您选择的多播组也可以是仅限本地的。 尝试另一个。

您的物理网络层也可能导致问题。 我冒昧地质疑开关和直接(交叉)连接。 集线器和所有更智能的设备应该可以很好地处理它们。 然而,我没有任何文献支持这一点。

I can't see a TTL specified 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.

I can't recommend the TTL you need - as I personally need to do a lot of experimentation.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文