为什么我的UDP多播无法到达网络上的计算机? [英] Why are my UDP multicast not reaching machines on the network?

查看:114
本文介绍了为什么我的UDP多播无法到达网络上的计算机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用UDP多播设置自动发现,并正在使用来自Internet的一些示例代码.当我在同一台计算机上运行客户端和服务器时,这似乎可以正常工作,但是当我在不同计算机上运行它们时,无论是在计算机上运行VM的计算机(virtualBox)还是在计算机上的其他真实"计算机上运行网络,那么其他计算机似乎再也不会收到正在广播的消息.

I am trying to set up auto discovery using UDP multicasting, and am using some sample code from the internet. this seems to work ok when I run the client and the server on the same machine, but when I run them on different machines, either with a machine running in a VM on my machine (virtualBox) or on other 'real' machines on the network then the other machines never seem to receive the messages being broadcast.

在进行一些谷歌搜索之后,可能的罪魁祸首可能是路由器(SpeedTouch 780)可能丢弃了数据包.如何检查是否是这种情况?我还有其他可以检查并找出问题的东西吗?可能完全是其他东西吗?

After some googling it seems the likely culprit would be the router (SpeedTouch 780) which might be dropping the packets. How can I check if this is the case? Are their other things which I can check to try and track down the problem? Might it be something else entirely?

codez:

服务器代码

using System;
using System.Net.Sockets;
using System.Text;

internal class StockPriceMulticaster
    {
    private static string[] symbols = {"ABCD", "EFGH", "IJKL", "MNOP"};

    public static void Main ()
        {
        using (UdpClient publisher = new UdpClient ("230.0.0.1", 8899))
            {
            Console.WriteLine ("Publishing stock prices to 230.0.0.1:8899");
            Random gen = new Random ();
            while (true)
                {
                int i = gen.Next (0, symbols.Length);
                double price = 400*gen.NextDouble () + 100;
                string msg = String.Format ("{0} {1:#.00}", symbols, price);
                byte[] sdata = Encoding.ASCII.GetBytes (msg);
                publisher.Send (sdata, sdata.Length);
                System.Threading.Thread.Sleep (5000);
                }
            }
        }
    }

和客户:

using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class StockPriceReceiver{
    public static void Main(){
        UdpClient subscriber = new UdpClient(8899);
        IPAddress addr = IPAddress.Parse("230.0.0.1");
        subscriber.JoinMulticastGroup(addr);
        IPEndPoint ep = null;
        for(int i=0; i<10;i++){
            byte[] pdata = subscriber.Receive(ref ep);
            string price = Encoding.ASCII.GetString(pdata);
            Console.WriteLine(price);
        }
        subscriber.DropMulticastGroup(addr);
    }
}

编辑

因此,出于某种原因,它似乎仅在VirtualBox主机的网络接口上发布UDP数据包,而不是在所有计算机都连接的无线网络上发布UDP数据包.只是需要弄清楚如何使它不那么做... 所以改为在答案中添加分辨率...

So it seems that it is publishing the UDP packets on the VirtualBox host only network interface for some reason rather than the wireless network that all the machines are connected to. Just need to figure out how to make it not do that... So added the resolution in an answer instead...

推荐答案

所以问题出在当我拥有多个活动网络连接时,它选择了一个并使用了该连接,这导致UDP数据包被在客户端正在侦听的其他网络连接上发送.当我安装Virtual Box时,它已经安装并激活了VirtualBox Host-only网络适配器,因此只能支持主机网络连接.当我将VirtualBox切换到仅主机模式时,开始接收数据包.禁用VirtualBox适配器并切换回桥接连接也可以.

So the issue turned out to be that as I had more than 1 active network connection it was choosing one and using that and that was causing the UDP packets to be sent out on a different network connection that the client was listening on. As i had installed Virtual box it had installed and activated the VirtualBox Host-only network adapter, so that host only network connections could be supported. When I switched VirtualBox over to host only mode the packets started to be received. Disabling the VirtualBox adapter and switching back to a bridged connection also worked.

这篇关于为什么我的UDP多播无法到达网络上的计算机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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