This is how I send and receive files on Linux using Lazarus between two computer system. This code works as expected - sends and receives files. However, when I examine the received file
MyBigFile.dat, its content doesn't match the content in the original file. Where there is suppose to be strings, there is none. Where there is suppose to be Integer values, they are all zeros... But what is confusing is that the received file's
SIZE is exactly same as the sent file down to decimal point like 44.1 KiB. So, please can someone take a look at my code below and tell me what I am doing wrong? Thanks.
Receiving files:...
const
BufferSize = 1024;
var
RBufSize:Integer;
fs:TFileStream;
Buffer:Pointer;
...
fs := Tfilestream.Create('MyBigFile.dat',fmCreate);
try
GetMem(Buffer, BufferSize);
while True do
begin
RBufSize := FNet.Get(Buffer^,BufferSize, aSocket);
Sleep(70);
if (RBufSize = 0) then
Break
else
fs.Write(Buffer,RBufSize);
Application.ProcessMessages;
end;
finally
fs.Free;
FreeMem(Buffer);
end;
...
Sending Files:...
const
BufferSize = 1024;
var
BytesRead:Integer;
fs:TFileStream;
Buffer:Pointer;
...
fs := TFileStream.Create('MyBigFile.dat',fmOpenread);
try
GetMem(Buffer, BufferSize);
try
repeat
BytesRead := Fs.Read(Buffer^, BufferSize);
if BytesRead <=0 then
Break;
if aSocket.Connected then
asocket.Send(Buffer, BytesRead)
else
break;
sleep(30);
until false;
finally
FreeMem(Buffer);
end;
finally
Fs.Free;
end;