unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdTCPServer, IdGlobal;
type
TForm1 = class(TForm)
Button1: TButton;
log: TMemo;
IdTCPServer1: TIdTCPServer;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
LastSessionID: Integer = 1;
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
SessionID: Integer;
procedure DoLog(const Msg: String);
var
S: String;
I: Integer;
begin
S := Format('%s-%0.6d: %s', [FormatDateTime('YYMMDD HHNNSS ZZZ', Now), SessionID, Msg]);
for I := 1 to Length(S) do
if S[I] in [#10,#13] then S[I] := ' ';
// da Memo1.Lines.Add() intern über SendMessage() arbeitet sollte dies Threadsafe sein.
log.Lines.Add(S);
end;
function ReadByte: Byte;
begin
AThread.Connection.ReadBuffer(Result, SizeOf(Result));
//DoLog(inttostr(Result));
end;
function ReadLong: Cardinal;
function SwapLong(Value: Cardinal): Cardinal;
// konvertiert Big Endian zu Little Endian
// im INet ist es üblich ALLE Daten in Big Endian zu übertragen
asm
BSWAP EAX
end;
begin
AThread.Connection.ReadBuffer(Result, SizeOf(Result));
Result := SwapLong(Result);
end;
function ReadString: String;
begin
SetLength(Result, ReadByte);
AThread.Connection.ReadBuffer(Result[1], Length(Result));
end;
procedure WriteByte(Value: Byte);
begin
AThread.Connection.WriteBuffer(Value, SizeOf(Value));
end;
procedure WriteString(const Value: ShortString);
begin
WriteByte(Length(Value));
AThread.Connection.WriteBuffer(Value[1], Length(Value));
end;
var
FileName: String;
FileSize: Cardinal;
FileStream: TStream;
cmd:integer;
begin
SessionID := LastSessionID;
Inc(LastSessionID);
cmd:=ReadByte;
DoLog(inttostr(cmd));
while True do
case cmd of
5: begin // LOGOUT
DoLog('try to read string');
DoLog(ReadString);
Break;
end;
end;
sleep(1);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
IdTCPServer1.Active:=false;
end;
procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
log.Lines.Add('Client disconnected');
end;
end.