Hi All,
So I am trying out a file transfer from linux to windows using QTcpSocket.
I read the file in small “chunks” on the linux machine and append a checksum to the end of each chunk. Only when the receiving computer receives the data and and finds the checksums to match, do I send the the next chunk. If I send over a text file, it comes through perfectly except when there are special characters. When I try and send an image file, it is corrupted even though the checksums always match perfectly.
Below is a snippet of how I send my info across the socket. I have removed some of the overhead, such as checking the data read from the file is valid and open file is successful and such. I stripped it down to show the basics:
void AddCHSM(QByteArray &In)
{
unsigned int CHKSM = 0;
for (int i = 0; i < In.size(); i++)
{
CHKSM += (unsigned int)In[i];
}
char CheckSumMSB= (char)(CHKSM >> 8);
char CheckSumLSB = (char)CHKSM;
In.append(CheckSumMSB);
In.append(CheckSumLSB);
}
void SendChunkFunction()
{
int ChunkSize = 2500;
QByteArray FileContents;
QFile *Filer = new QFile("tree.jpg");
FileContents = Filer->read(ChunkSize);
AddCHSM(FileContents);
Sock->write(FileContents);
}
On the receiving side:
bool CheckCHSM(QByteArray ArIn)
{
unsigned int CHKSM = 0;
for (int i = 0; i < ArIn.size()-2; i++)
{
CHKSM += (unsigned int) ArIn[i];
}
char CheckSumMSB = (char)(CHKSM >> 8);
char CheckSumLSB = (char)CHKSM;
char ReceivedCheckSumMSB = (char) ArIn[ArIn.size()-2];
char ReceivedCheckSumLSB = (char) ArIn[ArIn.size()-1];
if ((CheckSumLSB == ReceivedCheckSumLSB) && (CheckSumMSB == ReceivedCheckSumMSB) )
return true;
return false;
}
void DataReceived()
{
QByteArray Ar;
Ar = Sock->readAll();
if (CheckCHSM(Ar))
{
//Good message received
Ar = Ar.left(Ar.size()-2);
QFile *File = new QFile("tree.jpg");
if(!File->open(QIODevice::ReadWrite))
{
Sock->write(FATAL_ERROR);
return;
}
File->seek(File->size());
File->write(Ar);
File->close();
Sock->write(GOOD_DATA_RECEIVED);
}
Once again I have stripped the code down to its bare essentials. I currently make sure the file is empty at the beginning of the code, so the problem is not that I have rogue information in the file before starting.
I compare the two files in Notepad++ after running the code and they are the same size and look like they have the same characters. The file header is correct. But the are a few characters different.
Are there any ideas as to what is happening?
Thank you for your help!
Jermcb
↧