Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Socket.Poll Method

Determines the status of the Socket.

Namespace: System.Net.Sockets
Assembly: System (in system.dll)

public bool Poll (
	int microSeconds,
	SelectMode mode
)
public boolean Poll (
	int microSeconds, 
	SelectMode mode
)
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.

Exception typeCondition

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.

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.

NoteNote

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 in the MSDN library for a detailed description of the error.

NoteNote

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.

NoteNote

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

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

//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.
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, 
    ProtocolType.Tcp);
// Connects to host using IPEndPoint.
s.Connect(epHost);
if (!(s.get_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.");
        }
    }
}

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft