Export (0) Print
Expand All

Socket.Select Method

Determines the status of one or more sockets.

[Visual Basic]
Public Shared Sub Select( _
   ByVal checkRead As IList, _
   ByVal checkWrite As IList, _
   ByVal checkError As IList, _
   ByVal microSeconds As Integer _
)
[C#]
public static void Select(
 IList checkRead,
 IList checkWrite,
 IList checkError,
 int microSeconds
);
[C++]
public: static void Select(
 IList* checkRead,
 IList* checkWrite,
 IList* checkError,
 int microSeconds
);
[JScript]
public static function Select(
   checkRead : IList,
 checkWrite : IList,
 checkError : IList,
 microSeconds : int
);

Parameters

checkRead
An IList of Socket instances to check for readability.
checkWrite
An IList of Socket instances to check for writeability.
checkError
An IList of Socket instances to check for errors.
microSeconds
The time to wait for a response, in microseconds.

Exceptions

Exception Type Condition
ArgumentNullException The checkRead parameter is a null reference (Nothing in Visual Basic) or empty.

-and-

The checkWrite parameter is a null reference (Nothing) or empty

-and-

The checkError parameter is a null reference (Nothing) or empty.

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

Remarks

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 writeability, 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.

If you make a nonblocking call to Connect, writability means that you have connected successfully. If you already have a connection established, writeability 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.

Note   Use the Poll method if you only want to determine the status of a single Socket.
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, C#, C++] The following example uses Select to determine which listening sockets have a connection request.

[Visual Basic] 
Dim ipHostEntry As IPHostEntry = Dns.Resolve(Dns.GetHostName())
Dim ipAddress As IPAddress = ipHostEntry.AddressList(0)

Dim socket0 As Socket = Nothing
Dim socket1 As Socket = Nothing
Dim socket2 As Socket = Nothing
Dim socket3 As Socket = Nothing
Dim socket4 As Socket = Nothing
Dim socket5 As Socket = Nothing

Dim listenList As New ArrayList()
listenList.Add(socket0)
listenList.Add(socket1)
listenList.Add(socket2)

Dim acceptList As New ArrayList()
acceptList.Add(socket3)
acceptList.Add(socket4)
acceptList.Add(socket5)

Dim i As Integer
For i = 0 To 2
   listenList(i) = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
   CType(listenList(i), Socket).Bind(New IPEndPoint(ipAddress, 11000 + i))
   CType(listenList(i), Socket).Listen(10)
Next i

'Only the sockets that contain a connection request
'will remain in listenList after Select returns.
Socket.Select(listenList, Nothing, Nothing, 1000)

For i = 0 To listenList.Count - 1
   acceptList(i) = CType(listenList(i), Socket).Accept()
Next i

[C#] 
IPHostEntry ipHostEntry = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostEntry.AddressList[0];

Socket socket0 = null;
Socket socket1 = null; 
Socket socket2 = null; 
Socket socket3 = null; 
Socket socket4 = null; 
Socket socket5 = null; 

ArrayList listenList = new ArrayList();
listenList.Add(socket0);
listenList.Add(socket1);
listenList.Add(socket2);

ArrayList acceptList = new ArrayList();
acceptList.Add(socket3);
acceptList.Add(socket4);
acceptList.Add(socket5);

for( int i = 0; i < 3; i++ )
{
  listenList[i] = new Socket(AddressFamily.InterNetwork,
                             SocketType.Stream,
                             ProtocolType.Tcp);
  ((Socket)listenList[i]).Bind(new IPEndPoint(ipAddress, 11000 + i));
  ((Socket)listenList[i]).Listen(10);
}

// Only the sockets that contain a connection request
// will remain in listenList after Select returns.

Socket.Select(listenList, null, null, 1000);

for( int i = 0; i < listenList.Count; i++ )
{
  acceptList[i] = ((Socket)listenList[i]).Accept();
}

[C++] 

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

        //Gets three separate local endpoints.

        IPEndPoint *lep1 = new IPEndPoint(lipa->AddressList[0], 11000);
        IPEndPoint *lep2 = new IPEndPoint(lipa->AddressList[0], 11001);
        IPEndPoint *lep3 = new IPEndPoint(lipa->AddressList[0], 11002);

        //creates an array of endpoints.
        IPEndPoint *ipendpoints[] = new IPEndPoint* [3];

        ipendpoints[0] = lep1;
        ipendpoints[1] = lep2;
        ipendpoints[2] = lep3;


        //Creates three separate sockets.
        Socket *s1 = new Socket(lep1->Address->AddressFamily,
            SocketType::Stream, ProtocolType::Tcp);

        Socket *s2 = new Socket(lep2->Address->AddressFamily,
            SocketType::Stream, ProtocolType::Tcp);

        Socket *s3 = new Socket(lep3->Address->AddressFamily,
            SocketType::Stream, ProtocolType::Tcp);



        Socket *socketList[] = new 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( __try_cast<IList*>( socketList ), NULL, NULL, 1000);

        //Reads on the sockets returned by Select.
        Byte buffer[] = new Byte[1024];

        String *outString;
        for (Int32 j=0; j < (socketList->Length-1); j++){
            socketList[j]->Receive(buffer);
            outString = "Socket ";
            outString->Concat((__box(j))->ToString(), " has the message", Encoding::ASCII->GetString(buffer));
            Console::WriteLine(outString);
        }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter 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 | IList | Accept | Connect | Poll

Show:
© 2014 Microsoft