StreamSocketListener 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
支援使用 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
- 繼承
- 屬性
- 實作
Windows 需求
| 規格需求 | Description |
|---|---|
| 裝置系列 |
Windows 10 (已於 10.0.10240.0 引進)
|
| API contract |
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)
|
| 應用程式功能 |
bluetooth.rfcomm
internetClientServer
privateNetworkClientServer
|
備註
StreamSocketListener 類別支援透過串流套接字監聽進來的網路連線並接受該連線。
典型的操作順序如下:
- 建立 StreamSocketListener。
- 使用 Control 屬性取得 StreamSocketListenerControl 物件並設定所需的服務品質。
- 將 ConnectionReceived 事件指派給事件處理器。
- 呼叫 BindServiceNameAsync 或 BindEndpointAsync 方法來綁定到本地的 TCP 埠號或服務名稱。 對於藍牙 RFCOMM,本地服務名稱參數是藍牙服務 ID。
- 當收到連線時,請使用 StreamSocketListenerConnectionReceivedEventArgs 物件,取得已建立的 StreamSocket 物件的 Socket 屬性。
- 使用 StreamSocket 物件來傳送和接收資料。
- 呼叫 Close 方法來停止監聽和接受進來的網路連線,並釋放所有與 StreamSocketListener 物件相關的非管理資源。 任何在接收連線時建立的 StreamSocket 物件都不受影響,且可視需要繼續使用。
SocketProtectionLevel 枚舉允許伺服器在使用 StreamSocketListener 物件透過藍牙監聽並綁定套接字時,控制與用戶端的協定協商。 當 StreamSocketListener 物件透過藍牙使用時,支援的 SocketProtectionLevel 值為 PlainSocket、 BluetoothEncryptionAllowNullAuthentication 或 BluetoothEncryptionWithAuthentication。 當使用 StreamSocketListener 物件來監聽並綁定非藍牙的 socket 時,唯一支援的 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 聊天範例。
在 Windows Server 2012 上使用 StreamSocketListener
在 Windows Server 2012 和 Windows Server 2012 R2,這是實作大部分 Windows 類別的 Windows.Networking.dll。Networking.Sockets 命名空間若未啟用 Media Foundation 功能,將無法載入。 因此,使用 StreamSocketListener 及相關 socket 類別的應用程式在 Windows 中,如果關閉 Media Foundation 功能,Networking.Sockets 命名空間將會失敗。 Windows Server 2012 或 Windows Server 2012 R2 安裝時,關閉了媒體基礎功能。
媒體基礎功能可在 Windows Server 2012 或 Windows Server 2012 R2 上啟用,使用 伺服器管理員,或透過命令提示字元或腳本輸入以下文字:
dism /online /enable-feature /featurename:ServerMediaFoundation 啟用 Media Foundation 功能後,使用者會被提示重新啟動。 電腦重新啟動後,Windows 中會建立 sockets 和 WebSockets 的類別。Networking.sockets 命名空間會如預期運作。
建構函式
| 名稱 | Description |
|---|---|
| StreamSocketListener() |
建立一個新的 StreamSocketListener 物件。 |
屬性
| 名稱 | Description |
|---|---|
| Control |
取得 StreamSocketListen 物件上的套接字控制資料。 |
| Information |
取得 StreamSocketListener 物件的套接字資訊。 |
方法
事件
| 名稱 | Description |
|---|---|
| ConnectionReceived |
一個表示 StreamSocketListener 物件已接收連線的事件。 |
適用於
另請參閱
- IClosable
- SocketError
- SocketErrorStatus
- StreamSocket
- StreamSocketListenerConnectionReceivedEventArgs
- StreamSocketListenerControl
- StreamSocketListenerInformation
- 連接插座
- 處理網路應用程式中的例外
- 如何連接串流插座
- 如何連接串流插座
- 如何指定裝置的藍牙功能
- 如何使用進階插座控制
- 網路連線故障排除與除錯
- 藍牙 Rfcomm 聊天範例
- ControlChannelTrigger StreamSocket 範例
- 鄰近取樣
- StreamSocket 範例