[RESOLVE] lnet Send/Recive custom type lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- TheLastCayen · 2015-08-28 21:44 Hi, I am using :   Lazarus: 1.4.0   FPC: 2.6.4   lnet: 0.6.5   OS: Linux I need to send/receive a record but I am not sure if I am using the right function. For now, I use aSocket.send() and aSocket.get(). Also, I am not too sure how to set the size in the function aSocket.get(). Code: [Select]type   { data }   TData = record     User: string;     Message: String;     Time: TDateTime;   end; .... procedure TForm1.MCWriteKeyPress(Sender: TObject; var Key: char); var   CDATA:TData; begin   if Key = #13 then     Begin       CData.User:=ECName.Text;       CData.Time:=Now;       CData.Message:=MCWrite.Text;       Key:= #0;       MCWrite.Clear;       LTCPCClient.Send(CData,SizeOf(CData));     end;        end; ... procedure TForm1.LTCPCServerReceive(aSocket: TLSocket);  var   S:TData; begin   If aSocket.Get(S,SizeOf(S)+1) > 0 then     begin       MSRead.Lines.Add(DateToStr(s.Time)+' - '+'('+S.User+') '+S.Message);     end; end;    Please, tell me if I am using the right functions and how to use them. Thank you + Attachments and other options ---------------------------------------------------------------------- taazz · 2015-08-28 21:55 the record is full of dynamic fields. Try using array[255] of ansichar instead of a string. ---------------------------------------------------------------------- Leledumbo · 2015-08-29 15:34 SizeOf will not return the correct size as you have dynamic string field. Calculate the size manually (only you know the actual string length), and no, you can't read it in one go. String is not a simple contiguous character data only, it's a compiler defined type. Writing bytes blindy on it will break the structure. Read each field individually. ---------------------------------------------------------------------- TheLastCayen · 2015-09-11 15:00 Sorry for the late reply. Thank you taazz and Leledumbo, the static array fixed my issue. My code look like this now: Code: [Select]type   { data }   TData = record     User: array[0..24] of Char;     Message: array[0..255] of Char;        Time: TDateTime;   end; .... procedure TForm1.MCWriteKeyPress(Sender: TObject; var Key: char); var   CDATA:TData; begin   if Key = #13 then     Begin       fillchar(CData.User,25,#0);       fillchar(CData.Message,256,#0);       CData.User:=ECName.Text;       CData.Time:=Now;       CData.Message:=MCWrite.Text;       Key:= #0;       MCWrite.Clear;       LTCPCClient.Send(CData,SizeOf(CData));     end;        end; ... procedure TForm1.LTCPCServerReceive(aSocket: TLSocket);  var   S:TData; begin   fillchar(S.User,25,#0);   fillchar(S.Message,256,#0);     S.Time:=Now;    aSocket.Get(S,SizeOf(S))   MSRead.Lines.Add(DateToStr(s.Time)+' - '+'('+S.User+') '+S.Message); end;