Socket.EnableBroadcast Socket.EnableBroadcast Socket.EnableBroadcast Socket.EnableBroadcast Property

定义

获取或设置一个 Boolean 值,该值指定 Socket 是否可以发送或接收广播数据包。Gets or sets a Boolean value that specifies whether the Socket can send or receive 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

属性值

如果 Socket 允许广播数据包,则为 true;否则为 falsetrue if the Socket allows broadcast packets; otherwise, false. 默认值为 falseThe default is false.

异常

此选项仅对数据报套接字有效。This option is valid for a datagram socket only.

示例

下面的代码示例演示如何将EnableBroadcast属性。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 {0}", 
                                              udpSocket.DontFragment);
	Console.WriteLine("  EnableBroadcast {0}", 
                                              udpSocket.EnableBroadcast);
	Console.WriteLine("  MulticastLoopback {0}", 
                                              udpSocket.MulticastLoopback);
}

注解

广播仅限于特定的子网,并且必须使用用户数据报协议 (UDP)。有关 Internet 协议版本 4,您可以通过将数据包发送到 255.255.255.255; 广播本地子网或者,可以使用定向广播的地址,这是使用中的主机部分设置的所有位的 Internet 协议 (IP) 地址的网络部分。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. 例如,如果你的 IP 地址为 192.168.1.40 (C 类地址,网络掩码为 255.255.255.0-网络部分是前三个八位字节,而主机部分是最后一个八进制数),则定向广播的地址是 192.168.1.255。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.

传输控制协议 (TCP) 套接字上设置此属性不会影响。Setting this property on a Transmission Control Protocol (TCP) socket will have no effect.

适用于