Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

TServerSocket's OnClientDisconnect ?

Why is it that I can't seem to get the OnClientDisconnect of the TServerSocket to report the coorect ActiveConnections?

I am simply creating a ListItem in my ListView for each connection on the OnClientConnect and then removing them in the OnClientDisconnect event - by using a UpdateClients method - see below, however, it doesn't seem to be working for the OnClientDisconnect event. - Any remedy?


procedure TfrmMain.sckMainClientConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
 pnlClients.Caption:= IntToStr(sckMain.Socket.ActiveConnections)+' Clients';
 UpdateClients;
end;

procedure TfrmMain.sckMainClientDisconnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
 pnlClients.Caption:= IntToStr(sckMain.Socket.ActiveConnections-1)+' Clients';
 UpdateClients;
end;

procedure TfrmMain.UpdateClients;
var
 Item: TListItem;
 I: Integer;
begin
 lvwClients.Items.BeginUpdate;
 lvwClients.Clear;
 for I:= 0 to sckMain.Socket.ActiveConnections-1 do
 begin
  Item:= lvwClients.Items.Add;
  Item.Caption:= sckMain.Socket.Connections[I].RemoteHost;
 end;
 lvwClients.Items.EndUpdate;
end;
0
shaneholmes
Asked:
shaneholmes
  • 2
  • 2
1 Solution
 
gmayoCommented:
Perhaps the OnClientDisconnect is called *before* it is actually removed from the ActiveConnections list? I certainly don't have trouble keeping a list of clients, but I add and remove them from my own list, added when ClientConnect and removed on ClientDisconnect.

Geoff M.
0
 
gmayoCommented:
You could set a flag in your Connect/Disconnect handlers and have a timer running. If the timer triggers and the flag is set, then update the clients list.

procedure TfrmMain.OnTimerTimer(Sender : TObject);
begin
   if UpdateRequired then UpdateClients;
   UpdateRequired := false;
end;

procedure TfrmMain.sckMainClientConnect(Sender: TObject;
 Socket: TCustomWinSocket);
begin
   UpdateRequired := true;
end;

procedure TfrmMain.sckMainClientDisconnect(Sender: TObject;
 Socket: TCustomWinSocket);
begin
   UpdateRequired := true;
end;

procedure TfrmMain.UpdateClients;
var
Item: TListItem;
I: Integer;
begin
lvwClients.Items.BeginUpdate;
lvwClients.Clear;
for I:= 0 to sckMain.Socket.ActiveConnections-1 do
begin
 Item:= lvwClients.Items.Add;
 Item.Caption:= sckMain.Socket.Connections[I].RemoteHost;
end;
lvwClients.Items.EndUpdate;
end;

Geoff M.
0
 
shaneholmesAuthor Commented:
K, let me give that a try....btw, i left you a comment on my last post, regarding the Disabling Ctrl-Alt-Del in XP.
Would you please go back and view this and help me out with it.
0
 
shaneholmesAuthor Commented:
Sorry about my last comment, it was not you who i left a comment for.

BTW, thanks for the perfect answer to my post!

Shane
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now