3

I was just playing around with a UdpClient and noticed setting UdpClient.EnableBroadcast to true or false doesn't have any (side)effect, I am able to broadcast with it either way:

using (UdpClient client = new UdpClient())
{
    byte[] data = Encoding.ASCII.GetBytes("Hello");
    while (true)
    {
        client.Send(data, data.Length, new IPEndPoint(IPAddress.Broadcast, 45678));
        Console.WriteLine("sent");
        Console.ReadKey();
    }
}

The MSDN page is rather vague on this, but it sounds like it should cause an exception if you broadcast with the property set to false:

Gets or sets a Boolean value that specifies whether the UdpClient may send or receive broadcast packets. ... true if the UdpClient allows broadcast packets; otherwise, false. The default is false.

Not a critical issue, but it just made me wonder. Is this a bug or is the property just there so you can refer to it later and find out whether you are meant to broadcast with it or not?

Saeb Amini
  • 23,054
  • 9
  • 78
  • 76
  • 1
    I've noticed the same behavior and wondered the same thing. It's not clear what this property is designed for, but indeed we can apparently send broadcast packets regardless of the setting. – blitz_jones Dec 06 '13 at 17:51

1 Answers1

0

You need to enable the broadcast mode when you initialize the udp socket, something like as under

this.someUdpSocket.EnableBroadcast = true;
this.someUdpSocket.Client.Bind(new IPEndPoint(IPAddress.Any, 0));

New Edit:

Above doesn't work and the below code

this.someUdpSocket.Client.EnableBroadcast = false;

also does not serve the purpose. So as for your answer "Yes it can be called as a bug or the property might be relevant in some other context."

But if you want to disable the broadcasting explicitly you can do it like as under:

this.someUdpSocket.Client.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.Broadcast, false);

Thanks for the motivation.

Jsinh
  • 2,539
  • 1
  • 19
  • 35
  • This is irrelevant. The point is that I _am able to_ broadcast, while the "broadcast mode" is _"disabled"_. – Saeb Amini Nov 13 '12 at 13:55
  • Check "New Edit" apologize for making it short before and not relevant to question asked. – Jsinh Nov 13 '12 at 14:14