Solved

TServerSocket's OnClientDisconnect ?

Posted on 2003-10-23
4
376 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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