Einzelnen Beitrag anzeigen

Klaus01

Registriert seit: 30. Nov 2005
Ort: München
5.768 Beiträge
 
Delphi 10.4 Sydney
 
#2

Re: Indy TCP Client - Auf Nachrichten warten...

  Alt 16. Sep 2009, 20:56
Guten Abend,

ich habe das mal für einen Thread so gelöst.

Delphi-Quellcode:
type
 TTcpClientThread = class(TThread)
    private
      tcpClient : TIdTcpClient;
      alarmFilter : TAlarmFilter;
      reports : TReporter;
      procedure myOnConnected(Sender: TObject);
      procedure myOnDisconnected(sender: TObject);
      procedure myOnStatus(aSender: TObject; const aStatus: TIdStatus; const aStatusText: AnsiString);
    public
      constructor Create(comServ: TComServ);virtual;
      procedure Execute; override;
      destructor Destroy; override;
  end;

und die execute Methode:
Delphi-Quellcode:
 procedure TTcpClientThread.Execute;
  var
    addNewLine : Boolean;
    noDataCounter : Byte;
    s : AnsiString;
  begin
    addNewLine := false;
    noDataCounter := 0;
    while (not terminated) do
      begin
        try
          while not tcpClient.Connected do
            begin
              try
                tcpClient.Connect;
              except
                on E:Exception do
                  begin
                    writeLn('Error '+E.Message);
                    sleep(5000);
                  end;
              end;
            end;

          if not tcpClient.IOHandler.InputBufferIsEmpty then
            begin
              s:= tcpClient.IOHandler.InputBufferAsString;
              alarmFilter.getData(s);
              //reporter.getData(s);
              s:='';
              noDataCounter := 0;
              addNewLine := true;
            end
          else
            begin
              sleep(100);
              inc(noDataCounter);
              if (noDataCounter > 5) and addNewLine then
                begin
                  addNewLine := false;
                  alarmFilter.getData(#13#10);
                end;
            end;

        except
          on E:Exception do
            begin
              writeLn('Error '+E.Message);
              tcpClient.IOHandler.CloseGracefully;
            end;
        end;
      end;
    tcpClient.Disconnect;
  end;
Grüße
Klaus
Klaus
  Mit Zitat antworten Zitat