unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdTCPConnection, IdTCPClient, IdBaseComponent,
IdComponent, IdTCPServer;
type
TTest=record
a,b,c:
String[50];
end;
TForm1 =
class(TForm)
Server: TIdTCPServer;
Client: TIdTCPClient;
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ServerExecute(AThread: TIdPeerThread);
private
{ Private-Deklarationen }
FTest : TTest;
procedure UpdateForm;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Server.Active:=Not Server.Active;
If Server.Active
then
Button1.Caption:='
Shutdown Server'
else
Button1.Caption:='
Run Server';
end;
procedure TForm1.Button2Click(Sender: TObject);
var
Test : TTest;
MStream : TMemoryStream;
begin
Test.a:='
Jens Schumann';
Test.b:='
at';
Test.c:='
www.DelphiPraxis.net';
MStream:=TMemoryStream.Create;
Client.Connect;
Try
MStream.
Write(Test,SizeOf(TTest));
Client.OpenWriteBuffer;
Client.WriteStream(MStream,True,True);
Client.CloseWriteBuffer;
Finally
Client.Disconnect;
MStream.Free;
end;
end;
procedure TForm1.ServerExecute(AThread: TIdPeerThread);
{Achtung: Diese Event wird im Kontext das Indy-Thread ausgeführt.
D.h. nicht im Kontext des MainThread's deshalb werden Aktualisierungen
des Formulars über Synchronize gemacht}
var
Test : TTest;
MStream : TMemoryStream;
F : TThread;
begin
MStream:=TMemoryStream.Create;
Try
Athread.Connection.ReadStream(MStream);
MStream.Position:=0;
MStream.ReadBuffer(FTest,SizeOf(TTest));
AThread.Synchronize(UpdateForm);
Finally
MStream.Free;
end;
end;
procedure TForm1.UpdateForm;
begin
Memo1.Clear;
Memo1.Lines.Add(FTest.a);
Memo1.Lines.Add(FTest.b);
Memo1.Lines.Add(FTest.c);
end;
end.