簡體   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