DatagramSocket Class
支持使用 UDP 数据报套接的网络通信。
语法
public ref class DatagramSocket sealed : IClosable
特性
- [DualApiPartition()]
- [MarshalingBehavior(Agile)]
- [Threading(Both)]
- [Version(0x06020000)]
成员
DatagramSocket类 具有这些类型的成员:
构造函数
DatagramSocket类 具有这些构造函数。
构造函数 | 说明 |
---|---|
DatagramSocket | Creates a new DatagramSocket object. |
事件
DatagramSocket类 具有这些事件。
事件 | 说明 |
---|---|
MessageReceived | An event that indicates that a message was received on the DatagramSocket object. |
方法
The DatagramSocket 类 具有这些方法。 对于 C#、Visual Basic 和 C++,它还继承方法于Object 类.
方法 | 说明 |
---|---|
BindEndpointAsync | Starts a bind operation on a DatagramSocket to a local hostname and a local service name. |
BindServiceNameAsync(String) | Starts a bind operation on a DatagramSocket to a local service name. |
BindServiceNameAsync(String, NetworkAdapter) | Starts a bind operation on a DatagramSocket to a local service name and specific network interface. |
Close [C++, JavaScript] | Closes the DatagramSocket object. |
ConnectAsync(EndpointPair) | Starts a connect operation on a DatagramSocket to a remote network destination specified as an EndpointPair object. |
ConnectAsync(HostName, String) | Starts a connect operation on a DatagramSocket to a remote destination specified by a remote hostname and a remote service name. |
Dispose [C#, VB] | Performs tasks associated with freeing, releasing, or resetting unmanaged resources. |
GetEndpointPairsAsync(HostName, String) | Gets a list of EndpointPair objects based on a remote hostname and remote service name that can be used to send datagrams to a remote network destination. |
GetEndpointPairsAsync(HostName, String, HostNameSortOptions) | Gets a list of EndpointPair objects based on a remote hostname and remote service name and the sort order to be used. |
GetOutputStreamAsync(EndpointPair) | Starts an operation to get an IOutputStream to a remote network destination specified by an EndpointPair object that can then be used to send network data. |
GetOutputStreamAsync(HostName, String) | Starts an operation to get an IOutputStream to a remote destination specified by a remote hostname and a remote service name that can then be used to send network data. |
JoinMulticastGroup | Joins a DatagramSocket object to a multicast group. |
属性
DatagramSocket类 具有这些属性。
属性 | 访问类型 | 说明 |
---|---|---|
只读 | Gets socket control data on a DatagramSocket object. | |
只读 | Gets socket information on the local and remote hostnames and local and remote service names for the DatagramSocket object. | |
只读 | Gets the output stream to write to the remote host. |
备注
DatagramSocket 类支持使用 UDP 数据报套接的网络通信。DatagramSocket 对象可用于发送 UDP 数据包的客户端应用程序,或用于侦听 UDP 数据的服务器应用程序。
需要执行多个步骤才能使用 DatagramSocket 对象接收数据。应用程序首先将 MessageReceived 事件分配给事件处理程序。下一个应用程序调用 BindEndpointAsync 或 BindServiceNameAsync 方法以将 DatagramSocket 绑定到本地服务名称或 UDP 端口。 下一个应用程序调用 ConnectAsync 方法。如果事件处理程序和绑定操作在连接操作之后发生,则会出现一个错误。
操作典型的顺序如下:
- 创建 DatagramSocket。
- 使用 Control 属性来检索 DatagramSocketControl 对象并设置所有高级控件。大多数应用程序通常不需要此步骤。
- 将 MessageReceived 事件分配到一个事件处理程序。
- 绑定 DatagramSocket 到本地服务名称或 UDP 端口。
- 调用 ConnectAsync 方法,以便将 DatagramSocket 绑定到远程终结点。如果应用程序需要从 DatagramSocket 对象上的任何远程终结点接收数据,则不应使用 ConnectAsync 方法,因为这会将 DatagramSocket 绑定到某个远程终结点。应改用 BindServiceNameAsync 或 BindEndpointAsync 方法。
此类还可用于参与多路广播组和发送 UDP 数据包到多路广播组。
处理异常
当你在 DatagramSocket 类上调用异步方法时,必须编写代码以处理异常。 异常可能来自参数验证错误、名称解析错误和网络错误。因网络错误(例如,断开连接、连接失败和服务器故障)导致的异常随时都可能发生。 这些错误将导致引发异常。未经您的应用程序处理的异常可能导致整个应用程序被运行时终止。
Windows.Networking.Sockets 命名空间有一个方便的帮助器方法和枚举,用于在使用套接字时处理错误。这可用于应用程序中的特定网络异常的不同处理方式。针对参数验证错误,应用程序还使用异常中的 HRESULT,以详细了解有关导致该异常的错误的信息。
有关可能的异常以及如何处理异常的更多信息,请参见处理网络应用程序中的异常。
在 Windows Server 2012 上使用 DatagramSocket
在 Windows Server 2012 和 Windows Server 2012 R2 中,Windows.Networking.dll 会无法加载(它用于实现 Windows.Networking.Sockets 命名空间中的大多数类),除非启用了媒体基础功能。这样一来,如果应用程序使用 DatagramSocket 和 Windows.Networking.Sockets 命名空间中的相关套接字类,那么当禁用 Media Foundation 功能时,该应用程序将失败。Windows Server 2012 或 Windows Server 2012 R2 安装时会禁用 Media Foundation 功能。
可以通过使用服务器管理器或在命令提示或脚本中输入以下文本在 Windows Server 2012 或 Windows Server 2012 R2 上启用 Media Foundation 功能。
dism /online /enable-feature /featurename:ServerMediaFoundation
启用 Media Foundation 功能后,系统会提示用户重新启动。一旦计算机重新启动,Windows.Networking.Sockets 命名空间中用于套接字和 WebSocket 的类将按预期工作。
要求
最低支持客户端 | Windows 8 [Windows 应用商店应用程序, 桌面应用程序] |
---|---|
最低支持服务器 | Windows Server 2012 [Windows 应用商店应用程序, 桌面应用程序] |
最低支持电话 | Windows Phone 8 |
命名空间 |
|
元数据 |
|
DLL |
|
Capabilities |
|
另请参见
- 其他资源
- 连接到网络服务(使用 JavaScript 和 HTML 的 Windows Store 应用程序)
- 连接到网络服务(使用 C#/VB/C++ 和 XAML 的 Windows Store 应用程序)
- 处理网络应用程序中的异常
- 如何使用高级套接字控件(使用 JavaScript 和 HTML 的 Windows Store 应用程序)
- 如何使用高级套接字控件(使用 C#/VB/C++ 和 XAML 的 Windows Store 应用程序)
- 快速入门:使用数据报套接字连接到网络资源(使用 JavaScript 和 HTML 的 Windows Store 应用程序)
- 疑难解答和调试网络连接
- 引用
- DatagramSocketControl
- DatagramSocketInformation
- DatagramSocketMessageReceivedEventArgs
- 如何使用高级套接字控件
- IClosable
- Object
- SetSocketMediaStreamingMode