Socket.Select Method

Determines the status of one or more sockets.

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

'Declaration
Public Shared Sub Select ( _
	checkRead As IList, _
	checkWrite As IList, _
	checkError As IList, _
	microSeconds As Integer _
)

Parameters

checkRead
Type: System.Collections.IList

An IList of Socket instances to check for readability.

checkWrite
Type: System.Collections.IList

An IList of Socket instances to check for writability.

checkError
Type: System.Collections.IList

An IList of Socket instances to check for errors.

microSeconds
Type: System.Int32

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

ExceptionCondition
ArgumentNullException

The checkRead parameter is Nothing or empty.

-and-

The checkWrite parameter is Nothing or empty

-and-

The checkError parameter is Nothing or empty.

SocketException

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.

NoteNote

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

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

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.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft