UdpClient 类

定义

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

public ref class UdpClient : IDisposable
public class UdpClient : IDisposable
type UdpClient = class
    interface IDisposable
Public Class UdpClient
Implements IDisposable
继承
UdpClient
实现

示例

以下示例使用端口 11000 上的主机名UdpClient建立www.contoso.com连接。 将一条小字符串消息发送到两个单独的远程主机。 在收到消息之前,该方法 Receive 会阻止执行。 IPEndPoint使用传递给Receive的主机的标识会显示。

// 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使用此方法从远程主机接收数据。

注释

请勿使用主机名Send或已指定默认远程主机进行调用IPEndPoint。 如果这样做, UdpClient 将引发异常。

UdpClient 方法还允许发送和接收多播数据报。 JoinMulticastGroup使用该方法订阅UdpClient多播组。 DropMulticastGroup使用该方法取消订阅UdpClient多播组。

构造函数

名称 说明
UdpClient()

初始化 UdpClient 类的新实例。

UdpClient(AddressFamily)

初始化 UdpClient 类的新实例。

UdpClient(Int32, AddressFamily)

初始化类的新实例 UdpClient ,并将其绑定到提供的本地端口号。

UdpClient(Int32)

初始化类的新实例 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

获取或设置一个值,该值指定 Internet 协议 (IP) 数据包发送的 UdpClient生存时间 (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()

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

Dispose(Boolean)

释放由托管资源使用 UdpClient 的非托管资源,并选择性地释放托管资源。

DropMulticastGroup(IPAddress, Int32)

保留多播组。

DropMulticastGroup(IPAddress)

保留多播组。

EndReceive(IAsyncResult, IPEndPoint)

结束挂起的异步接收。

EndSend(IAsyncResult)

结束挂起的异步发送。

Equals(Object)

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

(继承自 Object)
GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

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

向多播组添加 a UdpClient

JoinMulticastGroup(IPAddress, Int32)

向具有指定生存时间(TTL)的多播组添加一个 UdpClient

JoinMulticastGroup(IPAddress, IPAddress)

向多播组添加 a UdpClient

JoinMulticastGroup(IPAddress)

向多播组添加 a UdpClient

MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
Receive(IPEndPoint)

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

ReceiveAsync()

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

ReceiveAsync(CancellationToken)

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

Send(Byte[], Int32, IPEndPoint)

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

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

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

Send(Byte[], Int32)

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

Send(ReadOnlySpan<Byte>, IPEndPoint)

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

Send(ReadOnlySpan<Byte>, String, Int32)

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

Send(ReadOnlySpan<Byte>)

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

SendAsync(Byte[], Int32, IPEndPoint)

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

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

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

SendAsync(Byte[], 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命令使用的所有资源。

适用于

另请参阅