Socket.ReceiveLength subtracting 4 bytes

1

Good evening guys,

I'm having problems on the receiving part of the file which is as follows:

The file is sent correctly to the server application with all the original content, it happens that when I'm debugging the server application with breakpoint and I put a

ShowMessage(IntToStr(Socket.ReceiveLenght));

If the original content that came from the txt file has for example 251 bytes, the message returns me the exact size (251) so far so good !, already when I assign

Socket.ReceiveLength the curSize variable (server side) debug tells me that only 247 bytes of content have been assigned.

I no longer know where to tinker with this code. Could someone give me an idea where I'm going wrong?

Below is the send and receive code, respectively.

// Código que envia o arquivo (aplicativo cliente)

var 

fs: TFileStream; 
size: integer; 
foname:string; 

begin 

foname:= 'c:\arquivo.txt'; 

   fs := TFileStream.Create(foname, fmOpenRead); 

try 
  size := fs.Size; 
  CS1.Socket.SendBuf(size, sizeOf(size)); 
  CS1.Socket.SendStream(fs); 
except 
  fs.Free; 
  exit; 
end; 

end; 


//Código que recebe o arquivo (aplicativo servidor)

var 

  fSize: integer = -1; 
  fs: TFileStream = nil; 

        procedure TForm1.SSLClientRead(Sender: TObject; Socket: TCustomWinSocket); 
        var 
        curSize: integer; 
        buf: pointer; 
        myDate: TDateTime; 
        myYear, myMonth, myDay: Word; 
        myHour, myMin, mySec, myMilli: Word; 

        begin 

        myDate := Now; 
        DecodeDateTime(myDate, myYear, myMonth, myDay, myHour, myMin, mySec, myMilli); 
        if Socket.ReceiveLength = 0 then 
        exit; 

        if fSize = -1 then 
        begin 
        Socket.ReceiveBuf(fSize, sizeOf(integer)); 
        end; 

        fs := TFileStream.Create('arquivo' + ' --- ' + IntToStr(myHour) + '-' + IntToStr(myMin) + '-' + IntToStr(MySec) + '.txt', fmCreate); 
        curSize := Socket.ReceiveLength; // aqui é onde está o problema 

        while curSize > 0 do 
        begin 
        if curSize + fs.Size > fSize then 
        curSize := fs.Size - fSize; 
        GetMem(buf, curSize); 
        try 
        curSize := Socket.ReceiveBuf(buf^, curSize); 
        if curSize <> - 1 then 
        begin 
        fs.Write(buf^, curSize); 
        end; 
        finally 
        FreeMem(buf); 
        end; 

        if (fs.Size = fSize) then 
        begin 
        fSize := -1; 
        fs.Free; 
        fs := nil; 
        Application.ProcessMessages; 
        if Socket.ReceiveLength = 0 then 
        begin 
        end; 
        end; 
        break; 
        end; 

        curSize := Socket.ReceiveLength; 
        end; 

 end; 
    
asked by anonymous 25.09.2015 / 03:44

0 answers