【发布时间】: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?”,其中的共识是“不,他们不应该”。