Editare

Partajați prin


Socket.EnableBroadcast Property

Definition

Gets or sets a Boolean value that specifies whether the Socket can send broadcast packets.

public:
 property bool EnableBroadcast { bool get(); void set(bool value); };
public bool EnableBroadcast { get; set; }
member this.EnableBroadcast : bool with get, set
Public Property EnableBroadcast As Boolean

Property Value

true if the Socket allows sending broadcast packets; otherwise, false. The default is false.

Exceptions

This option is valid for a datagram socket only.

The Socket has been closed.

Examples

The following code example demonstrates the use of the EnableBroadcast property.

static void ConfigureUdpSocket(Socket^ udpSocket)
{

    // Set the Don't Fragment flag.
    udpSocket->DontFragment = true;
     
    // Enable broadcast.
    udpSocket->EnableBroadcast = true;
     
    // Disable multicast loopback.
    udpSocket->MulticastLoopback = false;
    Console::WriteLine("Udp Socket configured:");
    Console::WriteLine("  DontFragment {0}", 
        udpSocket->DontFragment);
    Console::WriteLine("  EnableBroadcast {0}", 
        udpSocket->EnableBroadcast);
    Console::WriteLine("  MulticastLoopback {0}", 
        udpSocket->MulticastLoopback);
}
static void ConfigureUdpSocket(Socket udpSocket)
{
    // set the Don't Fragment flag.
    udpSocket.DontFragment = true;
    // Enable broadcast.
    udpSocket.EnableBroadcast = true;

    // Disable multicast loopback.
    udpSocket.MulticastLoopback = false;

    Console.WriteLine("Udp Socket configured:");
    Console.WriteLine($"  DontFragment {udpSocket.DontFragment}");
    Console.WriteLine($"  EnableBroadcast {udpSocket.EnableBroadcast}");
    Console.WriteLine($"  MulticastLoopback {udpSocket.MulticastLoopback}");
}

Remarks

Broadcasting is limited to a specific subnet, and must use User Datagram Protocol (UDP.) For Internet Protocol version 4, you can broadcast to your local subnet by sending a packet to 255.255.255.255; or you can use the directed broadcast address, which is the network portion of an Internet Protocol (IP) address with all bits set in the host portion. For example, if your IP address is 192.168.1.40 (a Class C address, with a netmask of 255.255.255.0 -- the network portion is the first three octets, and the host portion is the last octet), your directed broadcast address is 192.168.1.255.

Setting this property on a Transmission Control Protocol (TCP) socket will have no effect.

Applies to