Club Delphi  
    Paypal   FTP   CCD     Buscar   Trucos   Trabajo   Foros

Retroceder   Foros Club Delphi > Principal > OOP
Registrarse FAQ Miembros Calendario Guía de estilo Temas de Hoy

Coloboración Paypal con ClubDelphi

 
 
Herramientas Buscar en Tema Desplegado
  #1  
Antiguo 09-10-2005
Avatar de DarkByte
DarkByte DarkByte is offline
Miembro
 
Registrado: sep 2003
Ubicación: Desconocido
Posts: 1.322
Poder: 24
DarkByte Va por buen camino
Otro típico List Index Out Of Bounds

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
    { Private declarations }
  public
    { Public declarations }
    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
{ Loop through all the clients connected to the system and set their names }
  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
__________________
:)
Responder Con Cita
 



Normas de Publicación
no Puedes crear nuevos temas
no Puedes responder a temas
no Puedes adjuntar archivos
no Puedes editar tus mensajes

El código vB está habilitado
Las caritas están habilitado
Código [IMG] está habilitado
Código HTML está deshabilitado
Saltar a Foro


La franja horaria es GMT +2. Ahora son las 01:42:40.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi
Copyright 1996-2007 Club Delphi