【问题标题】:Broadcast not reaching hosts广播未到达主机
【发布时间】:2014-02-06 19:37:44
【问题描述】:

我需要几个 C#(一个服务器和一个客户端)例程,这些例程允许我从 PC 中查找连接到子网的 (WinCE) 设备。尽管测试了几个示例,但我无法使广播正常工作。

在设备上运行的是基于 C#(紧凑框架)的服务器,其代码如下所示:

public void SomeClass()
{

    IPEndPoint ipUDP = new IPEndPoint(IPAddress.Any, 5003);
    this.serverUDP = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
    this.serverUDP.Bind(ipUDP);
    IPEndPoint sender = new IPEndPoint(IPAddress.Any, 6001);
    this.epSender = (EndPoint)sender;
    this.serverUDP.BeginReceiveFrom(this.bufferUDP, 0, 20, SocketFlags.None, ref epSender, new AsyncCallback(someMethod), null);
}

在 PC 中,我运行一个客户端,该客户端应该使用广播消息调用这些服务器:

public static void Discover(ref string mensaje, string IPDestino, ref ArrayList equipos)
{            
    UdpClient client = null;
    try
    {
        client = new UdpClient(6001);

        byte[] toSend = Encoding.ASCII.GetBytes("FINDSTT");
        client.Send(toSend, toSend.Length, "255.255.255.255", 5003);

        //Some logic
    }
    catch (Exception ex)
    {
        //Some handling
    }
    if (client != null)
        client.Close();
}

问题是广播永远不会到达服务器。但是,如果我在 client.Send() 中更改特定地址的广播地址(255.255.255.255),它会被服务器接收。但是这样一来,我就失去了寻找我事先不知道的设备的主要目标。

【问题讨论】:

  • 如果您在服务器上运行数据包捕获,您是否看到任何生成的广播流量? (而且,我猜更重要的是,客户端和服务器是否在同一个逻辑网络上?)
  • 您的客户端是否在 Windows 7 上运行?
  • 永远不会调用 AsyncCallback(在代码“someMethod”中调用)。如果一些数据来了,它应该被调用。在这种情况下,PC 和 WinCE 设备通过以太网连接到同一个交换机。
  • @OnoSendai,这是 Windows 8 笔记本电脑。
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。

标签: c# broadcast


【解决方案1】:

如果您的客户端在 Windows 7/8 下运行,问题可能在于操作系统处理零网络广播地址255.255.255.255的方式。

如果是这样,那么this post may be of some help to you

还有,相关的:

How to fix the global broadcast address (255.255.255.255) behavior on Windows?(技术网,Win7)

IPv4 broadcast problem (win8)

【讨论】:

  • 您的回答解决了一半的问题。实际上,没有为所有适配器发送广播。这个可以通过安装WinIPBroadcast来解决:github.com/dechamps/WinIPBroadcast
  • @OnoSendsai,您的回答解决了我最初的问题。实际上,没有为所有适配器发送广播。这可以通过安装我在您的一个链接中找到的服务来解决:github.com/dechamps/WinIPBroadcast
  • @Ferite 很高兴听到。我过去遇到过同样的问题,我通过编程映射网络接口并通过每个发送(这里是双关语)单独的广播数据包来解决它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
相关资源
最近更新 更多