Wops!
Acudo a vosotros para pediros otra vez ayuda
Estoy intentando que el servidor me enumere las conexiones clientes... he creado un ListBox llamado "lbClients", una clase llamada Client, y un objeto TList llamado Clients donde guardo la lista de Client.
Código Delphi
[-]
type
TSimpleClient = class(TObject)
DNS,
Name : String;
ListLink : Integer;
Thread : Pointer;
end;
TfrmLambda = class(TForm)
IdTCPServer1: TIdTCPServer;
lbClients: TListBox;
private
public
Clients : TList;
procedure BroadcastMsg(Codigo,Mensaje:string);
procedure UpdateClientList;
end;
Bien. El TList se crea en el evento OnCreate del form... y se rellena de la siguiente manera:
Código Delphi
[-]
procedure TfrmLambda.IdTCPServer1Connect(AContext: TIdContext);
var
Client : TSimpleClient;
begin
Client := TSimpleClient.Create;
Client.DNS := AContext.Connection.Socket.Binding.PeerIP;
Client.Name := 'Registrandose';
Client.ListLink := lbClients.Items.Count;
Client.Thread := AContext;
lbClients.Items.Add(Client.Name);
AContext.Data := Client;
Clients.Add(Client);
AContext.Connection.IOHandler.Write('Nick: ');
end;
Cuando el cliente se desconecta, evidentemente, hay que quitarlo de en medio...
Código Delphi
[-]
procedure TfrmLambda.IdTCPServer1Disconnect(AContext: TIdContext);
var
Client : TSimpleClient;
begin
Client := Pointer(AContext.Data);
BroadCastMsg('004', '*** '+Client.Name+' se ha salido');
Clients.Delete(Client.ListLink);
lbClients.Items.Delete(lbClients.Items.IndexOf(Client.Name));
Client.Free;
AContext.Data := nil;
UpdateClientList;
end;
Y nos falta el método por el cual actualizo la lista de clientes...
Código Delphi
[-]
procedure TfrmLambda.UpdateClientList;
var
Count : Integer;
begin
for Count := 0 to lbClients.Items.Count -1 do
if Count < Clients.Count then
lbClients.Items.Strings[Count] := TSimpleClient(Clients.Items[Count]).Name;
end;
Pues bien, gran parte de este código por no decir que prácticamente todo está cojido de un ejemplo de las indy que tenía por ahí y portado a Indy 10.
El programa me falla específicamente cuando un cliente se desconecta y hago clic en uno que esté por debajo (se ha conectado posteriormente). En el OnClic del ListBox tengo el siguiente código:
Código Delphi
[-]
procedure TfrmLambda.lbClientsClick(Sender: TObject);
var
Client : TSimpleClient;
begin
if lbClients.ItemIndex = -1 then
exit;
Client := Clients.Items[lbClients.ItemIndex];
lbNombre.Caption := Client.Name;
lbIp.Caption := Client.DNS;
end;
Muchísimas gracias a todos... y un abrazo