Socket.Poll Method

Definition

Overloads

Poll(TimeSpan, SelectMode)

Determines the status of the Socket.

Poll(Int32, SelectMode)

Determines the status of the Socket.

Poll(TimeSpan, SelectMode)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

Determines the status of the Socket.

C#
public bool Poll(TimeSpan timeout, System.Net.Sockets.SelectMode mode);

Parameters

timeout
TimeSpan

The time to wait for a response.

mode
SelectMode

One of the SelectMode values.

Returns

The status of the Socket based on the polling mode value passed in the mode parameter. Returns true if any of the following conditions occur before the timeout expires, otherwise, false.

  • For SelectRead, it returns true if Listen() has been called and a connection is pending, if data is available for reading, or if the connection has been closed, reset, or terminated.
  • For SelectWrite, it returns true if processing a Connect and the connection has succeeded or if data can be sent.
  • For SelectError, it returns true if processing a Connect that does not block and the connection has failed, or if OutOfBandInline is not set and out-of-band data is available.
  • Otherwise, it returns false.

Exceptions

timeout is less than -1 milliseconds or greater than MaxValue milliseconds.

An error occurred when attempting to access the socket.

The Socket has been closed.

Applies to

.NET 10 and other versions
Product Versions
.NET 7, 8, 9, 10

Poll(Int32, SelectMode)

Source:
Socket.cs
Source:
Socket.cs
Source:
Socket.cs

Determines the status of the Socket.

C#
public bool Poll(int microSeconds, System.Net.Sockets.SelectMode mode);

Parameters

microSeconds
Int32

The time to wait for a response, in microseconds.

mode
SelectMode

One of the SelectMode values.

Returns

The status of the Socket based on the polling mode value passed in the mode parameter.

  • For SelectRead, it returns true if Listen() has been called and a connection is pending, if data is available for reading, or if the connection has been closed, reset, or terminated.
  • For SelectWrite, it returns true if processing a Connect and the connection has succeeded or if data can be sent.
  • For SelectError, it returns true if processing a Connect that does not block and the connection has failed, or if OutOfBandInline is not set and out-of-band data is available.
  • Otherwise, it returns false.

Exceptions

The mode parameter is not one of the SelectMode values.

An error occurred when attempting to access the socket. See remarks below.

The Socket has been closed.

Examples

The following code example creates a socket, connects to a server, and uses Poll to check the status of the socket.

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 Socket is readable." );
 }
 else if (s.Poll(-1, SelectMode.SelectError)){
      Console.WriteLine("This Socket has an error.");
 }

Remarks

The Poll method checks 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 or data becomes available. 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 the SocketException.ErrorCode property to obtain the specific error code. After you have obtained this code, refer to the Windows Sockets version 2 API error code documentation for a detailed description of the error.

Note

This method cannot detect certain kinds of connection problems, such as a broken network cable, or that the remote host was shut down ungracefully. You must attempt to send or receive data to detect these kinds of errors.

Note

This member outputs trace information when you enable network tracing in your application. For more information, see Network Tracing in .NET Framework.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1