PDA

Ver la Versión Completa : Detectar Socket Desconectado


Edgtho
08-09-2006, 21:13:38
Buenas, estoy con algo que me tiene de cabeza. Estoy probando comunicaciones entre sockets, digamos que tengo un servidor y un cliente. El cliente se conecta, y perfecto sale como que se ha conectado. Ahora la clave es que me interesa saber como detectar si por alguna razon el cliente se ha desconectado a lo bestia (reseteo del ordenador o cierre a lo bestia).

El codigo del servidor es este

procedure TForm1.FormCreate(Sender: TObject);
begin
{ Initalize our clients list }
Clients := TList.Create;
end;

procedure TForm1.IdTCPServerConnect(AThread: TIdPeerThread);
var
Client : TSimpleClient;
begin
//Se crea un cliente especifico a esta conexion
Client := TSimpleClient.Create;

Client.RemoteIP := AThread.Connection.Socket.Binding.PeerIP; // IP de conexion
Client.DNS := AThread.Connection.LocalName; // Nombre local
Client.Name := 'Log'; // Nombre asignado
Client.ListLink := Contador; // Indice del contador

// Punteros de identificacion del hilo de conexion

Client.Thread := AThread;
AThread.Data := Client;
Athread.Connection.ASCIIFilter := False;
Clients.Add(Client);

mLog.Lines.Add('Conexion de ' + client.Name + '-' + client.RemoteIP);
DateToStr(Date) + ';' + TimeToStr(Time));
end;

procedure TForm1.IdTCPServerDisconnect(AThread: TIdPeerThread);
var
Client : TSimpleClient;
nombre: string;
begin
// Identifico el hilo
Client := Pointer(AThread.Data);

mLog.Lines.Add('Desconexion de ' + client.Name + '-' + client.RemoteIP);
// Elimino el hilo de los registros
Clients.Delete(Client.ListLink);

// Liberacion de recursos
Client.Free;
AThread.Data := nil;
end;


procedure TForm1.IdTCPServerExecute(AThread: TIdPeerThread);
var
Client : TSimpleClient;
begin
// Identifico el hilo
Client := Pointer(AThread.Data);

end;

Y estoy intentando detectar si el cliente se ha desconectado mediante este codigo

var
List: TList;
I: Integer;
begin

List := IdTCPServer.Threads.LockList;
try
Application.ProcessMessages;
for I := 0 to List.Count - 1 do
begin
try
// TIdPeerThread(List.Items[i]).Connection.CheckForDisconnect;
TIdPeerThread(List.Items[i]).Connection.CheckForDisconnect(True,False);
TIdPeerThread(List.Items[i]).Connection.WriteLn('algo');
// TIdPeerThread(List.Items[i]).Connection.CheckForDisconnect(False,True);
if TIdPeerThread(List.Items[i]).connection.Connected then
begin
showmessage('hola');
end;
except
TIdPeerThread(List.Items[i]).Connection.Disconnect;
Sleep(1000);
end;
end;
finally
IdTCPServer.Threads.UnlockList;
end;

Como veis he probado con varias combinaciones de checkfordisconnect, pero nada. Aunque aunque apague el router me sigue dando como conectado (en mi caso el mensaje hola) ¿Alguien me puede echar un cable?