Board index » delphi » Closing Server Client Connections without Closing Server Socket.

Closing Server Client Connections without Closing Server Socket.

I'm trying to close all active client connections to a Server Socket (not
indy, Delphi Sockets).
I use the following procedure which closes the clients but the server stills
shows them
as acctive connections i.e ServerSocket.ActiveConnections does not change.

procedure TCommServer.CloseAllConnections;
var
x, sc: integer;
begin
sc := FServerSocket.Socket.ActiveConnections-1;
if FServerSocket.Active and (sc > 0) then
for x := 0 to sc do begin
FServerSocket.Socket.Connections[x].Close;
end;
FActiveClients := 0;
end;

The procedure disconnects the Clients but does not reflect this in the
FServerSocket.Socket.ActiveConnections property. Is this down to bad
borland socket code ??? any help.

Thanks, Carlos

 

Re:Closing Server Client Connections without Closing Server Socket.


Quote
On Tue, 18 Feb 2003 19:28:55 -0000, "carlos" <nore...@here.com> wrote:
>The procedure disconnects the Clients but does not reflect this in the
>FServerSocket.Socket.ActiveConnections property. Is this down to bad
>borland socket code ??? any help.

I assume you are using the server in non-blocking mode?
Give them some time to close properly.
A graceful disconnect requires some data exchange, and in non-blocking
mode, this is done in the background, i.e. calling close starts the
process, but when close returns it is still going on.
Check the OnClientDisconnect event that should get fired eventually...

Andy

Re:Closing Server Client Connections without Closing Server Socket.


Yes, I do check the OnClientDisconnect Events and they are being called at
both ends. The Sockets are getting disconnected but the ActiveConnections
property is not changing. I am now assuming it is a buggy scktcmp unit
as I followed the Socket.Close procedure and at the end it just removes
the socket from a TList property where the ActiveConnections property
is also getting it's value.

From Delphi docs it states that a TList moves items down in the list
after an item is removed. I am going to investigate the Server.Close
procedure because that closes all connections and updates the
ActiveConnections
property fine. Also closing just a single Client Connection works fine too.
It is only when trying to close all socket connections in a loop, only the
first
closed connection in the loop is updating the ActiveConnections property.

If it is true that the TList moves items down when they are removed, then
when
I loop through connections to close them using a loop index from 0 to
ActiveConnections-1
then the numbers here must be causing the problem. Maybe I should just be
using
Connections[0].close and not Connections[0..1..2..etc].close in my loop ????

Help !!!

Quote
"Andy M." <[no-spam]andy.mail...@gmx.net> wrote in message

news:jh955vok0krv82rssr1hjghv109987tp0d@4ax.com...
Quote
> On Tue, 18 Feb 2003 19:28:55 -0000, "carlos" <nore...@here.com> wrote:
> >The procedure disconnects the Clients but does not reflect this in the
> >FServerSocket.Socket.ActiveConnections property. Is this down to bad
> >borland socket code ??? any help.

> I assume you are using the server in non-blocking mode?
> Give them some time to close properly.
> A graceful disconnect requires some data exchange, and in non-blocking
> mode, this is done in the background, i.e. calling close starts the
> process, but when close returns it is still going on.
> Check the OnClientDisconnect event that should get fired eventually...

> Andy

Re:Closing Server Client Connections without Closing Server Socket.


Quote
On Wed, 19 Feb 2003 15:08:49 -0000, "carlos" <nore...@here.com> wrote:
>If it is true that the TList moves items down when they are removed, then
>when
>I loop through connections to close them using a loop index from 0 to
>ActiveConnections-1
>then the numbers here must be causing the problem. Maybe I should just be
>using
>Connections[0].close and not Connections[0..1..2..etc].close in my loop ????

Yes, this is true.
Looking at your original code, you should get problems anyway.
I didn't look very closely the first time, but now I noticed the
for-loop.
You should use a while loop in this case, or simply do as you
suggested and always close the [0] connection.
Because, if things would have worked out as you expected, you would
have gotten an AV, since the loop var will be out of the bounds of
.Connections somewherer during the loop.

Try.

While Server.Socket.ActiveConnections > 0 do
 Server.Socket.Connecions[0].Close;

instead.
Maybe this was your problem after all.
I don't believe that scktcomp has a bug here.
There are bugs in scktcomp, but this particular code worked fine for
me.

Andy

Re:Closing Server Client Connections without Closing Server Socket.


Thanks Andy, but the while loop just freezes the application after
closing the first connection[0].

I've now concluded that it is not possible to close all clients in a
for or while loop without introducing some form of delay after closing
each connection.

Thanks for help but I'm going to have use an alternate method of
closing all Client Connections.

Quote
"Andy M." <[no-spam]andy.mail...@gmx.net> wrote in message

news:n9a75v8d2dg725219n0t64nuio2fot4qq1@4ax.com...
Quote
> On Wed, 19 Feb 2003 15:08:49 -0000, "carlos" <nore...@here.com> wrote:
> >If it is true that the TList moves items down when they are removed, then
> >when
> >I loop through connections to close them using a loop index from 0 to
> >ActiveConnections-1
> >then the numbers here must be causing the problem. Maybe I should just be
> >using
> >Connections[0].close and not Connections[0..1..2..etc].close in my loop
????

> Yes, this is true.
> Looking at your original code, you should get problems anyway.
> I didn't look very closely the first time, but now I noticed the
> for-loop.
> You should use a while loop in this case, or simply do as you
> suggested and always close the [0] connection.
> Because, if things would have worked out as you expected, you would
> have gotten an AV, since the loop var will be out of the bounds of
> .Connections somewherer during the loop.

> Try.

> While Server.Socket.ActiveConnections > 0 do
>  Server.Socket.Connecions[0].Close;

> instead.
> Maybe this was your problem after all.
> I don't believe that scktcomp has a bug here.
> There are bugs in scktcomp, but this particular code worked fine for
> me.

> Andy

Other Threads