[SOLVED] How can send a UDP broadcast to any IP address on the network?

Stout 286 Reputation points
2021-09-22T19:48:01.81+00:00

Hi. I'm trying to send a UDP broadcast to any machine within my subnet. It works well when I specify the IP address (e.g. 192.168.0.19), but I need it to be generic (192.168.x.x). How can I do that?

I tried IPAddress.Any, but it's not working. I don't even know what the term is called (IP Masking, maybe?).

// Listener Code:

private void StartUdpListener()
{
    var listener = new UdpClient(_listenPort) { EnableBroadcast = true }; // set EnableBroadcast property to true
    //IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
    IPEndPoint groupEP = new IPEndPoint(IPAddress.Parse("192.168.255.255"), listenPort); // Should it be 192.168.255.255???

    try
    {
        while (true)
        {
            Console.WriteLine("Waiting for broadcast");
            byte[] bytes = listener.Receive(ref groupEP);

            Console.WriteLine($"Received broadcast from {groupEP} :");
            Console.WriteLine($" {Encoding.ASCII.GetString(bytes, 0, bytes.Length)}");
        }
    }
    catch (SocketException e)
    {
        Console.WriteLine(e);
    }
    finally
    {
        listener.Close();
    }
}

 // Server Code:

private void SendUdpBroadcast()
{
    var myIpAddressSegments = MyIpAddress.GetAddressBytes();
    var firstTwoOctetsSegment = string.Join(".", myIpAddressSegments[0], myIpAddressSegments[1]);

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

    var broadcast = IPAddress.Parse(firstTwoOctetsSegment + ".255.255"); // Gets set to 192.168.255.255. Is this correct???

    var sendbuf = Encoding.ASCII.GetBytes("Testing...");
    var ep = new IPEndPoint(broadcast, _listenPort);

    s.SendTo(sendbuf, ep);

    Console.WriteLine("Message sent to the broadcast address");
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,233 questions
{count} votes

Accepted answer
  1. AgaveJoe 26,191 Reputation points
    2021-09-24T20:59:34.74+00:00

    Given your gateway and subnet mask the broadcast IP is 192.168.0.255.

    Sender

    using System;  
    using System.Net;  
    using System.Net.Sockets;  
    using System.Text;  
      
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);  
      
            IPAddress broadcast = IPAddress.Parse("192.168.0.255");  
      
            byte[] sendbuf = Encoding.ASCII.GetBytes("Hello World!");  
            IPEndPoint ep = new IPEndPoint(broadcast, 11000);  
      
            s.SendTo(sendbuf, ep);  
      
            Console.WriteLine("Message sent to the broadcast address");  
        }  
    }  
    

    Listener

    using System;  
    using System.Net;  
    using System.Net.Sockets;  
    using System.Text;  
      
    public class UDPListener  
    {  
        private const int listenPort = 11000;  
      
        private static void StartListener()  
        {  
            UdpClient listener = new UdpClient(listenPort);  
            IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);  
      
            try  
            {  
                while (true)  
                {  
                    Console.WriteLine("Waiting for broadcast");  
                    byte[] bytes = listener.Receive(ref groupEP);  
      
                    Console.WriteLine($"Received broadcast from {groupEP} :");  
                    Console.WriteLine($" {Encoding.ASCII.GetString(bytes, 0, bytes.Length)}");  
                }  
            }  
            catch (SocketException e)  
            {  
                Console.WriteLine(e);  
            }  
            finally  
            {  
                listener.Close();  
            }  
        }  
      
        public static void Main()  
        {  
            StartListener();  
        }  
    }  
    

    Use UDP services
    Ip Calculator

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful