Socket.Poll 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| Poll(TimeSpan, SelectMode) |
決定 的 Socket狀態。 |
| Poll(Int32, SelectMode) |
決定 的 Socket狀態。 |
Poll(TimeSpan, SelectMode)
- 來源:
- Socket.cs
- 來源:
- Socket.cs
- 來源:
- Socket.cs
- 來源:
- Socket.cs
- 來源:
- Socket.cs
決定 的 Socket狀態。
public:
bool Poll(TimeSpan timeout, System::Net::Sockets::SelectMode mode);
public bool Poll(TimeSpan timeout, System.Net.Sockets.SelectMode mode);
member this.Poll : TimeSpan * System.Net.Sockets.SelectMode -> bool
Public Function Poll (timeout As TimeSpan, mode As SelectMode) As Boolean
參數
- timeout
- TimeSpan
等待回應的時間。
- mode
- SelectMode
這是其中一項 SelectMode 價值。
傳回
根據輪詢模式值,參數中傳遞mode了 的Socket狀態。 若在 到期timeout前發生以下任一條件,則返回true;否則,。 false
- 對於 SelectRead,若Listen()已呼叫且連線待處理,資料可用,或連線已被關閉、重置或終止,則回傳
true。 - 對於 SelectWrite,若處理 a Connect 且連線成功,或是否能傳送資料,則回傳
true。 - 對於 SelectError,若處理的 a Connect 未阻塞且連線失敗,或OutOfBandInline未設定且有帶外資料可用,則會回傳
true。 - 否則會傳回
false。
例外狀況
timeout 小於 -1 毫秒或大於 MaxValue 毫秒。
嘗試存取該套接字時發生錯誤。
該店 Socket 已經關閉。
適用於
Poll(Int32, SelectMode)
- 來源:
- Socket.cs
- 來源:
- Socket.cs
- 來源:
- Socket.cs
- 來源:
- Socket.cs
- 來源:
- Socket.cs
決定 的 Socket狀態。
public:
bool Poll(int microSeconds, System::Net::Sockets::SelectMode mode);
public bool Poll(int microSeconds, System.Net.Sockets.SelectMode mode);
member this.Poll : int * System.Net.Sockets.SelectMode -> bool
Public Function Poll (microSeconds As Integer, mode As SelectMode) As Boolean
參數
- microSeconds
- Int32
等待回應的時間,以微秒計。
- mode
- SelectMode
這是其中一項 SelectMode 價值。
傳回
根據輪詢模式值,參數中傳遞mode了 的Socket狀態。
- 對於 SelectRead,若Listen()已呼叫且連線待處理,資料可用,或連線已被關閉、重置或終止,則回傳
true。 - 對於 SelectWrite,若處理 a Connect 且連線成功,或是否能傳送資料,則回傳
true。 - 對於 SelectError,若處理的 a Connect 未阻塞且連線失敗,或OutOfBandInline未設定且有帶外資料可用,則會回傳
true。 - 否則會傳回
false。
例外狀況
參數 mode 不是其中 SelectMode 的值。
嘗試存取該套接字時發生錯誤。 請見下方的備註。
該店 Socket 已經關閉。
範例
以下程式碼範例建立一個套接字,連接伺服器,並用來 Poll 檢查套接字的狀態。
//Creates the Socket for sending data over TCP.
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp );
// Connects to host using IPEndPoint.
s.Connect(EPhost);
if (!s.Connected)
{
strRetPage = "Unable to connect to host";
}
// Use the SelectWrite enumeration to obtain Socket status.
if(s.Poll(-1, SelectMode.SelectWrite)){
Console.WriteLine("This Socket is writable.");
}
else if (s.Poll(-1, SelectMode.SelectRead)){
Console.WriteLine("This Socket is readable." );
}
else if (s.Poll(-1, SelectMode.SelectError)){
Console.WriteLine("This Socket has an error.");
}
'Creates the Socket for sending data over TCP.
Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
' Connects to host using IPEndPoint.
s.Connect(EPhost)
If Not s.Connected Then
strRetPage = "Unable to connect to host"
End If
' Use the SelectWrite enumeration to obtain Socket status.
If s.Poll(- 1, SelectMode.SelectWrite) Then
Console.WriteLine("This Socket is writable.")
Else
If s.Poll(- 1, SelectMode.SelectRead) Then
Console.WriteLine(("This Socket is readable. "))
Else
If s.Poll(- 1, SelectMode.SelectError) Then
Console.WriteLine("This Socket has an error.")
End If
End If
End If
備註
該 Poll 方法檢查 的 Socket狀態。 指定SelectMode.SelectReadselectMode參數來判斷 是否Socket可讀。 指定 SelectMode.SelectWrite 以判斷 是否 Socket 可寫。 用 SelectMode.SelectError 來偵測錯誤狀況。
Poll 將阻塞執行,直到指定時間(以 microseconds為單位)經過或資料可用。 如果你想無限期等待回應,可以將參數設 microSeconds 為負整數。 如果你想檢查多個插槽的狀態,可能會比較喜歡用這個 Select 方法。
備註
如果你收到 SocketException,請使用該 SocketException.ErrorCode 屬性取得特定的錯誤代碼。 取得此程式碼後,請參閱 Windows Sockets 版本 2 API 錯誤代碼 文件,以獲得錯誤的詳細說明。
備註
此方法無法偵測某些連線問題,例如網路線斷裂,或遠端主機被不當關機。 你必須嘗試傳送或接收資料以偵測這類錯誤。
備註
當您在應用程式中啟用網路追蹤時,此成員會輸出追蹤資訊。 欲了解更多資訊,請參閱 .NET Framework 中的網路追蹤。