On the receiving I need it because without it the last stream appears on Log and ins't in file
That shouldn't happen.
Look at the code.
Thread.Execute is always executed only once (and you don't have a loop in there).
So:
MainStream:=TFileStream.Create(infile_name,fmCreate);
Connection.RecvStream(MainStream,60000);
if MainStream.Size>=StrToInt(infile_size) then begin
MainStream.free;
StatString:='>:Transfer Done!';
Synchronize(@ShowStat);
mode:='none';
Form1.ProgressLabelPercent.Caption:='100%';
Form1.ProgressBar.Position:=Form1.ProgressBar.Max;
Form1.ProgressLabelByte.Caption:=(infile_size+'/'+infile_size);
end;
In case of an interrupt the MainStream.Free isn't executed and you get a leak.
In case of a successful RecvStream(), then MainStream.Size should ALWAYS equal infile_size so the if should always be true.
If you really want to take connection interruption into account, then do this:
if MainStream.Size = StrToInt(infile_size) then begin // EXACTLY THE SAME SIZE
StatString:='>:Transfer Done!';
Synchronize(@ShowStat);
mode:='none';
Form1.ProgressLabelPercent.Caption:='100%';
Form1.ProgressBar.Position:=Form1.ProgressBar.Max;
Form1.ProgressLabelByte.Caption:=(infile_size+'/'+infile_size);
end
else begin
StatString:='>:TRANSFER FAILED!';
Synchronize(@ShowStat);
mode:='none';
end;
MainStream.free;
That way at least the MainStream is freed in both cases (and you get a neat message about the failure).
(also notice that the received size needs to be the exact size of infile_size. And not smaller or bigger.)