Solved

TServerSocket's OnClientDisconnect ?

Posted on 2003-10-23
4
365 Views
Last Modified: 2010-04-05
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
Comment
Question by:shaneholmes
  • 2
  • 2
4 Comments
 
LVL 8

Expert Comment

by:gmayo
ID: 9612740
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
 
LVL 8

Accepted Solution

by:
gmayo earned 150 total points
ID: 9612753
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
 
LVL 11

Author Comment

by:shaneholmes
ID: 9615305
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
 
LVL 11

Author Comment

by:shaneholmes
ID: 9616003
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi - replicating a form 8 76
Wincontrol not (correctly) drawn 15 38
DBCtrlGrid, Delphi, Scroll 7 15
Firemonkey BASS_Init into a thread 17 9
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question