This topic has not yet been rated - Rate this topic

Socket.Select Method

Determines the status of one or more sockets.

Namespace:  System.Net.Sockets
Assembly:  System (in System.dll)
public static void Select(
	IList checkRead,
	IList checkWrite,
	IList checkError,
	int microSeconds
)

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.
Exception Condition
ArgumentNullException

The checkRead parameter is null or empty.

-and-

The checkWrite parameter is null or empty

-and-

The checkError parameter is null 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.

Note Note

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

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


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Yet Another Mindless Help Document
Ah, ya see, a function like select with no real world explanation. This would be excellent if we created sockets in a group of array items to choose from, but in a real world scenario, this is utterly unacceptable. I mean, is there some type of enumeration or collection of sockets within a certain class or project? Who knows!, we certainly couldn't ask the help to explain that one. What are we up to now? framework 5??? 


So in short, another useless help document! WTF MS!, we're not building school projects here we're trying to build solutions for business, servers, etc. 


For those who are trying to see if a socket is connected try these and see if it returns true to get a value that might get ya into the ballpark(S= a bound socket).



                Dim test1 As Boolean = S.Poll(10, System.Net.Sockets.SelectMode.SelectRead)
                Dim test2 As Boolean = S.Poll(10, System.Net.Sockets.SelectMode.SelectError)


Thanks again for nothing MS!
This function does not have meaning, I think
public static void Select( IList checkRead, IList checkWrite, IList checkError, int microSeconds ) $0For example, Socket.Select(readList,null,null,-1); $0 $0After calling above code, i need a list of socket which is readable. But above function does not return list of readable socket. If I have to use the Poll function for checking again, it is duplicated. So, I think it does not have meaning.  $0 $0It is better if the Select function return list of readable sockets, writable sockets, error socket. I mean the prototype of its is something likes: $0 $0publicstaticvoid Select( IList& checkRead, IList& checkWrite, IList& checkError, int microSeconds )  $0 $0How to use:  ArrayList writelist = null; ArrayList errorlist = null; ArrayList readlist = new ArrayList(); readlist .Add(listensocket); readlist .Add(clientsocket1); readlist .Add(clientsocket2); readlist .Add(clientsocket3); Socket.Select(readlist,writelist,errorlist);  if(readlist.Find(listensocket)) { //New connection come listensocket.Accept(); } if(readlist.Find(clientsocket1)) { //New data come clientsocket1.Receive(); }$0
Re: -1
At least on .NET 4.0 a timeout value of -1 does indeed cause the call to block.
-1
Passing -1 does not make it to wait infinitely -- quite the opposite, it returns immediately, no matter what. If looped, leads to an insane endless loop with high kernel mode times.

My workaround: simulate infinity with int.MaxValue.