UdpClient.Client 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置基础网络 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启用广播。
// This derived class demonstrate the use of three protected methods belonging to the UdpClient class.
public ref class MyUdpClientDerivedClass: public UdpClient
{
public:
MyUdpClientDerivedClass()
: UdpClient()
{}
void UsingProtectedMethods()
{
//Uses the protected Active property belonging to the UdpClient base class to determine if a connection is established.
if ( this->Active )
{
//Calls the protected Client property belonging to the UdpClient base class.
Socket^ s = this->Client;
//Uses the Socket returned by Client to set an option that is not available using UdpClient.
s->SetSocketOption( SocketOptionLevel::Socket, SocketOptionName::Broadcast, 1 );
}
}
};
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 将基础 Socket 设置为现有的 Socket。 如果要利用使用预先存在的 Socket的简单性UdpClient,这非常有用。