通过Indy提高传输速度

时间:2012-12-01 20:11:07

标签: delphi indy indy10

我需要使用INDY10组件将许多文件从一个tcp indy服务器传输到客户端。有没有办法通过为TCP客户端或服务器通信设置任何参数来提高传输速度。

文件大小:~10 MBYte .... 50 Mybte

文件大小是否有限制,我的PC使用WIN 7 x64和32 GBYTE RAM我们的网络是LAN.100其他位置LAN已经改进到GIGABIT LAN

  function SendStream(AContext: TIdContext; AStream: TStream): Boolean; overload;
  var
    StreamSize: LongInt;
  begin
    try
      Result := True;
      try
        StreamSize := (AStream.Size);

        // AStream.Seek(0, soFromBeginning);

        AContext.Connection.IOHandler.Write(LongInt(StreamSize));
        AContext.Connection.IOHandler.WriteBufferOpen;
        AContext.Connection.IOHandler.Write(AStream, 0, False);
        AContext.Connection.IOHandler.WriteBufferFlush;
      finally
        AContext.Connection.IOHandler.WriteBufferClose;
      end;
    except
      Result := False;
    end;
  end;

2 个答案:

答案 0 :(得分:2)

发送代码可以缩减为

  function SendStream(AContext: TIdContext; AStream: TStream): Boolean;
  begin
    Result := True;
    try
      AContext.Connection.IOHandler.Write(AStream, 0, True);
    except
      Result := False;
    end;
  end;

第三个参数导致Indy将流大小写为Integer或Int64(取决于TIdIOHandler.LargeStream属性的值)值给客户端。

然后,客户端可以使用

读取流大小和流
// reads the stream size then reads the stream data
Client.IOHandler.ReadStream(MyStream, -1, False);

(在Delphi TidTCPServer and TidTCPClient transferring a record中找到,其中仅传输方向反转)

答案 1 :(得分:1)

直接发送流并保存正在生成的字节的另一个副本。特别是在你的例子中 - 删除提及WriteBuffer的3行代码。

根据流的大小和并发客户端的数量,您可能会使用您并不真正需要的大型缓冲副本来破坏内存管理器。您在大转移上的目标是限制某些事件处理整个流的次数。

相关问题