Description
The select function checks whether one or more sockets are readable or writeable, and/or checks one or more sockets for errors.
A socket is readable if:
The system include file WinSock2.inc contains the following declarations for the select function, and the fd_set and timeval types:
FD_SETLAST = 63;
fd_set = packed record
fd_count : u_int; (* Number of sockets in the array *)
fd_array : array[0..FD_SETLAST] of SOCKET; (* array of sockets *)
end;
timeval = packed record
tv_sec : integer; (* seconds *)
tv_usec : integer; (* and micro-seconds (i.e. 1/1,000,000 of a second) *)
end;
function select(nfds : integer; var readfds, writefds, exceptfds : fd_set;
var tmeout : timeval) : integer;
Arguments
At least one of the second, third, or fourth arguments must not be null.
The First Argument
The first argument is provided only for compatiblility with the Berkeley Sockets API and is ignored.
The Second Argument
The second argument to the select function is of type fd_set, and is passed by reference. When you call the select function, this argument should contain the set of sockets you want to check for readability. If you don't want to check any sockets for readability then use null as the value of this argument. When the select function returns, this argument contains the set of sockets that were checked and found to be readable.
The Third Argument
The third argument to the select function is of type fd_set, and is passed by reference. When you call the select function, this argument should contain the set of sockets you want to check for writeability. If you don't want to check any sockets for writeability then use null as the value of this argument. When the select function returns, this argument contains the set of sockets that were checked and found to be writeable.
The Fourth Argument
The fourth argument to the select function is of type fd_set, and is passed by reference. When you call the select function, this argument should contain the set of sockets you want to check for errors. If you don't want to check any sockets for errors then use null as the value of this argument. When the select function returns, this argument contains the set of sockets that were checked and found to have experienced errors.
The Fifth Argument
The fifth argument to the select function is of type timeval, and although it is passed by reference it is not modified by the call. This argument specifies the maximum length of time the select function should wait for one or more of checks on the sockets to succeed. If this argument is null then the select function will wait indefinitely.
Return Values
The select function returns the total number of sockets returned in the second, third, and fourth arguments is the call is successful. If the call fails then the value SOCKET_ERROR is returned, and in this case you can use the WSAGetLastError function to retrieve a code that identifies the error that caused the call to fail. NOTE: SOCKET_ERROR is declared in the system include file WinSock2.inc.
Example
The following function uses the select function to check whether a socket is readable.
function IsSocketReadable(s : SOCKET; iMaxWaitSecs, iMaxWaitUSecs : integer) : boolean;
var
s_set : fd_set;
tv : timeval;
iRet : integer;
begin (* IsSocketReadable *)
(* Put socket in the set of sockets to be tested *)
s_set.fd_count := 1;
s_set.fd_array[0] := s;
(* Specify the maximum wait time *)
tv.tv_sec := iMaxWaitSecs;
tv.tv_usec := iMaxWaitUSecs;
(* Check whether socket is readable *)
iRet := select(0, s_set, null, null, tv);
IsSocketReadable := (iret = 1);
end; (* IsSocketReadable *)
Reference Information
The authoritative source of information about the WinSock2 library is the Microsoft Developers Network (MSDN). You can access the MSDN on the Microsoft website at msdn.microsoft.com.