Socket.Receivelenght subtracting 4 bytes

Asked

Viewed 40 times

1

Good evening, everyone,

I’m having trouble 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 am debugging the server application with breakpoint and put a

ShowMessage(IntToStr(Socket.ReceiveLenght));

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

Socket.Receivelenght the variable curSize (server part) debug informs me that only 247 bytes of content have been assigned.

I don’t know where else to move this code. Could someone give me an idea where I’m going wrong?

Below is the sending and receiving 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; 
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.