通过


UdpClient.Client 属性

定义

获取或设置基础网络 Socket

public:
 property System::Net::Sockets::Socket ^ Client { System::Net::Sockets::Socket ^ get(); void set(System::Net::Sockets::Socket ^ value); };
protected:
 property System::Net::Sockets::Socket ^ Client { System::Net::Sockets::Socket ^ get(); void set(System::Net::Sockets::Socket ^ value); };
public System.Net.Sockets.Socket Client { get; set; }
protected System.Net.Sockets.Socket Client { get; set; }
member this.Client : System.Net.Sockets.Socket with get, set
Public Property Client As Socket
Protected Property Client As Socket

属性值

基础网络 Socket

示例

以下示例演示了属性 Client 的使用。 在此示例中,为基础 Socket启用广播。

public static void Main(string[] args)
{
    if (args.Length < 1)
    {
        Console.WriteLine("you must specify a port number!");
        return;
    }

    UdpClient uClient = new UdpClient(Convert.ToInt32(args[0]));
    Socket uSocket = uClient.Client;

    // use the underlying socket to enable broadcast.
    uSocket.SetSocketOption(SocketOptionLevel.Socket,
                  SocketOptionName.Broadcast, 1);
}
' This derived class demonstrates the use of three protected methods belonging to the UdpClient class.
Public Class MyUdpClientDerivedClass
   Inherits UdpClient
   
   Public Sub New()
   End Sub
   
   Public Sub UsingProtectedMethods()
      
      'Uses the protected Active property belonging to the UdpClient base class to determine if a connection is established.
      If Me.Active Then
         ' Calls the protected Client property belonging to the UdpClient base class.
         Dim s As Socket = Me.Client
              'Uses the Socket returned by Client to set an option that is not available using UdpClient.
         s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1)
      End If
   End Sub
End Class

注解

UdpClient Socket创建用于通过网络发送和接收数据。 派生自 UdpClient 的类可以使用此属性来获取或设置此属性 Socket。 如果需要超出该范围的访问权限,UdpClient请使用从Client中返回的基础Socket。 还可以用于Client将基础设置为现有 SocketSocket 如果要利用使用预先存在的Socket简单性UdpClient,这非常有用。

适用于

另请参阅