简体   繁体   English

Delphi,Indy,如何高效发送记录

[英]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:通过IndyTCPClientIndyTCPServer传输记录数据时,我总是使用一种简单的方法 - 所有记录都具有固定大小并放置到 stream: 在客户端:

  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.它工作正常,但似乎不是很有效,因为我需要使用几乎总是空的固定长度字符串,而且记录通常作为需要发送的大型 arrays 的一部分。 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.否则,您可以编写更简单的代码(例如,使用TIdMemoryBufferStream而不是TMemoryStream ),这将允许您使用更少的代码传输大量数据,但代价是使用效率较低的传输格式。 So you need to decide what will better suit your needs.因此,您需要决定什么更适合您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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