UdpClient 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供用户数据报协议(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 提供的任何发送方法将数据发送到远程设备。 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 | |
| ExclusiveAddressUse | |
| MulticastLoopback |
获取或设置一个 Boolean 值,该值指定传出多播数据包是否传递到发送应用程序。 |
| Ttl |
获取或设置一个值,该值指定 Internet 协议 (IP) 数据包发送的 UdpClient生存时间 (TTL) 值。 |
方法
显式接口实现
| 名称 | 说明 |
|---|---|
| IDisposable.Dispose() |
此 API 支持产品基础结构,不能在代码中直接使用。 释放该 UdpClient命令使用的所有资源。 |