简体   繁体   中英

Delphi, Indy, how to send records efficiently

When transferring records data via IndyTCPClient and IndyTCPServer I always use a simple approach - all records have a fixed size and are placed to the stream: On the client side:

  type
    TUser = record
      i:integer;
      s:string[100];
      i64:Int64;
      s2:string[200];
    end;

  with idClient.Socket do
    begin
      MStream := TMemoryStream.Create;
      try
        MStream.Write(User, sizeOf(TUser));
        MStream.Seek(0, soBeginning);
        write(MStream, MStream.Size);
      finally
        MStream.Free;
      end;
    end;

On the server side:

  with AIdCondext.Connection.Socket do
  begin
    MStream := TMemoryStream.Create;
    try
      ReadStream(MStream, sizeOf(TUser), False);
      MStream.Seek(0, soBeginning);
      MStream.read(User, MStream.Size);
    finally
      MStream.Free;
    end;
  end;

It works fine but it seems not very efficient cause I need to use fixed length strings which are almost always empty and also records often come as part of large arrays that need to be sent. Is there way to do it more efficiently without sending record members separately?

There is a trade-off to what you are asking for. If you want the transmission on the wire to be more efficient (less bandwidth, etc), then you need to write more code to serialize each record into a more efficient format on the wire. Otherwise, you can write simpler code (for instance, using TIdMemoryBufferStream instead of TMemoryStream ), which will allow you to transfer larger amounts of data using less code, but at the cost of using a less efficient transmission format. So you need to decide what will better suit your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM