繁体   English   中英

使用 UDP 广播检测网络上的多个设备 - C#

[英]Detect multiple devices on network using UDP broadcast - C#

我知道有很多关于 UDP 广播用于检测网络设备的问题,但没有一个答案对我真正有用。 我猜我遗漏了一些东西,如果有任何帮助,我将不胜感激。

我设计了一个位于网络上并运行 UDP 服务器的系统。 在某个端口 (AP) 上收到消息后,它将向发送 IP/端口发送回复。

在 C# 端,我使用以下代码:

UdpClient Client = new UdpClient();
                var RequestData = Encoding.ASCII.GetBytes("Discover");
                var ServerEp = new IPEndPoint(IPAddress.Any, 0);
                byte[] ServerResponseData = { 0 };

                Client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);

                Client.EnableBroadcast = true;
                Client.Send(RequestData, RequestData.Length, new IPEndPoint(IPAddress.Broadcast, AnnouncePort));

                ServerResponseData = LanguageUtils.IgnoreErrors(() => Client.Receive(ref ServerEp));

                if (ServerResponseData != null)
                {
                    var ServerResponse = Encoding.ASCII.GetString(ServerResponseData);

                    deviceList.Add(ServerEp.Address.ToString());

                    QueryFoundDevices(deviceList);
                    AvailableDevicesList.Nodes[0].Expand();
                }

我的问题是我一次只能检测到我的一个设备。 理想情况下,我希望能够从无限数量的设备接收消息。

我也尝试过使用异步方法,在这种情况下,我只收到自己的消息,看不到任何设备。 代码示例:

static void OnUdpData(IAsyncResult result)
        {
            // this is what had been passed into BeginReceive as the second parameter:
            UdpClient socket = result.AsyncState as UdpClient;
            // points towards whoever had sent the message:
            IPEndPoint source = new IPEndPoint(0, 0);
            // get the actual message and fill out the source:
            byte[] message = socket.EndReceive(result, ref source);
            // do what you'd like with `message` here:
            Console.WriteLine("Got " + message.Length + " bytes from " + source);
            // schedule the next receive operation once reading is done:
            socket.BeginReceive(new AsyncCallback(OnUdpData), socket);
        }

谁能告诉我如何做到这一点?

更新:根据评论 - 这些是基于以太网的设备,我已经验证这两个设备都在使用 Wireshark 进行回复。

我不知道这样做在哪里是正确的,但我现在已经根据 Lex Li 的评论找到了一种方法。 我想我会分享这个,以防其他人有类似的问题。

                UdpClient Client = new UdpClient();
                var RequestData = Encoding.ASCII.GetBytes("Discover");
                var ServerEp = new IPEndPoint(IPAddress.Any, 0);
                byte[] ServerResponseData = { 0 };

                Client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 1000);

                Client.EnableBroadcast = true;
                Client.Send(RequestData, RequestData.Length, new IPEndPoint(IPAddress.Broadcast, AnnouncePort));

                ServerResponseData = LanguageUtils.IgnoreErrors(() => Client.Receive(ref ServerEp));
                string ServerResponse;
                while (ServerResponseData != null)
                {
                    ServerResponse = Encoding.ASCII.GetString(ServerResponseData);

                    deviceList.Add(ServerEp.Address.ToString());

                    QueryFoundDevices(deviceList);
                    AvailableDevicesList.Nodes[0].Expand();

                    ServerResponseData = LanguageUtils.IgnoreErrors(() =>Client.Receive(ref ServerEp))
                }

Lex Li 指出,我在答案中发布的第一个代码部分只会收到第一个回复。 我试图阅读下一个回复,它奏效了。 我现在正在使用上述内容,这可能需要进一步优化,但到目前为止运行良好。

感谢各位的帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM