unit uMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp;
type TStrArr =
array of char;
type UserRec =
record
id : integer;
Username : TStrArr;
Password : TStrArr;
end;
TMessage_Header =
Record
Size : Longword;
End;
type
TForm1 =
class(TForm)
client: TClientSocket;
server: TServerSocket;
Memo1: TMemo;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure serverClientRead(Sender: TObject; Socket: TCustomWinSocket);
private
function ChartoStr(strarr: TStrArr):
String;
function StrToChar(str:
String): TStrArr;
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Clear;
server.Port := 32;
client.Port := 32;
server.Active := TRUE;
client.Host := '
127.0.0.1';
client.Active := true;
end;
procedure TForm1.Button1Click(Sender: TObject);
var header : TMessage_Header;
sendstream : TMemoryStream;
ur : UserRec;
begin
sendstream:=TMemorystream.create;
ur.id := 1;
ur.Username := strtochar('
username');
ur.Password := strtochar('
password');
header.Size := SizeOf(ur);
sendstream.WriteBuffer(Header, SizeOf(Header));
sendstream.WriteBuffer(ur,sizeof(ur));
sendstream.position := 0;
client.Socket.SendStream(sendstream);
end;
function TForm1.ChartoStr(strarr: TStrArr):
String;
var i : integer;
begin
result := '
';
for i := 0
to SizeOf(strarr)-1
do
begin
result := result + strarr[i];
end;
end;
function TForm1.StrToChar(str:
String): TStrArr;
var i : integer;
begin
SetLength(result, length(str));
for i := 1
to length(str)
do
begin
result[i-1] := str[i];
end;
end;
procedure TForm1.serverClientRead(Sender: TObject; Socket: TCustomWinSocket);
var rec : UserRec;
header : TMessage_Header;
nRead : Integer;
Buff :
Array[Word]
Of Byte;
OldPos : Integer;
fdata : TMemoryStream;
begin
fdata := TMemoryStream.Create;
OldPos := fdata.Position;
fData.Position := fData.Size;
Repeat
nRead := Socket.ReceiveBuf(buff, High(Buff) - Low(Buff));
fData.WriteBuffer(buff, nRead);
Until ( Socket.ReceiveLength = 0 );
fData.Position := OldPos;
fdata.ReadBuffer(header, sizeof(header));
fdata.readbuffer(rec, header.size);
Memo1.Lines.Add(inttostr(rec.id));
Memo1.Lines.Add(chartostr(rec.Username));
Memo1.Lines.Add(chartostr(rec.Password));
fdata.Free;
end;
end.