Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
- procedure CopyInputBuffer(Comment: string; Source, Dest: TIdIOHandler);
- var
- Stream: TMemoryStream;
- begin
- Stream := TMemoryStream.Create;
- try
- Source.InputBufferToStream(Stream);
- Stream.Position := 0;
- Dest.Write(Stream, Stream.Size);
- Log(Comment + ' ' + IntToStr(Stream.Size) + ' bytes');
- finally
- FreeAndNil(Stream);
- end;
- end;
- var
- Server: TIdTCPClient;
- Host, Port: string;
- begin
- if AContext.Data = nil then // First connect
- begin
- Server := TIdTCPClient.Create;
- Host := AContext.Connection.IOHandler.ReadLn(':');
- Port := AContext.Connection.IOHandler.ReadLn(#13#10#13#10);
- Server.Host := Host;
- Server.Port := StrToIntDef(Port, 80);
- Memo1.Lines.Add(Server.Host + ':' + IntToStr(Server.Port));
- AContext.Data := Server;
- try
- Server.Connect;
- except
- on E: Exception do
- begin
- Log('Error while connecting: ' + E.ClassName + ' ' + E.Message);
- Server.Disconnect;
- Server.Free;
- AContext.Data := nil;
- AContext.Connection.Disconnect;
- end;
- end;
- end
- else
- begin
- Server := TIdTCPClient(AContext.Data);
- Server.IOHandler.CheckForDataOnSource(50);
- AContext.Connection.IOHandler.CheckForDataOnSource(50);
- if Server.Connected and AContext.Connection.Connected then
- begin
- if not AContext.Connection.IOHandler.InputBufferIsEmpty then
- CopyInputBuffer('CLIENT -> SERVER', AContext.Connection.IOHandler, Server.IOHandler);
- if not Server.IOHandler.InputBufferIsEmpty then
- CopyInputBuffer('SERVER -> CLIENT', Server.IOHandler, AContext.Connection.IOHandler);
- end
- else
- begin
- Log('Disconnected');
- Server.Disconnect;
- Server.Free;
- AContext.Data := nil;
- AContext.Connection.Disconnect;
- end;
- end;
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement