Files Delphi

Title: Send Files with speed limit
Question: How to send files with speed limit
Answer:
procedure SendFileThrottled(const AFilename:string; TCP:ClientSocket;
const Speed:Integer=0);
const
MaxBlockSize = 1024;
var
FS:TFileStream;
Delay,
BlockSize:Integer;
Buffer:array[0..Pred(MaxBlockSize)]of Byte;
begin
FS := TFileStream.Create(AFilename, fmOpenRead or fmShareDenyWrite);
try
if Speed0 then
Delay := 1000 div Speed
else
Delay := 0;
while (FS.Position FS.Size) do begin
BlockSize:=Min(MaxBlockSize, FS.Size - FS.Position);
FS.Read(Buffer[0],BlockSize);
TCP.Socket.SendBuf(Buffer[0],BlockSize);
Sleep(Delay);
end;
finally
FreeAndNil(FS);
end;
end;