Forum > Networking and Web Programming

Sending and Receiving files Once Again...

(1/1)

Awesome Programmer:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---...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:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---...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; 

Remy Lebeau:

--- Quote from: Awesome Programmer on April 25, 2017, 03:15:52 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---fs.Write(Buffer,RBufSize);
--- End quote ---

Should be this instead:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---fs.Write(Buffer^,RBufSize);// Or better:// 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.

Awesome Programmer:

--- Quote from: Remy Lebeau on April 25, 2017, 08:51:40 pm ---
--- Quote from: Awesome Programmer on April 25, 2017, 03:15:52 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---fs.Write(Buffer,RBufSize);
--- End quote ---

Should be this instead:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---fs.Write(Buffer^,RBufSize);// Or better:// 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.

--- End quote ---

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

Navigation

[0] Message Index

Go to full version