Socket.Poll 方法

定義

多載

Poll(TimeSpan, SelectMode)

決定 Socket 的狀態。

Poll(Int32, SelectMode)

決定 Socket 的狀態。

Poll(TimeSpan, SelectMode)

來源:
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 值。

傳回

Socket 的狀態,根據從 mode 參數中傳遞的輪詢模式值而定。 true如果在 到期之前 timeout 發生下列任何條件,則傳回 , false 否則傳回 。

  • 針對 SelectRead ,如果已呼叫 ,且連接擱置中、資料可供讀取,或連接已關閉、重設或終止,則會傳回 trueListen()
  • 針對 SelectWrite ,如果處理 Connect 成功且連接成功,或可以傳送資料,則會傳回 true
  • 針對 SelectError ,如果處理 Connect 未封鎖且連線失敗的 ,或 OutOfBandInline 未設定且頻外資料可用,則會傳回 true
  • 否則會傳回 false

例外狀況

timeout 小於 -1 毫秒或大於 MaxValue 毫秒。

嘗試存取通訊端時發生錯誤。

適用於

Poll(Int32, SelectMode)

來源:
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 值。

傳回

Socket 的狀態,根據從 mode 參數中傳遞的輪詢模式值而定。

  • 針對 SelectRead ,如果已呼叫 ,且連接擱置中、資料可供讀取,或連接已關閉、重設或終止,則會傳回 trueListen()
  • 針對 SelectWrite ,如果處理 Connect 成功且連接成功,或可以傳送資料,則會傳回 true
  • 針對 SelectError ,如果處理 Connect 未封鎖且連線失敗的 ,或 OutOfBandInline 未設定且頻外資料可用,則會傳回 true
  • 否則會傳回 false

例外狀況

mode 參數不是其中一個 SelectMode 值。

嘗試存取通訊端時發生錯誤。 請參閱下面的<備註>。

範例

下列程式碼範例會建立通訊端、連接到伺服器,並使用 Poll 來檢查通訊端的狀態。

//Creates the Socket for sending data over TCP.
Socket^ s = gcnew 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.
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 中的網路追蹤

另請參閱

適用於