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 ID_CAP_NETWORKING [Windows Phone] internetClientServer privateNetworkClientServer

備註

StreamSocketListener 類別支援使用數據流套接字並接受連線來接聽連入網路連線。

作業的一般順序如下:

SocketProtectionLevel 列舉可讓伺服器在使用 StreamSocketListener 物件來接聽和系結至藍牙上的套接字時,控制與客戶端的通訊協定交涉。 當 StreamSocketListener 物件透過藍牙使用時,支援的 SocketProtectionLevel 值為 PlainSocketBluetoothEncryptionAllowNullAuthenticationBluetoothEncryptionWithAuthentication。 當 StreamSocketListener 物件用來接聽和系結至不使用藍牙的套接字時,唯一支援的 SocketProtectionLevel 值為 PlainSocket

若要搭配藍牙使用 StreamSocketListener,必須在應用程式指令清單中設定 bluetooth.rfcomm 裝置功能。 如需詳細資訊,請參閱 如何指定藍牙的裝置功能

處理例外狀況

當您在 StreamSocketListener 類別上呼叫異步方法時,您必須撰寫程式代碼來處理例外狀況。 例外狀況可能是因為參數驗證錯誤、名稱解析失敗和網路錯誤所造成。 網路錯誤 (連線中斷、連線失敗和伺服器失敗的例外狀況,例如,隨時可能會發生) 。 這些錯誤會造成擲出例外狀況。 如果應用程式未處理,例外狀況可能會導致整個應用程式由運行時間終止。

Windows.Networking.Sockets 命名空間的功能可簡化使用套接字時的處理錯誤。 SocketError 類別上的 GetStatus 方法可以將 HRESULT 從例外狀況轉換為 SocketErrorStatus 列舉值。 這對於在您的應用程式中以不同的方式處理特定網路例外狀況時很有用。 應用程式也可以使用參數驗證錯誤例外狀況中的 HRESULT ,深入瞭解造成例外狀況之錯誤的詳細資訊。

如需可能例外狀況以及如何處理例外狀況的詳細資訊,請參閱 處理網路應用程式中的例外狀況

搭配藍牙使用 StreamSocketListener

您的應用程式可以使用 StreamSocketListener 透過藍牙 RFCOMM 接聽網路連線。 透過藍牙的網路聯機會使用藍牙服務標識碼作為連線的端點,而不是IP埠或服務名稱。 若要接聽藍牙,您的應用程式會在 StreamSocketListener 上呼叫其中一個 BindServiceNameAsync 方法,並將 localServiceName 參數設定為藍牙服務標識符。

若要搭配藍牙使用 StreamSocketListener 和 StreamSocket ,必須在應用程式指令清單中設定 bluetooth.rfcomm 裝置功能。 如需詳細資訊,請參閱 Windows.Devices.Bluetooth.Rfcomm 命名空間、 如何指定藍牙的裝置功能,以及 藍牙 Rfcomm 聊天範例

在 Windows Server 2012 上使用 StreamSocketListener

在 Windows Server 2012 和 Windows Server 2012 R2 上,實作 Windows.Networking.Sockets 命名空間中大部分類別的 Windows.Networking.dll 將無法載入,除非已啟用 Media Foundation 功能。 因此,如果停用 Media Foundation 功能,使用 StreamSocketListener 和 Windows.Networking.Sockets 命名空間中相關套接字類別的應用程式將會失敗。 Windows Server 2012 或 Windows Server 2012 R2 安裝,並停用 Media Foundation 功能。

您可以使用 伺服器管理員 或在命令提示字元或文稿中輸入下列文字,在 Windows Server 2012 或 Windows Server 2012 R2 上啟用媒體基礎功能:

dism /online /enable-feature /featurename:ServerMediaFoundation 啟用 Media Foundation 功能之後,系統會提示使用者重新啟動。 重新啟動計算機之後, Windows.Networking.Sockets 命名空間中的套接字和 WebSocket 類別會如預期般運作。

建構函式

StreamSocketListener()

建立新的 StreamSocketListener 物件。

屬性

Control

取得 StreamSocketListener 物件上的套接字控件數據。

Information

取得 StreamSocketListener 物件的套接字資訊。

方法

BindEndpointAsync(HostName, String)

StreamSocketListener 上啟動系結作業至本機主機名和本機服務名稱。

BindServiceNameAsync(String)

StreamSocketListener 上啟動系結作業至本地服務名稱。

BindServiceNameAsync(String, SocketProtectionLevel)

StreamSocketListener 上啟動系結作業至具有指定 SocketProtectionLevel 的本地服務名稱,以在任何系結套接字上設定。

BindServiceNameAsync(String, SocketProtectionLevel, NetworkAdapter)

StreamSocketListener 上啟動套接字系結作業至指定網路適配器上的本地服務名稱,並指定 SocketProtectionLevel ,以在任何系結套接字上設定。

CancelIOAsync()

取消 StreamSocketListener 物件的暫止讀取和寫入。

Close()

關閉 StreamSocketListener 物件。

Dispose()

執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之應用程式定義的工作。

EnableTransferOwnership(Guid)

當此 StreamSocketListener 的流量在應用程式未使用時抵達時,套接字代理程式可觸發應用程式的背景工作。

EnableTransferOwnership(Guid, SocketActivityConnectedStandbyAction)

當此 StreamSocketListener 的流量在系統處於連線待命狀態時,讓套接字代理程式觸發應用程式的背景工作。

TransferOwnership(String)

StreamSocketListener 的擁有權轉移至套接字代理服務,以監視套接字活動,並在有任何活動時透過背景工作通知應用程式。

TransferOwnership(String, SocketActivityContext)

StreamSocketListener 的擁有權轉移至套接字代理服務,以監視套接字活動,並在有任何活動時透過背景工作通知應用程式。

事件

ConnectionReceived

事件,指出 已在 StreamSocketListener 物件上收到連接。

適用於

另請參閱