UdpClient 类

定义

提供用户数据报协议 (UDP) 网络服务。

public ref class UdpClient : IDisposable
public class UdpClient : IDisposable
type UdpClient = class
    interface IDisposable
Public 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 实例。

  • 创建类的 UdpClient 实例,然后调用 Connect 该方法。

可以使用其中 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

获取或设置一个 Boolean 值,该值指定是否可以 UdpClient 发送广播数据包。

ExclusiveAddressUse

获取或设置 Boolean 值,指定 UdpClient 是否只允许一个客户端使用端口。

MulticastLoopback

获取或设置 Boolean 值,该值指定是否将输出多播数据包传递给发送应用程序。

Ttl

获取或设置一个值,它指定由 UdpClient 发送的 Internet 协议 (IP) 数据包的生存时间 (TTL)。

方法

AllowNatTraversal(Boolean)

启用或禁用针对 UdpClient 实例的网络地址转换 (NAT) 遍历。

BeginReceive(AsyncCallback, Object)

从远程主机异步接收数据报。

BeginSend(Byte[], Int32, AsyncCallback, Object)

将数据报异步发送到远程主机。 先前已通过调用 Connect 指定目标。

BeginSend(Byte[], Int32, IPEndPoint, AsyncCallback, Object)

将数据报异步发送到目标。 目标由 EndPoint 指定。

BeginSend(Byte[], Int32, String, Int32, AsyncCallback, Object)

将数据报异步发送到目标。 目标由主机名和端口号指定。

Close()

关闭 UDP 连接。

Connect(IPAddress, Int32)

使用指定的 IP 地址和端口号建立默认远程主机。

Connect(IPEndPoint)

使用指定的网络终结点建立默认远程主机。

Connect(String, Int32)

使用指定的主机名和端口号建立默认远程主机。

Dispose()

释放由 UdpClient 占用的托管和非托管资源。

Dispose(Boolean)

释放由 UdpClient 占用的非托管资源,还可以另外再释放托管资源。

DropMulticastGroup(IPAddress)

退出多播组。

DropMulticastGroup(IPAddress, Int32)

退出多播组。

EndReceive(IAsyncResult, IPEndPoint)

结束挂起的异步接收。

EndSend(IAsyncResult)

结束挂起的异步发送。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
Finalize()
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
JoinMulticastGroup(Int32, IPAddress)

UdpClient 添加到多播组。

JoinMulticastGroup(IPAddress)

UdpClient 添加到多播组。

JoinMulticastGroup(IPAddress, Int32)

UdpClient 添加到具有指定生存时间 (TTL) 的多播组。

JoinMulticastGroup(IPAddress, IPAddress)

UdpClient 添加到多播组。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
Receive(IPEndPoint)

返回由远程主机发送的 UDP 数据报。

ReceiveAsync()

异步返回由远程主机发送的 UDP 数据报。

ReceiveAsync(CancellationToken)

异步返回由远程主机发送的 UDP 数据报。

Send(Byte[], Int32)

将 UDP 数据报发送到远程主机。

Send(Byte[], Int32, IPEndPoint)

将 UDP 数据报发送到位于指定远程终结点的主机。

Send(Byte[], Int32, String, Int32)

将 UDP 数据报发送到指定远程主机上的指定端口。

Send(ReadOnlySpan<Byte>)

将 UDP 数据报发送到远程主机。

Send(ReadOnlySpan<Byte>, IPEndPoint)

将 UDP 数据报发送到位于指定远程终结点的主机。

Send(ReadOnlySpan<Byte>, String, Int32)

将 UDP 数据报发送到指定远程主机上的指定端口。

SendAsync(Byte[], Int32)

将 UDP 数据报异步发送到远程主机。

SendAsync(Byte[], Int32, IPEndPoint)

将 UDP 数据报异步发送到远程主机。

SendAsync(Byte[], Int32, String, Int32)

将 UDP 数据报异步发送到远程主机。

SendAsync(ReadOnlyMemory<Byte>, CancellationToken)

将 UDP 数据报异步发送到远程主机。

SendAsync(ReadOnlyMemory<Byte>, IPEndPoint, CancellationToken)

将 UDP 数据报异步发送到远程主机。

SendAsync(ReadOnlyMemory<Byte>, String, Int32, CancellationToken)

将 UDP 数据报异步发送到远程主机。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

显式接口实现

IDisposable.Dispose()

此 API 支持产品基础结构,不能在代码中直接使用。

释放由 UdpClient 使用的所有资源。

适用于

另请参阅