This problem occured when I was downloading 0.5MB file from embedded device. Every ~16kB there were missing 9 bytes. It seems that slower devices can not prepare full buffer, like the code was supposing. So, SCP_ReceiveFile method should be corrected like this:
protected void SCP_ReceiveFile(Stream server, string rfile, string lfile, int size)
{
int copied = 0;
SendStartMessage(rfile, lfile, size, "Connected, starting transfer.");
// read a content of lfile
//FileStream fos = File.OpenWrite(lfile);
FileStream fos = File.Create(lfile);
int foo;
int filesize = size;
var buf = new byte[1024];
while (!_mCancelled)
{
if (buf.Length < filesize) foo = buf.Length;
else foo = filesize;
int len = server.Read(buf, 0, foo);
copied += len;
fos.Write(buf, 0, len); // it was: fos.Write(buf, 0, foo);
SendProgressMessage(rfile, lfile, copied, size, "Transferring...");
filesize -= len; // it was: filesize -= foo;
if (filesize == 0) break;
}
fos.Close();
if (_mCancelled)
return;
SCP_CheckAck(server);
SendEndMessage(rfile, lfile, copied, size, "Transfer completed successfuly (" + filesize + " bytes).");
}
Notice that value foo is always 1024 (buffer length), but the actual received data is len and does not have to be 1024.
Mr. Zdukic,
This turned out to be very helpful!
Thank you for sharing.
Best regards,
<Johan>