UdpClient 类 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供用户数据报协议 (UDP) 网络服务。
public ref class UdpClient : IDisposablepublic class UdpClient : IDisposabletype UdpClient = class
    interface IDisposablePublic Class UdpClient
Implements IDisposable- 继承
- 
				UdpClient
- 实现
示例
以下示例使用端口 11000 上的主机名www.contoso.com建立UdpClient连接。 一条小字符串消息将发送到两个单独的远程主机。 方法 Receive 会阻止执行,直到收到消息。 
              IPEndPoint使用传递给 Receive的 显示响应主机的标识。
// With this constructor the local port number is arbitrarily assigned.
UdpClient^ udpClient = gcnew UdpClient;
try
{
   udpClient->Connect( "host.contoso.com", 11000 );
   // Send message to the host to which you have connected.
   array<Byte>^sendBytes = Encoding::ASCII->GetBytes( "Is anybody there?" );
   udpClient->Send( sendBytes, sendBytes->Length );
   // Send message to a different host using optional hostname and port parameters.
   UdpClient^ udpClientB = gcnew UdpClient;
   udpClientB->Send( sendBytes, sendBytes->Length, "AlternateHostMachineName", 11000 );
   //IPEndPoint object will allow us to read datagrams sent from any source.
   IPEndPoint^ RemoteIpEndPoint = gcnew IPEndPoint( IPAddress::Any,0 );
   // Block until a message returns on this socket from a remote host.
   array<Byte>^receiveBytes = udpClient->Receive( RemoteIpEndPoint );
   String^ returnData = Encoding::ASCII->GetString( receiveBytes );
   // Use the IPEndPoint object to determine which of these two hosts responded.
   Console::WriteLine( String::Concat( "This is the message you received ", returnData->ToString() ) );
   Console::WriteLine( String::Concat( "This message was sent from ", RemoteIpEndPoint->Address->ToString(), " on their port number ", RemoteIpEndPoint->Port.ToString() ) );
   udpClient->Close();
   udpClientB->Close();
}
catch ( Exception^ e ) 
{
   Console::WriteLine( e->ToString() );
}
// This constructor arbitrarily assigns the local port number.
UdpClient udpClient = new UdpClient(11000);
    try{
         udpClient.Connect("www.contoso.com", 11000);
         // Sends a message to the host to which you have connected.
         Byte[] sendBytes = Encoding.ASCII.GetBytes("Is anybody there?");
         udpClient.Send(sendBytes, sendBytes.Length);
         // Sends a message to a different host using optional hostname and port parameters.
         UdpClient udpClientB = new UdpClient();
         udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000);
         //IPEndPoint object will allow us to read datagrams sent from any source.
         IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
         // Blocks until a message returns on this socket from a remote host.
         Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
         string returnData = Encoding.ASCII.GetString(receiveBytes);
         // Uses the IPEndPoint object to determine which of these two hosts responded.
         Console.WriteLine("This is the message you received " +
                                      returnData.ToString());
         Console.WriteLine("This message was sent from " +
                                     RemoteIpEndPoint.Address.ToString() +
                                     " on their port number " +
                                     RemoteIpEndPoint.Port.ToString());
          udpClient.Close();
          udpClientB.Close();
          }
       catch (Exception e ) {
                  Console.WriteLine(e.ToString());
        }
     ' This constructor arbitrarily assigns the local port number.
     Dim udpClient As New UdpClient(11000)
     Try
        udpClient.Connect("www.contoso.com", 11000)
        
        ' Sends a message to the host to which you have connected.
        Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there?")
        
        udpClient.Send(sendBytes, sendBytes.Length)
        
        ' Sends message to a different host using optional hostname and port parameters.
        Dim udpClientB As New UdpClient()
        udpClientB.Send(sendBytes, sendBytes.Length, "AlternateHostMachineName", 11000)
        
        ' IPEndPoint object will allow us to read datagrams sent from any source.
        Dim RemoteIpEndPoint As New IPEndPoint(IPAddress.Any, 0)
        
        ' UdpClient.Receive blocks until a message is received from a remote host.
        Dim receiveBytes As [Byte]() = udpClient.Receive(RemoteIpEndPoint)
        Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
        
        ' Which one of these two hosts responded?
        Console.WriteLine(("This is the message you received " + _
                                      returnData.ToString()))
         Console.WriteLine(("This message was sent from " + _
                                      RemoteIpEndPoint.Address.ToString() + _ 
                                      " on their port number " + _
                                      RemoteIpEndPoint.Port.ToString()))
        udpClient.Close()
        udpClientB.Close()
     Catch e As Exception
        Console.WriteLine(e.ToString())
     End Try
  End Sub
注解
类 UdpClient 提供在阻止同步模式下发送和接收无连接 UDP 数据报的简单方法。 由于 UDP 是无连接传输协议,因此无需在发送和接收数据之前建立远程主机连接。 但是,可以通过以下两种方式之一来选择建立默认远程主机:
可以使用 中 UdpClient 提供的任何发送方法将数据发送到远程设备。 Receive使用 方法从远程主机接收数据。
注意
如果已指定默认远程主机,则不要使用主机名或 IPEndPoint 调用 Send 。 如果这样做, UdpClient 将引发异常。
UdpClient 方法还允许发送和接收多播数据报。 JoinMulticastGroup使用 方法将 订阅UdpClient到多播组。 DropMulticastGroup使用 方法从多播组取消订阅 UdpClient 。
构造函数
| UdpClient() | 初始化 UdpClient 类的新实例。 | 
| UdpClient(AddressFamily) | 初始化 UdpClient 类的新实例。 | 
| UdpClient(Int32) | 初始化 UdpClient 类的新实例,并将它绑定到所提供的本地端口号。 | 
| UdpClient(Int32, AddressFamily) | 初始化 UdpClient 类的新实例,并将它绑定到所提供的本地端口号。 | 
| UdpClient(IPEndPoint) | 初始化 UdpClient 类的新实例,并将其绑定到指定的本地终结点。 | 
| UdpClient(String, Int32) | 初始化 UdpClient 类的新实例,并建立默认远程主机。 | 
属性
| Active | 获取或设置一个值,该值指示是否已建立默认远程主机。 | 
| Available | 获取从网络接收的可读取的数据量。 | 
| Client | 获取或设置基础网络 Socket。 | 
| DontFragment | 获取或设置 Boolean 值,该值指定 UdpClient 是否允许将 Internet 协议 (IP) 数据报分段。 | 
| EnableBroadcast | |
| ExclusiveAddressUse | |
| MulticastLoopback | 获取或设置 Boolean 值,该值指定是否将输出多播数据包传递给发送应用程序。 | 
| Ttl | 获取或设置一个值,它指定由 UdpClient 发送的 Internet 协议 (IP) 数据包的生存时间 (TTL)。 | 
方法
显式接口实现
| IDisposable.Dispose() | 此 API 支持产品基础结构,不能在代码中直接使用。 释放由 UdpClient 使用的所有资源。 |