Recent

Author Topic: [RESOLVE] lnet Send/Recive custom type  (Read 2955 times)

TheLastCayen

  • Jr. Member
  • **
  • Posts: 81
[RESOLVE] lnet Send/Recive custom type
« on: August 28, 2015, 11:44:10 pm »
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
« Last Edit: September 11, 2015, 05:00:50 pm by TheLastCayen »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: lnet Send/Recive custom type
« Reply #1 on: August 28, 2015, 11:55:49 pm »
the record is full of dynamic fields. Try using array[255] of ansichar instead of a string.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: lnet Send/Recive custom type
« Reply #2 on: August 29, 2015, 05:34:51 pm »
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

  • Jr. Member
  • **
  • Posts: 81
Re: lnet Send/Recive custom type
« Reply #3 on: September 11, 2015, 05:00:04 pm »
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;   


 

TinyPortal © 2005-2018