determine total clients

Hello:

I am using following simple code in OnConnect() and OnDisconnect() of Indy 
TCP Server to determine number of clients.

int TotalClients;
TList *ClientsList;
ClientsList = IdTCPServer1->Contexts->LockList();
try {
TotalClients = ClientsList->Count;
}
__finally {
IdTCPServer1->Contexts->UnlockList();
}

TotalClients increments ok when I connect multiple clients but when all the 
clients are disconnected, the TotalClients number remains at 1 instead of 
going to 0.

What is going wrong here?

Thanks,
Sudesh
0
Sudesh
8/17/2015 6:40:26 PM
embarcadero.cppbuilder.socket 566 articles. 0 followers. Follow

2 Replies
625 Views

Similar Articles

[PageSpeed] 4

Sudesh wrote:

> TotalClients increments ok when I connect multiple clients but when
> all the clients are disconnected, the TotalClients number remains at 1
> instead of going to 0.

In the OnDisconnect event, the client that is triggering the event has not 
been removed from the list yet.  You will have to account for that, eg:

{code}
int __fastcall TMyForm::GetClientCount()
{
    int Clients;
    TList *ClientsList = IdTCPServer1->Contexts->LockList();
    try {
        Clients = ClientsList->Count;
    }
    __finally {
        IdTCPServer1->Contexts->UnlockList();
    }
    return Clients;
}

void __fastcall TMyForm::IdTCPServer1Connect(TIdContext *AContext)
{
    int TotalClients = GetClientCount();
    //...
}

void __fastcall TMyForm::IdTCPServer1Disconnect(TIdContext *AContext)
{
    int TotalClients = GetClientCount() - 1;
    //...
}
{code}

-- 
Remy Lebeau (TeamB)
0
Remy
8/17/2015 8:46:55 PM
Oh I see.

Thanks Remy,
Sudesh

"Remy Lebeau (TeamB)"  wrote in message 
news:729786@forums.embarcadero.com...

Sudesh wrote:

> TotalClients increments ok when I connect multiple clients but when
> all the clients are disconnected, the TotalClients number remains at 1
> instead of going to 0.

In the OnDisconnect event, the client that is triggering the event has not
been removed from the list yet.  You will have to account for that, eg:

{code}
int __fastcall TMyForm::GetClientCount()
{
    int Clients;
    TList *ClientsList = IdTCPServer1->Contexts->LockList();
    try {
        Clients = ClientsList->Count;
    }
    __finally {
        IdTCPServer1->Contexts->UnlockList();
    }
    return Clients;
}

void __fastcall TMyForm::IdTCPServer1Connect(TIdContext *AContext)
{
    int TotalClients = GetClientCount();
    //...
}

void __fastcall TMyForm::IdTCPServer1Disconnect(TIdContext *AContext)
{
    int TotalClients = GetClientCount() - 1;
    //...
}
{code}

-- 
Remy Lebeau (TeamB)
0
Sudesh
8/17/2015 10:41:57 PM
Reply: