[SOLVED] UDP Discovery Code Unable to Receive UDP Broadcast.

Stout 281 Reputation points

Hi. I need to write a mobile app (Xamarin) to send a UDP broadcast, then have a desktop app (WPF/WinForms) listen to the UDP broadcast. I got the sample from the link below, but it is not working. The Xamarin app seems okay (on a Samsung phone), but the desktop app never gets past the "listener.Receive(ref ...)" statement. I would appreciate help. Thanks.


Xamarin Server:

    private void StartUdpDiscovery()  
        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);  

        IPAddress broadcast = IPAddress.Parse("");  

        //byte[] sendbuf = Encoding.ASCII.GetBytes(args[0]);  
        IPEndPoint ep = new IPEndPoint(broadcast, 11000);  

        byte[] data = new byte[2]; //broadcast data  
        data[0] = 0x0A;  
        data[1] = 0x60;  
        s.SendTo(data, ep);  

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

Desktop Client/Listener:

    private void StartUdpListener()  
        UdpClient listener = new UdpClient(listenPort);  
        //IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);  
        IPEndPoint groupEP = new IPEndPoint(IPAddress.Parse(""), listenPort);  

            while (true)  
                Console.WriteLine("Waiting for broadcast");  
                byte[] bytes = listener.Receive(ref groupEP); // NOTE: THE DEBUGGER STALLS HERE AND NEVER MOVES ON...  

                Console.WriteLine($"Received broadcast from {groupEP} :");  
                Console.WriteLine($" {Encoding.ASCII.GetString(bytes, 0, bytes.Length)}");  
        catch (SocketException e)  
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,001 questions
0 comments No comments
{count} votes

Accepted answer
  1. Kyle Wang 5,516 Reputation points

    Hi StoutGamer,

    Welcome to our Microsoft Q&A platform!

    The sample provided in the documentation works fine in my test.

    Please note that you should use the IP address of your "Desktop Client device", rather than "".

    And ensure that your mobile app and desktop app are on the same network segment.

    For example,



    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful