Newsgroups : Borland : borland.public.delphi.internet.winsock : 2008 Mar : Re: How to limit number of concurrent clients in TServerSocket?
| Subject: | Re: How to limit number of concurrent clients in TServerSocket? |
| Posted by: | "Bo Berglund" (bo.berglu..@telia.com) |
| Date: | Wed, 26 Mar 2008 20:22:55 |
On Tue, 25 Mar 2008 20:07:23 -0700, "Remy Lebeau \(TeamB\)"
<no.spam@no.spam.com> wrote:
>
>"Bo Berglund" <bo.berglund@telia.com> wrote in message
>news:6r0ju39bjjpvtkk8i8i9tbibjphfg7sh8v@4ax.com...
>
>> What I see is that even though I disconnect a client then when I
>> again connect the ActiveConnections property increments
>> monotonically 1-2-3-4-5 etc. I cannot see where it decrements.
>
>Then you are not disconnecting them correctly. TServerSocket can only
>accept 1 client at a time, so for the count to keep increasing like that,
>the previous connections are haanging around.
I don't see what you mean by disconnecting. The disconnect is done by
the client, not by the server, so what should the server do about it?
>
>Are you using the server in blocking or non-blocking mode?
>
The server is non-blocking.
What I do on connect is this (trivial stuff removed):
procedure TSSRemoteServer.sckServerClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
{Create the remote handler and assign the communication to it}
var
SSRC: TSSRemoteClientComm;
i: integer;
begin
try
i := sckServer.Socket.ActiveConnections;
if i > 1 then
LogServer.StdLog('Connection attempted with client already
connected. Active clients now = ' + IntToStr(i)); //< keeps increasing
SSRC := TSSRemoteClientComm.Create(Socket, FRemoteServer);
SSRC.OnDisconnect := FRemoteServer.OnClientDisconnect;
FRemoteServer.ClientCallback := SSRC.ClientCallback;
SSRC.Initialize;
except
on E: Exception do
begin
LogServer.ErrLog('Exception during client connect: ' +
E.Message);
end;
end;
end;
All communication for this client is now handled by the SSRC object,
which has received the Socket pointer.
Inside the object create I assigne the Socket.OnSocketEvent and
Socket.OnErrorEvent to internal handlers. Inside the OnSocketEvent I
decode the read event and here is where I receive the client commands.
I also detect the disconnect in this handler.
If the handler sees the disconnect I do this in the case structure:
seDisconnect:
begin
LogStd('Client is disconnecting: ' + Socket.RemoteAddress);
if not FRemoteServer.TaskRunning then
FRemoteServer.StopInstrument;
if Assigned(FOnDisconnect) then
begin
FOnDisconnect(Self, Socket.RemoteAddress);
end;
Self.Free;
end;
/BoB