procedure SendStream(const AStream: TStream);
var
Buffer: array[0..4095] of Byte; // 每包最大4K
StartPos, AmountInBuf, AmountSent: Integer;
begin
if AStream = nil then
Exit;
AStream.position := 0;
while True do
begin
StartPos := AStream.Position;
AmountInBuf := AStream.Read(Buffer, SizeOf(Buffer)); // 从流中取一个包
if AmountInBuf > 0 then
begin
AmountSent := socket.Send // 发送一个包
AStream.Position := StartPos + AmountSent; // 移动流指针
if AStream.Position = AStream.Size then // 流指针已经移到末尾
Break;
end
else Break;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
LStream: TMemoryStream;
begin
// 查询数据
FDQuery1.Close;
FDQuery1.SQL.Clear;
FDQuery1.SQL := '';
FDQuery1.Open;
// 流化数据并发送
LStream := TMemoryStream.Create;
try
FDQuery1.SaveToStream(LStream, sfBinary);
SendStream(LStream);
finally
LStream.Free;
end;
end;