Newsgroups : Borland : borland.public.delphi.internet.winsock : 2005 Sep : Connection is ok but the events don't work for long time???

www.cryer.info
Managed Newsgroup Archive

Connection is ok but the events don't work for long time???

Subject:Connection is ok but the events don't work for long time???
Posted by:"Daniel Henrique" (progtecn..@hotmail.com)
Date:28 Sep 2005 09:07:20

Hi,

I have two applications, a Client and another Server. They have a TidTCPClient and a TidTCPServer (Indy components). When the client connects in my TidTCPServer server, my Server connects in the Client (That also has a TidTCPServer) to send to each N seconds one information.

However after some hours, the event to CommandHandler of the TidTCPServer (Side of the client) seems that it doesn't work, but the connection remains active and the packages are changed normally between the two applications.

Why this happens? The connection is active but the events of indy do not work after some time... My code:

//*********** Server Application ***********
TMyPeerThread = class(TIdPeerThread)
public
  Host: String;
  Port: Integer;
end;

procedure TFrmServer.FormCreate(Sender: TObject);
begin
  // Server settings
  with indyServer do begin
    ThreadClass := TMyPeerThread;
  end;
end;

procedure TFrmServer.indyServerConnect(APeer: TIdPeerThread);
var
  PeerIP: string;
  S: String;
begin
  PeerIP := APeer.Connection.Socket.Binding.PeerIP;

  with TMyPeerThread(APeer) do begin
    S := Connection.ReadLn;
    Host := Values[0]; // Client Host
    Port := Values[1]; // (Client Server Port )
  end;

  // Connects with the client and keeps the opened connection
  // to prevent delays related with research DNS
  with APeer do begin
    Data := TIdTCPClient.Create(nil);
    with TIdTCPClient(Data) do begin
      Host := Connection.Socket.Binding.PeerIP;
      Port := TMyPeerThread(APeer).Port;
      Connect;
      if Connected then
        tmrPing.Enabled := true;
    end;
  end;
end;

procedure TFrmServer.tmrPingTimer(Sender: TObject);
var
  List: TList;
  APeer: TMyPeerThread;
  N: Integer;
begin
  tmrPing.Enabled := False;
  try
    List := indyServer.Threads.LockList;
    try
      for N := 0 to Pred(List.Count) do begin
        APeer := TMyPeerThread(List.Items[N]);
        with TIdTCPClient(APeer.Data) do
        begin
          if not Connected then
            Continue;
          try
            WriteLn('MESSAGE');
            WriteLn('blablabla');
          except
            on E: Exception do begin
              ShowMessage(E.Message);
            end;
          end;
        end;
      end;
    finally
      indyServer.Threads.UnlockList;
    end;
  finally
    tmrPing.Enabled := True;
  end;
end;

//*********** Client Application ***********
TMyPeerThread = class(TIdPeerThread)
public
  Host: String;
  Port: Integer;
end;

procedure TFrmClient.FormCreate(Sender: TObject);
begin
  // Server settings
  with indyServer do begin
    ThreadClass := TMyPeerThread;
    with CommandHandlers do
      Items[0].Command := 'MESSAGE';
  end;
end;

procedure TFrmClient.indyClientConnected(Sender: TObject);
begin
  indyClient.WriteLn(indyClient.LocalName + ' ' + IntToStr(indyServer.DefaultPort));
end;

// TidCommandHandler
procedure TFrmClient.indyServerDoMessageCommand(ASender: TIdCommand);
var
  Buffer: String;
begin
  Command := TIdCommand(ASender);
  Peer := TMyPeerThread(Command.Thread);
  Buffer := Peer.Connection.ReadLn;
  ShowMessage(Buffer);
end;

Thanks,

Daniel Henrique.

Replies:

www.cryer.info
Managed Newsgroup Archive