Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit Unit1;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdComponent,
- IdCustomTCPServer, IdTCPServer, IdContext, Vcl.StdCtrls;
- type
- TForm1 = class(TForm)
- IdTCPServer1: TIdTCPServer;
- mmo1: TMemo;
- procedure IdTCPServer1Connect(AContext: TIdContext);
- procedure IdTCPServer1Execute(AContext: TIdContext);
- procedure IdTCPServer1Disconnect(AContext: TIdContext);
- procedure IdTCPServer1Exception(AContext: TIdContext;
- AException: Exception);
- procedure FormCreate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- uses IdGlobal,unit2,uKBDynamic;
- {$R *.dfm}
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- GIdDefaultTextEncoding := encUTF8;
- end;
- procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
- begin
- //
- mmo1.Lines.Add('connected')
- end;
- procedure TForm1.IdTCPServer1Disconnect(AContext: TIdContext);
- begin
- mmo1.Lines.Add('disconnected')
- end;
- procedure TForm1.IdTCPServer1Exception(AContext: TIdContext;
- AException: Exception);
- begin
- mmo1.Lines.Add('except '+AException.Message)
- end;
- procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
- var
- msRecInfo: TMemoryStream;
- lrec: TMyRecordWithVeryLongStrings;
- lsize: Integer;
- begin
- msRecInfo := TMemoryStream.Create;
- try
- lsize:=AContext.Connection.IOHandler.ReadLongInt;
- AContext.Connection.IOHandler.ReadStream(msRecInfo, lsize);//, False);
- msRecInfo.Position := 0;
- TKBDynamic.ReadFrom(msRecInfo, lrec, TypeInfo(TMyRecordWithVeryLongStrings));
- mmo1.Lines.Add(lrec.FirstName) ;
- mmo1.Lines.Add(lrec.LastName);
- finally
- msRecInfo.Free;
- end;
- end;
- end.
- unit Unit2;
- interface
- uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdComponent,
- IdTCPConnection, IdTCPClient, Vcl.StdCtrls;
- type
- TForm2 = class(TForm)
- IdTCPClient1: TIdTCPClient;
- btn1: TButton;
- btn2: TButton;
- procedure btn1Click(Sender: TObject);
- procedure btn2Click(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- type
- TMyRecordWithVeryLongStrings = record
- LenFirstName: Integer; // we store only the length of the string in this field
- LenLastName: Integer; // same as above
- Age: Byte;
- DateOfBirth: TDateTime;
- FirstName: string; // we will ignore this field when writing, using it for value
- LastName: string; // same as above
- bBoolean: Boolean;
- end;
- var
- Form2: TForm2;
- implementation
- uses IdGlobal,uKBDynamic;
- {$R *.dfm}
- procedure TForm2.btn1Click(Sender: TObject);
- begin
- IdTCPClient1.Connect;
- end;
- procedure TForm2.btn2Click(Sender: TObject);
- var
- lrec: TMyRecordWithVeryLongStrings;
- msRecInfo: TMemoryStream;
- begin
- lrec.LenFirstName := 1;
- lrec.LenLastName := 2;
- lrec.Age := 3;
- lrec.DateOfBirth := now;
- lrec.FirstName := 'nocwnoocnoncon forţează barajele poliţiilor de frontieră, care încropeswononwconwoncwocnwc 1';
- lrec.LastName := 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 2';
- lrec.bBoolean := true;
- msRecInfo := TMemoryStream.Create;
- try
- TKBDynamic.WriteTo(msRecInfo,lrec,TypeInfo(TMyRecordWithVeryLongStrings));
- IdTCPClient1.IOHandler.LargeStream := true;
- // writes the stream size then writes the stream data
- IdTCPClient1.IOHandler.Write(msRecInfo, 0, True);
- finally
- msRecInfo.Free;
- end;
- end;
- procedure TForm2.FormCreate(Sender: TObject);
- begin
- GIdDefaultTextEncoding := encUTF8;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement