System.Net.Sockets.Socket.Select Method

Determines the status of one or more sockets.

Syntax

public static void Select (IList checkRead, IList checkWrite, IList checkError, int microSeconds)

Parameters

checkRead
An IList of System.Net.Sockets.Socket instances to check for readability.
checkWrite
An IList of System.Net.Sockets.Socket instances to check for writability.
checkError
An IList of System.Net.Sockets.Socket instances to check for errors.
microSeconds
The time-out value, in microseconds. A -1 value indicates an infinite time-out.

Exceptions

TypeReason
ArgumentNullExceptionAll of the following parameters are null or empty: checkRead, checkWrite, and checkError.
System.Net.Sockets.SocketException
Note: For additional information on causes of the SocketException, see the System.Net.Sockets.SocketException class.

Remarks

Socket.Select(IList, IList, IList, int) is a static method that determines the status of one or more System.Net.Sockets.Socket instances. You must place one or more sockets into an IList before you can use the Socket.Select(IList, IList, IList, int) method. Check for readability by calling Socket.Select(IList, IList, IList, int) 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 Socket.Select(IList, IList, IList, int), 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 Socket.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 System.Net.Sockets.Socket has shut down the connection; in that case a call to Socket.Receive(Byte[], int, SocketFlags) will return immediately, with zero bytes returned.

Socket.Select(IList, IList, IList, int) 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 Socket.Connect(System.Net.EndPoint), 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 Socket.Connect(System.Net.EndPoint), the checkerror parameter identifies sockets that have not connected successfully.

Note:

Use the Socket.Poll(int, SelectMode) method if you only want to determine the status of a single System.Net.Sockets.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 System.Net.Sockets.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.

Example

The following example determines the status of the socket instance named socket3 and writes the result to the console.

C# Example

using System;
using System.Collections;
using System.Net.Sockets;

class SelectTest {

  public static void Main() {

    Socket socket1 =
      new Socket(AddressFamily.InterNetwork,
                 SocketType.Stream,
                 ProtocolType.Tcp);
    Socket socket2 =
      new Socket(AddressFamily.InterNetwork,
                 SocketType.Stream,
                 ProtocolType.Tcp);
    Socket socket3 =
      new Socket(AddressFamily.InterNetwork,
                 SocketType.Stream,
                 ProtocolType.Tcp);

    ArrayList readList = new ArrayList();
    ArrayList writeList = new ArrayList();
    ArrayList errorList = new ArrayList();

    readList.Add(socket1);
    readList.Add(socket2);
    readList.Add(socket3);
    errorList.Add(socket1);
    errorList.Add(socket3);

    // readList.Contains(Socket3) returns true
    // if Socket3 is in ReadList.
    Console.WriteLine(
      "socket3 is placed in readList and errorList.");
    Console.WriteLine(
      "socket3 is {0}in readList.",
      readList.Contains(socket3) ? "" : "not " );
    Console.WriteLine(
      "socket3 is {0}in writeList.",
      writeList.Contains(socket3) ? "" : "not " );
    Console.WriteLine(
      "socket3 is {0}in errorList.",
      errorList.Contains(socket3) ? "" : "not " );

    Socket.Select(readList, writeList, errorList, 10);
    Console.WriteLine("The Select method has been called.");
    Console.WriteLine(
      "socket3 is {0}in readList.",
      readList.Contains(socket3) ? "" : "not " );
    Console.WriteLine(
      "socket3 is {0}in writeList.",
      writeList.Contains(socket3) ? "" : "not " );
    Console.WriteLine(
      "socket3 is {0}in errorList.",
      errorList.Contains(socket3) ? "" : "not " );
  }
}
   

The output is

socket3 is placed in readList and errorList.
socket3 is in readList.
socket3 is not in writeList.
socket3 is in errorList.
The Select method has been called.
socket3 is not in readList.
socket3 is not in writeList.
socket3 is not in errorList.

Requirements

Namespace: System.Net.Sockets
Assembly: System (in System.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0