Newsgroups : Borland : borland.public.delphi.internet.winsock : 2008 Mar : Re: How to limit number of concurrent clients in TServerSocket?

www.cryer.info
Managed Newsgroup Archive

Re: How to limit number of concurrent clients in TServerSocket?

Subject:Re: How to limit number of concurrent clients in TServerSocket?
Posted by:"Remy Lebeau (TeamB)" (no.spam@no.spam.com)
Date:Wed, 26 Mar 2008 12:54:11

"Bo Berglund" <bo.berglund@telia.com> wrote in message
news:tt7lu35i601fjqmimenlicduie8fouovk8@4ax.com...

> 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?

The server still has to close its endpoints of the connections as well.
When a client disconnects normally, TServerSocket should be doing that
automatically for you.

> The server is non-blocking.

Do you have an active message loop running in the thread that is using the
TServerSocket?  When a client is disconnected, whether by the client or the
server, the TCustomWinSocket object is not freed right away.  It posts a
CM_DEFERFREE message to a hidden window that TServerSocket has.  If you are
not allowing new messages to be processed, that might account for why you
are seeing the ActiveConnections value increase unexpectedly.

> What I do on connect is this (trivial stuff removed):

You are not using the code I gave you earlier.  You need to Close() the
TCustomWinSocket when you detect that a previous connection already exists.
All you are doing is logging it but then not closing the new connection at
all.  Try this instead:

    procedure TSSRemoteServer.sckServerClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
    var
        SSRC: TSSRemoteClientComm;
        i: integer;
    begin
        i := sckServer.Socket.ActiveConnections;
        if i > 1 then
        begin
            LogServer.StdLog('Connection attempted with client already
connected.'
            Socket.Close; // <-- ADD THIS!!!!
        end else
        begin
            try
                SSRC := TSSRemoteClientComm.Create(Socket, FRemoteServer);
                try
                    SSRC.OnDisconnect := FRemoteServer.OnClientDisconnect;
                    FRemoteServer.ClientCallback := SSRC.ClientCallback;
                    SSRC.Initialize;
                except
                    SSRC.Free;
                    raise;
                end;
            except
                on E: Exception do
                    LogServer.ErrLog('Exception during client connect: ' +
E.Message);
            end;
        end;
    end;


Gambit

Replies:

In response to:

www.cryer.info
Managed Newsgroup Archive