PDA

View Full Version : Send exe file over QTcpSocket



scarecr0w132
18th October 2013, 09:45
Hello,

I am trying to send an EXE file over QTcpSocket, but it comes back with the wrong data.
I have researched and think it is because it only is getting the first "packet"?
I'm new to Tcp sockets should I use QDataStream?


Server side:

...
QByteArray byteArray;
QFile file("test.exe");
if(file.open(QIODevice::ReadOnly)) {
byteArray = file.readAll();
}
file.close();
socket->write(byteArray);
...

Client side:

...
int bytesReceived = 0;
readyRead(){
QByteArray byteArray;
if (tcpSocket->waitForReadyRead()) {
bytesReceived += (int)tcpSocket->bytesAvailable();
byteArray += tcpSocket->readAll();
qDebug() << byteArray.size();
qDebug() << "recieved: " + QString::number(bytesReceived);

updateFile = new QFile("test.exe");
if(updateFile->open(QIODevice::WriteOnly)) {
qDebug() << updateFile->write(byteArray);
}
}
...


Output:

87600
"recieved: 87600"
87600
131072
"recieved: 218672"
131072
131072
"recieved: 349744"
131072
131072
"recieved: 480816"
131072
131072
"recieved: 611888"
131072
131072
"recieved: 742960"
131072
131072
"recieved: 874032"
131072
131072
"recieved: 1005104"
131072
131072
"recieved: 1136176"
131072
129488
"recieved: 1265664"
129488

test.exe in written to file but is 129,488 bytes when it should be 1265664.
So server writes the correct data to client but client interprets it wrong?

Thank you for any help

anda_skoa
18th October 2013, 11:08
You only wait for the first ready read so you obviously only get the data that has been recieved until that point.

If the sender closes the socket after sending you could try waiting for disconnected or waiting for readyRead in a loop until it the socket is closed

Cheers,
_

scarecr0w132
20th October 2013, 03:38
You only wait for the first ready read so you obviously only get the data that has been recieved until that point.

If the sender closes the socket after sending you could try waiting for disconnected or waiting for readyRead in a loop until it the socket is closed

Cheers,
_

Hi anda_skoa,

I don't understand what you mean.
The sender doesn't close any connections.

Thanks

wysota
20th October 2013, 09:51
The sender doesn't close any connections.

It's about the receiver, not the sender. You're expecting all data to magically be teleported to the receiving machine and only then the receiving application be notified about the data. That's not the case, if you have N bytes of data then it takes some time to send it, it takes some time for it to be transported and it takes some time to be received. The receiving application is notified about the incoming data not when the N'th byte arrives but rather when the first byte arrives. Thus if you "read all" then you only read the part that has already been received (usually up to one TCP receiving window, which seems to be 128kB on your system).

anda_skoa
20th October 2013, 13:32
I don't understand what you mean.
The sender doesn't close any connections.


Well, in that case you will need a different way for the receiver to know that all data has been received.
For example by sending the number of bytes first.

Cheers,
_

scarecr0w132
21st October 2013, 00:07
Fixed
thankyou