Sending a Dynamic array (Inside a record) through Socket?

后端 未结 2 718
悲哀的现实
悲哀的现实 2021-01-24 05:31

i\'m trying to transfer a record from server to client, directly using .SendBuf().

however, this record has a member which is a dynamic array, and i have read somewhere

2条回答
  •  隐瞒了意图╮
    2021-01-24 06:06

    As mentioned in some comments, serialize your record to a stream and then send the stream contents over the wire. I use kbLib in some of my projects and it works really good. You can use any dynamic type like strings, arrays in your record.

    Small example:

    type 
      TMyRecord = record
        str : string;
      end;
    
    procedure Test;
    
    var
     FStream : TMemoryStream;
     MYrecord : TMyRecord;
     MYrecord1 : TMyRecord;
    
    begin
     FStream := TMemoryStream.Create;
     try
      MyRecord.Str := 'hello world';
      // save record to stream 
      TKBDynamic.WriteTo(FStream, MyRecord, TypeInfo(TMyRecord)); 
      FStream.Position := 0;
      // read record from stream
      TKBDynamic.ReadFrom(FStream, MyRecord1, TypeInfo(TMyRecord));  
      If MyRecord1.Str <> MyRecord.Str then
       ShowMessage('this should not happen!');
      finally
       FStream.Free; 
      end;
    end;
    

提交回复
热议问题