select函数原型:
function select(nfds: Integer; readfds, writefds, exceptfds: PFDSet; timeout: PTimeVal): Longint;
参数说明:
nfds:在winsock中忽略
readfds: 检测是否可读的FDSet指针
writefds: 检测是否可写的FDSet指针
exceptfds: 检测是否可写的FDSet指针
timeout: 超时值。
例子:
procedure TServerClientThread.ClientExecute;
var
FDSet: TFDSet;
TimeVal: TTimeVal;
begin
while not Terminated and ClientSocket.Connected do
begin
FD_ZERO(FDSet);
FD_SET(ClientSocket.SocketHandle, FDSet);
TimeVal.tv_sec := 0;
TimeVal.tv_usec := 500;
if [red](select(0, @FDSet, nil, nil, @TimeVal) > 0)[/red] and not Terminated then
if ClientSocket.ReceiveBuf(FDSet, -1) = 0 then Break
else Synchronize(DoRead);
if (select(0, nil, @FDSet, nil, @TimeVal) > 0) and not Terminated then
Synchronize(DoWrite);
end;
end;