Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdComponent,
  8. IdCustomTCPServer, IdTCPServer, IdContext, Vcl.StdCtrls;
  9.  
  10. type
  11. TForm1 = class(TForm)
  12. IdTCPServer1: TIdTCPServer;
  13. mmo1: TMemo;
  14. procedure IdTCPServer1Connect(AContext: TIdContext);
  15. procedure IdTCPServer1Execute(AContext: TIdContext);
  16. procedure IdTCPServer1Disconnect(AContext: TIdContext);
  17. procedure IdTCPServer1Exception(AContext: TIdContext;
  18. AException: Exception);
  19. procedure FormCreate(Sender: TObject);
  20. private
  21. { Private declarations }
  22. public
  23. { Public declarations }
  24. end;
  25.  
  26. var
  27. Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. uses IdGlobal,unit2,uKBDynamic;
  32.  
  33. {$R *.dfm}
  34.  
  35.  
  36. procedure TForm1.FormCreate(Sender: TObject);
  37. begin
  38. GIdDefaultTextEncoding := encUTF8;
  39. end;
  40.  
  41. procedure TForm1.IdTCPServer1Connect(AContext: TIdContext);
  42. begin
  43. //
  44. mmo1.Lines.Add('connected')
  45. end;
  46.  
  47. procedure TForm1.IdTCPServer1Disconnect(AContext: TIdContext);
  48. begin
  49. mmo1.Lines.Add('disconnected')
  50. end;
  51.  
  52. procedure TForm1.IdTCPServer1Exception(AContext: TIdContext;
  53. AException: Exception);
  54. begin
  55. mmo1.Lines.Add('except '+AException.Message)
  56. end;
  57.  
  58. procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
  59. var
  60. msRecInfo: TMemoryStream;
  61. lrec: TMyRecordWithVeryLongStrings;
  62. lsize: Integer;
  63. begin
  64. msRecInfo := TMemoryStream.Create;
  65. try
  66. lsize:=AContext.Connection.IOHandler.ReadLongInt;
  67. AContext.Connection.IOHandler.ReadStream(msRecInfo, lsize);//, False);
  68.  
  69. msRecInfo.Position := 0;
  70.  
  71. TKBDynamic.ReadFrom(msRecInfo, lrec, TypeInfo(TMyRecordWithVeryLongStrings));
  72.  
  73. mmo1.Lines.Add(lrec.FirstName) ;
  74. mmo1.Lines.Add(lrec.LastName);
  75. finally
  76. msRecInfo.Free;
  77. end;
  78. end;
  79.  
  80. end.
  81.  
  82. unit Unit2;
  83.  
  84. interface
  85.  
  86. uses
  87. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  88. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdComponent,
  89. IdTCPConnection, IdTCPClient, Vcl.StdCtrls;
  90.  
  91. type
  92. TForm2 = class(TForm)
  93. IdTCPClient1: TIdTCPClient;
  94. btn1: TButton;
  95. btn2: TButton;
  96. procedure btn1Click(Sender: TObject);
  97. procedure btn2Click(Sender: TObject);
  98. procedure FormCreate(Sender: TObject);
  99. private
  100. { Private declarations }
  101. public
  102. { Public declarations }
  103. end;
  104.  
  105. type
  106. TMyRecordWithVeryLongStrings = record
  107. LenFirstName: Integer; // we store only the length of the string in this field
  108. LenLastName: Integer; // same as above
  109. Age: Byte;
  110. DateOfBirth: TDateTime;
  111. FirstName: string; // we will ignore this field when writing, using it for value
  112. LastName: string; // same as above
  113. bBoolean: Boolean;
  114. end;
  115.  
  116. var
  117. Form2: TForm2;
  118.  
  119.  
  120. implementation
  121.  
  122. uses IdGlobal,uKBDynamic;
  123.  
  124. {$R *.dfm}
  125.  
  126.  
  127. procedure TForm2.btn1Click(Sender: TObject);
  128. begin
  129. IdTCPClient1.Connect;
  130. end;
  131.  
  132. procedure TForm2.btn2Click(Sender: TObject);
  133. var
  134. lrec: TMyRecordWithVeryLongStrings;
  135. msRecInfo: TMemoryStream;
  136. begin
  137. lrec.LenFirstName := 1;
  138. lrec.LenLastName := 2;
  139. lrec.Age := 3;
  140. lrec.DateOfBirth := now;
  141. lrec.FirstName := 'nocwnoocnoncon forţează barajele poliţiilor de frontieră, care încropeswononwconwoncwocnwc 1';
  142. lrec.LastName := 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 2';
  143. lrec.bBoolean := true;
  144.  
  145. msRecInfo := TMemoryStream.Create;
  146. try
  147.  
  148. TKBDynamic.WriteTo(msRecInfo,lrec,TypeInfo(TMyRecordWithVeryLongStrings));
  149.  
  150. IdTCPClient1.IOHandler.LargeStream := true;
  151.  
  152. // writes the stream size then writes the stream data
  153. IdTCPClient1.IOHandler.Write(msRecInfo, 0, True);
  154. finally
  155. msRecInfo.Free;
  156. end;
  157. end;
  158.  
  159. procedure TForm2.FormCreate(Sender: TObject);
  160. begin
  161. GIdDefaultTextEncoding := encUTF8;
  162. end;
  163.  
  164. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement