Socket::Select Method (IList^, IList^, IList^, Int32)


Determines the status of one or more sockets.

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

static void Select(
	IList^ checkRead,
	IList^ checkWrite,
	IList^ checkError,
	int microSeconds


Type: System.Collections::IList^

An IList of Socket instances to check for readability.

Type: System.Collections::IList^

An IList of Socket instances to check for writability.

Type: System.Collections::IList^

An IList of Socket instances to check for errors.

Type: System::Int32

The time-out value, in microseconds. A -1 value indicates an infinite time-out.

Exception Condition

The checkRead parameter is null or empty.


The checkWrite parameter is null or empty


The checkError parameter is null or empty.


An error occurred when attempting to access the socket. See the Remarks section for more information.

Select is a static method that determines the status of one or more Socket instances. You must place one or more sockets into an IList before you can use the Select method. Check for readability by calling Select with the IList as the checkRead parameter. To check your sockets for writability, use the checkWrite parameter. For detecting error conditions, use checkError. After calling Select, the IList will be filled with only those sockets that satisfy the conditions.

If you are in a listening state, readability means that a call to Accept will succeed without blocking. If you have already accepted the connection, readability means that data is available for reading. In these cases, all receive operations will succeed without blocking. Readability can also indicate whether the remote Socket has shut down the connection; in that case a call to Receive will return immediately, with zero bytes returned.

Select returns when at least one of the sockets of interest (the sockets in the checkRead, checkWrite, and checkError lists) meets its specified criteria, or the microSeconds parameter is exceeded, whichever comes first. Setting microSeconds to -1 specifies an infinite time-out.

If you make a nonblocking call to Connect, writability means that you have connected successfully. If you already have a connection established, writability means that all send operations will succeed without blocking.

If you have made a non-blocking call to Connect, the checkerror parameter identifies sockets that have not connected successfully.


Use the Poll method if you only want to determine the status of a single Socket.


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.


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.

The following code example uses Select to determine which listening sockets have a connection request.

      IPHostEntry^ lipa = Dns::Resolve( Dns::GetHostName() );

      //Gets three separate local endpoints.
      IPEndPoint^ lep1 = gcnew IPEndPoint( lipa->AddressList[ 0 ],11000 );
      IPEndPoint^ lep2 = gcnew IPEndPoint( lipa->AddressList[ 0 ],11001 );
      IPEndPoint^ lep3 = gcnew IPEndPoint( lipa->AddressList[ 0 ],11002 );

      //creates an array of endpoints.
      array<IPEndPoint^>^ipendpoints = gcnew array<IPEndPoint^>(3);
      ipendpoints[ 0 ] = lep1;
      ipendpoints[ 1 ] = lep2;
      ipendpoints[ 2 ] = lep3;

      //Creates three separate sockets.
      Socket^ s1 = gcnew Socket( lep1->Address->AddressFamily,SocketType::Stream,ProtocolType::Tcp );
      Socket^ s2 = gcnew Socket( lep2->Address->AddressFamily,SocketType::Stream,ProtocolType::Tcp );
      Socket^ s3 = gcnew Socket( lep3->Address->AddressFamily,SocketType::Stream,ProtocolType::Tcp );
      array<Socket^>^socketList = gcnew array<Socket^>(3);
      socketList[ 0 ] = s1;
      socketList[ 1 ] = s2;
      socketList[ 2 ] = s3;

      //Binds and Listens on all sockets in the array of sockets.
      for ( int i = 0; i < 3; i++ )
         socketList[ i ]->Bind( ipendpoints[ i ] );
         socketList[ i ]->Listen( 1000 );


      //Calls Select to determine which sockets are ready for reading.
      Socket::Select( safe_cast<IList^>(socketList), nullptr, nullptr, 1000 );

      //Reads on the sockets returned by Select.
      array<Byte>^buffer = gcnew array<Byte>(1024);
      String^ outString;
      for ( Int32 j = 0; j < (socketList->Length - 1); j++ )
         socketList[ j ]->Receive( buffer );
         outString =  "Socket ";
         outString->Concat( j.ToString(),  " has the message", Encoding::ASCII->GetString( buffer ) );
         Console::WriteLine( outString );



int main()
   return 0;

.NET Framework
Available since 1.1
Return to top