How to do Network discovery using UDP broadcast

前端 未结 5 1891
眼角桃花
眼角桃花 2020-11-27 02:23

I want to to do network discovery using UDP Broadcast in C#. I don\'t know how to do this. Can you give me advice on how to do it?

I want to do like this tutorial.

5条回答
  •  天涯浪人
    2020-11-27 03:24

    I had the same question but it was not that easy for me as the answer that @rufanov suggests.

    Here some situation I had:

    • Since my application is running normally in a computer that has several network interfaces, I had the problem that the broadcast message was sent only in one of the adapters. To solve this situation I had to get first all the network adapter list and go one by one sending the broadcast message and receiving the answer message.
    • It is important that you bind the correct localIpEndPoint to your adapters ip address, otherwise you will have problems with the broadcast address by sending.

    After some reserch and work I got to this solution. This code corresponds to the server side and will make the network discovery of all devices answering to the braodcast message.

    public static void SNCT_SendBroadcast(out List DevicesList)
    {
      DevicesList = new List();
      byte[] data = new byte[2]; //broadcast data
      data[0] = 0x0A;
      data[1] = 0x60;
    
      IPEndPoint ip = new IPEndPoint(IPAddress.Broadcast, 45000); //braodcast IP address, and corresponding port
    
      NetworkInterface[] nics = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces(); //get all network interfaces of the computer
    
      foreach (NetworkInterface adapter in nics)
      {
        // Only select interfaces that are Ethernet type and support IPv4 (important to minimize waiting time)
        if (adapter.NetworkInterfaceType != NetworkInterfaceType.Ethernet) { continue; }
        if (adapter.Supports(NetworkInterfaceComponent.IPv4) == false) { continue; }
        try
        {
            IPInterfaceProperties adapterProperties = adapter.GetIPProperties();    
            foreach (var ua in adapterProperties.UnicastAddresses)
            {
                if (ua.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                {
                 //SEND BROADCAST IN THE ADAPTER
                    //1) Set the socket as UDP Client
                    Socket bcSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //broadcast socket
                    //2) Set socker options
                    bcSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
                    bcSocket.ReceiveTimeout = 200; //receive timout 200ms
                    //3) Bind to the current selected adapter
                    IPEndPoint myLocalEndPoint = new IPEndPoint(ua.Address, 45000);
                    bcSocket.Bind(myLocalEndPoint);
                    //4) Send the broadcast data
                    bcSocket.SendTo(data, ip);
    
                //RECEIVE BROADCAST IN THE ADAPTER
                    int BUFFER_SIZE_ANSWER = 1024;
                    byte[] bufferAnswer = new byte[BUFFER_SIZE_ANSWER];
                    do
                    {
                        try
                        {
                            bcSocket.Receive(bufferAnswer);
                            DevicesList.Add(GetMyDevice(bufferAnswer)); //Corresponding functions to get the devices information. Depends on the application.
                        }
                        catch { break; }
    
                    } while (bcSocket.ReceiveTimeout != 0); //fixed receive timeout for each adapter that supports our broadcast
                    bcSocket.Close();
                }
            }
          }
          catch { }
      }
      return;
    }
    

提交回复
热议问题