Socket.Select Method

Definition

Overloads

Select(IList, IList, IList, TimeSpan)

Determines the status of one or more sockets.

Select(IList, IList, IList, Int32)

Determines the status of one or more sockets.

Select(IList, IList, IList, TimeSpan)

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

Determines the status of one or more sockets.

C#
public static void Select(System.Collections.IList? checkRead, System.Collections.IList? checkWrite, System.Collections.IList? checkError, TimeSpan timeout);

Parameters

checkRead
IList

An IList of Socket instances to check for readability.

checkWrite
IList

An IList of Socket instances to check for writability.

checkError
IList

An IList of Socket instances to check for errors.

timeout
TimeSpan

The timeout value. A value equal to -1 microseconds indicates an infinite timeout.

Exceptions

The checkRead, checkWrite, and checkError parameters are all null or empty. At least one of checkRead, checkWrite, or checkError must contain at least one Socket.

The checkRead, checkWrite, or checkError parameter contains too many sockets.

The timeout was less than -1 microseconds or greater than MaxValue microseconds

An error occurred when attempting to access the socket.

One or more sockets was disposed.

Applies to

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

Select(IList, IList, IList, Int32)

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

Determines the status of one or more sockets.

C#
public static void Select(System.Collections.IList? checkRead, System.Collections.IList? checkWrite, System.Collections.IList? checkError, int microSeconds);
C#
public static void Select(System.Collections.IList checkRead, System.Collections.IList checkWrite, System.Collections.IList checkError, int microSeconds);

Parameters

checkRead
IList

An IList of Socket instances to check for readability.

checkWrite
IList

An IList of Socket instances to check for writability.

checkError
IList

An IList of Socket instances to check for errors.

microSeconds
Int32

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

Exceptions

The checkRead, checkWrite, and checkError parameters are all null or empty. At least one of checkRead, checkWrite, or checkError must contain at least one Socket.

An error occurred when attempting to access the socket.

.NET 5 and later: One or more sockets are disposed.

The checkRead, checkWrite, or checkError parameter contains too many sockets.

Examples

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

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();
}

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

Note

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

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

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.

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