* * *

Author Topic: Sending and Receiving files Once Again...  (Read 481 times)

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 288
  • Programming is FUN only when it works :)
    • Cool Technology
Sending and Receiving files Once Again...
« on: April 25, 2017, 03:15:52 pm »
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:

Code: Pascal  [Select]
  1. ...
  2. const
  3.    BufferSize = 1024;
  4. var
  5.    RBufSize:Integer;
  6.    fs:TFileStream;
  7.    Buffer:Pointer;
  8. ...
  9.        fs := Tfilestream.Create('MyBigFile.dat',fmCreate);
  10.        
  11.        try
  12.           GetMem(Buffer, BufferSize);
  13.  
  14.           while True do
  15.           begin
  16.              RBufSize := FNet.Get(Buffer^,BufferSize, aSocket);
  17.  
  18.              Sleep(70);
  19.  
  20.              if (RBufSize = 0) then  
  21.                 Break
  22.              else
  23.                 fs.Write(Buffer,RBufSize);
  24.              
  25.              Application.ProcessMessages;
  26.           end;
  27.  
  28.        finally
  29.              fs.Free;
  30.              FreeMem(Buffer);
  31.        end;
  32. ...
  33.  


Sending Files:

Code: Pascal  [Select]
  1. ...
  2. const
  3.    BufferSize = 1024;
  4. var
  5.    BytesRead:Integer;
  6.    fs:TFileStream;
  7.    Buffer:Pointer;
  8. ...
  9.        fs := TFileStream.Create('MyBigFile.dat',fmOpenread);
  10.  
  11.        try
  12.           GetMem(Buffer, BufferSize);
  13.           try
  14.              repeat
  15.                 BytesRead := Fs.Read(Buffer^, BufferSize);
  16.  
  17.                 if BytesRead <=0 then
  18.                    Break;
  19.  
  20.                 if aSocket.Connected then
  21.                    asocket.Send(Buffer, BytesRead)
  22.                 else
  23.                    break;
  24.  
  25.                  sleep(30);
  26.              until false;
  27.  
  28.           finally
  29.               FreeMem(Buffer);
  30.           end;
  31.        finally
  32.               Fs.Free;
  33.        end;
  34.  
« Last Edit: April 25, 2017, 04:06:07 pm by Awesome Programmer »

Remy Lebeau

  • Sr. Member
  • ****
  • Posts: 257
    • Lebeau Software
Re: Sending and Receiving files Once Again...
« Reply #1 on: April 25, 2017, 08:51:40 pm »
Code: Pascal  [Select]
  1. fs.Write(Buffer,RBufSize);

Should be this instead:

Code: Pascal  [Select]
  1. fs.Write(Buffer^,RBufSize);
  2. // Or better:
  3. // fs.WriteBuffer(Buffer^,RBufSize);

The important thing is adding the '^' after 'Buffer', otherwise you are passing the wrong memory address to Write.  You are lucky your code didn't crash.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) open source project - Admin, Developer

Awesome Programmer

  • Sr. Member
  • ****
  • Posts: 288
  • Programming is FUN only when it works :)
    • Cool Technology
Re: Sending and Receiving files Once Again...
« Reply #2 on: April 26, 2017, 07:56:56 pm »
Code: Pascal  [Select]
  1. fs.Write(Buffer,RBufSize);

Should be this instead:

Code: Pascal  [Select]
  1. fs.Write(Buffer^,RBufSize);
  2. // Or better:
  3. // fs.WriteBuffer(Buffer^,RBufSize);

The important thing is adding the '^' after 'Buffer', otherwise you are passing the wrong memory address to Write.  You are lucky your code didn't crash.

OMG!!! It works... :) Thanks a lot, Remi.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus