Socket.Poll Method
Determines the status of the Socket.
[Visual Basic] Public Function Poll( _ ByVal microSeconds As Integer, _ ByVal mode As SelectMode _ ) As Boolean [C#] public bool Poll( int microSeconds, SelectMode mode ); [C++] public: bool Poll( int microSeconds, SelectMode mode ); [JScript] public function Poll( microSeconds : int, mode : SelectMode ) : Boolean;
Parameters
- microSeconds
- The time to wait for a response, in microseconds.
- mode
- One of the SelectMode values.
Return Value
| Mode | Return Value |
|---|---|
| SelectRead | true if Listen has been called and a connection is pending;
-or- true if data is available for reading; -or- true if the connection has been closed, reset, or terminated; otherwise, returns false. |
| SelectWrite | true, if processing a Connect, and the connection has succeeded;
-or- true if data can be sent; otherwise, returns false. |
| SelectError | true if processing a Connect that does not block, and the connection has failed;
-or- true if OutOfBandInline is not set and out-of-band data is available; otherwise, returns false. |
Exceptions
| Exception Type | Condition |
|---|---|
| NotSupportedException | The mode parameter is not one of the SelectMode values. |
| SocketException | An error occurred when attempting to access the socket. See remarks below. |
| ObjectDisposedException | The Socket has been closed. |
Remarks
The Poll method will check the state of the Socket. Specify SelectMode.SelectRead for the selectMode parameter to determine if the Socket is readable. Specify SelectMode.SelectWrite to determine if the Socket is writable. Use SelectMode.SelectError to detect an error condition. Poll will block execution until the specified time period, measured in microseconds, elapses. Set the microSeconds parameter to a negative integer if you would like to wait indefinitely for a response. If you want to check the status of multiple sockets, you might prefer to use the Select method.
Note If you receive a SocketException, use SocketException.ErrorCode to obtain the specific error code. Once you have obtained this code, you can refer to the Windows Socket Version 2 API error code documentation in MSDN for a detailed description of the error.
Example
[Visual Basic] '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 should not print. Because this is not a listening Socket," + " no incoming connecton requests are expected. ")) Else If s.Poll(- 1, SelectMode.SelectError) Then Console.WriteLine("This Socket has an error.") End If End If [C#] //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 should not print. Because this is not a listening Socket," + " no incoming connecton requests are expected. " ); } else if (s.Poll(-1, SelectMode.SelectError)){ Console.WriteLine("This Socket has an error."); } [C++] //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 = S"Unable to connect to host"; } // Use the SelectWrite enumeration to obtain Socket status. if (s->Poll(-1, SelectMode::SelectWrite)) { Console::WriteLine(S"This Socket is writable."); } else if (s->Poll(-1, SelectMode::SelectRead)) { Console::WriteLine(S"This should not print. Because this is not a listening Socket, " S" no incoming connecton requests are expected."); } else if (s->Poll(-1, SelectMode::SelectError)) { Console::WriteLine(S"This Socket has an error."); }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
See Also
Socket Class | Socket Members | System.Net.Sockets Namespace | SelectMode | Poll