语言

StreamSocketListener 类

定义

支持使用 TCP 流套接字或蓝牙 RFCOMM 侦听传入网络连接。

public ref class StreamSocketListener sealed : IClosable
/// [Windows.Foundation.Metadata.Activatable(65536, Windows.Foundation.UniversalApiContract)]
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class StreamSocketListener final : IClosable
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
/// [Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
class StreamSocketListener final : IClosable
[Windows.Foundation.Metadata.Activatable(65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class StreamSocketListener : System.IDisposable
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.Foundation.Metadata.Activatable(65536, "Windows.Foundation.UniversalApiContract")]
public sealed class StreamSocketListener : System.IDisposable
function StreamSocketListener()
Public NotInheritable Class StreamSocketListener
Implements IDisposable
继承
Object Platform::Object IInspectable StreamSocketListener
属性
实现

Windows 要求

要求 说明
设备系列
Windows 10 (在 10.0.10240.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)
应用功能
bluetooth.rfcomm internetClientServer privateNetworkClientServer

注解

StreamSocketListener 类支持使用流套接字和接受连接侦听传入网络连接。

操作的典型顺序如下所示:

SocketProtectionLevel 枚举允许服务器在使用 StreamSocketListener 对象通过蓝牙侦听和绑定到套接字时控制与客户端的协议协商。 当 StreamSocketListener 对象通过蓝牙使用时,支持的 SocketProtectionLevel 值为 PlainSocket、 BluetoothEncryptionAllowNullAuthentication 或 BluetoothEncryptionWithAuthentication。 当 StreamSocketListener 对象用于侦听和绑定到不使用蓝牙的套接字时,唯一支持的 SocketProtectionLevel 值为 PlainSocket。

若要将 StreamSocketListener 与蓝牙配合使用,必须在应用清单中设置 bluetooth.rfcomm 设备功能。 有关详细信息,请参阅 如何为蓝牙指定设备功能。

处理异常

在 StreamSocketListener 类上调用异步方法时,必须编写代码来处理异常。 异常可能是由参数验证错误、名称解析失败和网络错误导致的。 网络错误(例如连接丢失、连接失败和服务器故障)可能会随时发生异常。 这些错误会导致引发异常。 如果应用未处理,异常可能会导致整个应用被运行时终止。

Windows。Networking.Sockets 命名空间具有简化使用套接字时处理错误的功能。 SocketError 类上的 GetStatus 方法可以将 HRESULT 从异常转换为 SocketErrorStatus 枚举值。 这可用于在应用中以不同的方式处理特定网络异常。 应用还可以使用来自参数验证错误的异常中的 HRESULT 来了解有关导致异常的错误的更多详细信息。

有关可能的异常以及如何处理异常的详细信息,请参阅 在网络应用中处理异常。

将 StreamSocketListener 与蓝牙配合使用

应用可以使用 StreamSocketListener 通过蓝牙 RFCOMM 侦听网络连接。 通过蓝牙建立的网络连接使用蓝牙服务 ID 作为连接的终结点,而不是 IP 端口或服务名称。 若要侦听蓝牙,应用将在 StreamSocketListener 上调用 BindServiceNameAsync 方法之一,并将 localServiceName 参数设置为蓝牙服务 ID。

若要将 StreamSocketListener 和 StreamSocket 与蓝牙配合使用,必须在应用清单中设置 bluetooth.rfcomm 设备功能。 有关详细信息,请参阅Windows。Devices.Bluetooth.Rfcomm 命名空间、如何为蓝牙指定设备功能以及蓝牙 Rfcomm Chat 示例。

在 Windows Server 2012 上使用 StreamSocketListener

在 Windows Server 2012 和 Windows Server 2012 R2 上,实现Windows中大多数类的 Windows.Networking.dll。除非启用了 Media Foundation 功能,否则 Networking.Sockets 命名空间将无法加载。 因此,在Windows中使用 StreamSocketListener 和相关套接字类的应用。如果禁用媒体基础功能,Networking.Sockets 命名空间将失败。 禁用媒体基础功能Windows Server 2012或Windows Server 2012 R2 安装。

可以使用服务器管理器或在命令提示符或脚本中输入以下文本,在 Windows Server 2012 或 Windows Server 2012 R2 上启用媒体基础功能:

dism /online /enable-feature /featurename:ServerMediaFoundation 启用媒体基础功能后,系统会提示用户重启。 重启计算机后,Windows中套接字和 WebSocket 的类。Networking.Sockets 命名空间将按预期工作。

构造函数

名称 说明
StreamSocketListener()

创建新的 StreamSocketListener 对象。

属性

名称 说明
Control

获取 StreamSocketListener 对象上的套接字控件数据。

Information

获取 StreamSocketListener 对象的套接字信息。

方法

名称 说明
BindEndpointAsync(HostName, String)

对 StreamSocketListener 启动绑定到本地主机名和本地服务名称的绑定操作。

BindServiceNameAsync(String, SocketProtectionLevel, NetworkAdapter)

在 StreamSocketListener 上启动套接字绑定操作,以使用指定的 SocketProtectionLevel 在指定的网络适配器上将本地服务名称设置为任何绑定套接字。

BindServiceNameAsync(String, SocketProtectionLevel)

在 StreamSocketListener 上启动绑定操作,以使用指定的 SocketProtectionLevel 将本地服务名称设置为任何绑定套接字。

BindServiceNameAsync(String)

在 StreamSocketListener 上启动绑定到本地服务名称的绑定操作。

CancelIOAsync()

取消 StreamSocketListener 对象上挂起的读取和写入。

Close()

关闭 StreamSocketListener 对象。

Dispose()

执行与释放、释放或重置非托管资源关联的应用程序定义任务。

EnableTransferOwnership(Guid, SocketActivityConnectedStandbyAction)

当此 StreamSocketListener 的流量到达系统处于连接待机状态时,使应用的后台任务能够由套接字代理触发。

EnableTransferOwnership(Guid)

当此 StreamSocketListener 的流量在应用未处于活动状态时到达时,允许套接字代理触发应用的后台任务。

TransferOwnership(String, SocketActivityContext)

将 StreamSocketListener 的所有权转移到套接字代理服务,该服务监视套接字活动,并在有任何活动时通过后台任务通知应用。

TransferOwnership(String)

将 StreamSocketListener 的所有权转移到套接字代理服务,该服务监视套接字活动,并在有任何活动时通过后台任务通知应用。

事件

名称 说明
ConnectionReceived

指示在 StreamSocketListener 对象上收到连接的事件。

适用于

另请参阅