unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DdeMan, Scktcomp, StdCtrls;
type
TForm1 =
class(TForm)
Button1: TButton;
Memo1: TMemo;
Edit1: TEdit;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ServerClientRead(Sender: TObject; Socket: TCustomWinSocket);
private
{ Private-Deklarationen }
public
Server: TServerSocket;
Client: TClientSocket;
{ Public-Deklarationen }
end;
type
CustomRec =
record
Text:
String[20];
Zahl: byte;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
//Initialisierung des Objekts (ältere Delphi-Versionen)
try
Server := TServerSocket.Create(Form1);
Client := TClientSocket.Create(Form1);
Server.OnClientRead := ServerClientRead;
//Nur bei älteren Versionen
Server.Port := 10024;
Server.Open;
Client.Port := 10024;
Client.Host := '
127.0.0.1';
Client.Open;
except
//Im Fehlerfall wieder freigeben (ältere Versionen)
Client.Free;
Server.Free;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Client.Free;
Server.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Rec: CustomRec;
begin
//Unserem Record werden Werte zugewiesen
with Rec
do begin
Text := Edit1.Text;
//In Edit2 nur Zahlen eingeben!
Zahl := StrToInt(Edit2.Text);
end;
//Unser Record wird verschickt!
Client.Socket.SendBuf(Rec,sizeof(Rec));
end;
procedure TForm1.ServerClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
Rec: CustomRec;
begin
//wir holen unseren Record ab...
Server.Socket.ReceiveBuf(Rec,SizeOf(Rec));
//...und übertragen den Inhalt in das MemoFeld
with Rec
do begin
Memo1.Lines.Add(Text);
Memo1.Lines.Add(IntToStr(Zahl));
end;
end;
end.