tscLegend
tscLegend

Reputation: 76

UDP Broadcast and Receive no responses after Single

Currently trying to access devices on my network by udp. The sendAsync is working properly however on receive I am only getting back a single message. The message returned message received is the response of the message sent I believe, but all the devices that correctly respond, viewed in wireshark never get into the application. Not sure if Im missing some firewall setting, or that the receiver isn't on the correct NIC, but have tried various options and binding directly to my computer's IPAddress.

    static async Task Main(string[] args)
        {
            await Task.Factory.StartNew(() => StartListener());
            // Give thread time to start listening
            Thread.Sleep(1000);

            var endpoint = new IPEndPoint(IPAddress.Broadcast, 56700);
            var sender = new UdpClient();
            sender.Client.Bind(new IPEndPoint(new UdpConnector(NetworkInterfaceType.Wireless80211, AddressFamily.InterNetwork).GetActiveIPAddresses(null).FirstOrDefault(), 56700)); //Make sure were on the correct NIC
            sender.Client.Blocking = false;
            sender.DontFragment = true;
            sender.Client.EnableBroadcast = true;
            sender.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            sender.Connect(endpoint);
            var msg = new byte[] { 0x24, 0x00, 0x00, 0x34, 0x7B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00 };
            await sender.SendAsync(msg, msg.Length);

            Console.WriteLine("Message sent to the broadcast address");
            Console.ReadKey();
        }

        private const int Port = 56700;

        private static async Task StartListener()
        {
            UdpClient udpClient = new UdpClient();
            IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, 0);
            //udpClient.Client.Bind(groupEP);

            try
            {
                while (true)
                {
                    Console.WriteLine("Waiting for broadcast");
                    var bytes = await udpClient.ReceiveAsync();

                    Console.WriteLine("Received broadcast from {0} :\n {1}\n",
                        bytes.RemoteEndPoint.ToString(),
                        Encoding.ASCII.GetString(bytes.Buffer, 0, bytes.Buffer.Length));
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                udpClient.Close();
            }
        }
    }

Wireshark trace

Upvotes: 2

Views: 298

Answers (2)

Xami Yen
Xami Yen

Reputation: 267

Here is a link to Microsoft document on this subject. Might be of help to you: UdpClient.BeginReceive(AsyncCallback, Object) Method

Upvotes: 0

scott
scott

Reputation: 1277

The call to Bind is commented out.

Upvotes: 1

Related Questions