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

Stout 286 Reputation points
2021-09-21T17:54:00.527+00:00

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.

https://learn.microsoft.com/en-us/dotnet/framework/network-programming/using-udp-services

Xamarin Server:

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

        IPAddress broadcast = IPAddress.Parse("192.168.1.255");  

        //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("192.168.1.255"), listenPort);  

        try  
        {  
            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)  
        {  
            Console.WriteLine(e);  
        }  
        finally  
        {  
            listener.Close();  
        }  
    }  
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,366 questions
0 comments No comments
{count} votes

Accepted answer
  1. Kyle Wang 5,531 Reputation points Microsoft Vendor
    2021-09-22T02:02:12.26+00:00

    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 "192.168.1.255".

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

    For example,

    mobile: 192.168.1.100
    desktop: 192.168.1.101

    Regards,
    Kyle


    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.