Einzelnen Beitrag anzeigen

DelTurbo

Registriert seit: 12. Dez 2009
Ort: Eifel
1.194 Beiträge
 
Delphi 2007 Architect
 
#5

AW: IdTCPServer <-> IdTCPClient verändert Daten

  Alt 1. Dez 2017, 15:10
So,
nun hab ich euch genug genervt. Habe das doch noch hinbekommen. Das OnExecute sollte so aussehen.

Trotzdem danke an alle

Delphi-Quellcode:
procedure TfMain.IdTCPServer1Execute(AContext: TIdContext);
var
  Cli :TIdTCPClient;
  IOhnd :TIdIOHandlerStack;
  Data :string;
  Len :Int64;
  Buf :TIdBytes;
begin
  try
    if ( PeerIPs.IndexOfName(AContext.Connection.Socket.Binding.PeerIP)<>-1 ) then begin //bekannt
      GblUrl:=PeerIPs.Values[AContext.Connection.Socket.Binding.PeerIP];
    end else Exit;
    Cli := nil;
    try
      // #### Create IOHandler ####
      IOhnd:=TIdIOHandlerStack.Create(nil);
      { Create & Connect to Server }
      Cli:=TIdTCPClient.Create(nil);
      Cli.IOHandler:=IOhnd;
      Cli.Host := GblUrl;
      Cli.Port := 80;
      { Connect to the remote server }
      Cli.Connect;
      { Read/Write loop }
      repeat
        { Read data from Client }
        if ( AContext.Connection.IOHandler.InputBuffer.Size>0 ) then begin
          Len :=AContext.Connection.IOHandler.InputBuffer.Size;
          Data:=AContext.Connection.IOHandler.ReadString(Len);
          { Write it to the Server }
          Cli.IOHandler.Write(Data);
        end;
        { Read data from Server }
        if ( Cli.IOHandler.InputBuffer.Size>0 ) then begin
          Len:=Cli.IOHandler.InputBuffer.Size;
          Cli.IOHandler.ReadBytes(Buf,Len,False);
          { Write it to the Server }
          AContext.Connection.IOHandler.Write(Buf,Len);
        { Release system slizes }
        end;
        SleepEx(1, True);
        Cli.CheckForGracefulDisconnect(False);
        AContext.Connection.CheckForGracefulDisconnect(False);
      until (not AContext.Connection.Connected) or (not Cli.Connected);
    finally
      if Assigned(Cli) then
      begin
        Cli.Disconnect;
        Cli.Free
      end;
      { Disconnect real client }
      AContext.Connection.Disconnect;
      if ( Assigned(IOhnd) ) then begin
        IOhnd.Free;
      end;
    end;
  except
  end;
end;
Alle meine Rechtschreibfehler sind Urheberrechtlich geschützt!!
  Mit Zitat antworten Zitat