Déconnecter proprement un client
Bonjours,
J'ai crée un serveur avec borland, qui permet de réceptionner des messages venant de différents clients, cependant je me trouve devant un problème / erreur lorsqu'un client se déconnecte ou lorsque que je souhaite stoppé l'écoute de mon serveur :
- Le code fonctionne pour 1 client mais quand j'ai 2 client en simultané et que j'arrête mon serveur j'ai l'erreur suivante :
EListeError indice hors limite (1)
Fonction pour stopper l'écoute du serveur
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| void __fastcall TForm1::Arrter1Click(TObject *Sender)
{
TListItem * LI ;
int nbClients = Server1->Socket->ActiveConnections-1;
ShowMessage("Nbclients : " +IntToStr(nbClients));
for(int i =0;i<=nbClients;i++)
{
//On récup la socket a fermé en fonction du client
//Client 1 : Connections[0] = 408
//Client 2 : Connections[1] = 412
int SocketClients = Server1->Socket->Connections[i]->SocketHandle;
Server1->Socket->Connections[i]->SendText("quit");
//message de test
ShowMessage("Fermeture SocketHandle : " +IntToStr(SocketClients)+" connection[" + IntToStr(Server1->Socket->Connections[i])+"]");
//effacer la ligne correspondant au client déconnecté
LI = ListView1->FindCaption(0,IntToStr(SocketClients),false,true,false);
LI->Delete();
//deconnecter le client en fonction de sa socket
Server1->Socket->Connections[i]->Disconnect(SocketClients);
}
Server1->Socket->Close();
Server1->Active = false;
Form2->FlagListen = false;
StatusBar1->Panels->Items[3]->Text = "Status : innactif "; |
Fonction déconnexion du client
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| void __fastcall TForm1::Server1ClientDisconnect(TObject *Sender,
TCustomWinSocket *Socket)
{
TListItem * LI;
int SocketClient = Socket->SocketHandle;
//affichage test
ShowMessage("Fermeture SocketHandle : " +IntToStr(SocketClient));
//On ferme la socket ouverte
Socket->CleanupInstance();
Socket->Free();
Socket->Disconnect(SocketClient);
//Server1->Socket->Close();
//effacer la ligne du Listview correspondant au cleint déconnecté
LI = ListView1->FindCaption(0,IntToStr(SocketClient),false,true,false);
LI->Delete();
} |