View Full Version : QTCPSocket not getting all data

28th January 2010, 23:02
I have a somewhat strange problem, which may not really be a qt problem, but might have more to do with my my understanding of tcp

However, I have not seen it when programming sockets using the standard socket API under Linux

I have a linux application that launches a TCP server. It waits for a data request message, and when it gets it, sends

1) a data start message, containing the number of bytes to be transferred
2) a series of lines of ascii text, totalling the number of bytes described in the initial message.

my QT application (running on windows XP, QT version 4.6) always receives the data request message--but does not get all of the line by line data. here's my receiving code:

int totalBytesRead = 0;
int byteCount;
bytesToRead = totalBytesToRead;
massiveArray = (char *)malloc(bytesToRead * sizeof(char));

// here is where I neeed to open a temporary file and write this data out
tempIniFile = new QTemporaryFile;
char *theTempFile = strdup((char *)tempIniFile->fileName().toAscii().data());

while(totalBytesRead < totalBytesToRead){
byteCount = iniClient->readLine(massiveArray,bytesToRead);

totalBytesRead += byteCount;
bytesToRead -= byteCount;

byteCount will eventually stop being anything but 0--sometimes after 10 lines of data, sometimes after 200, but once it returns zero, I never get anything else from the socket.

My understanding is that if the reading TCP buffers are full, the sender will block unitl it can write again--I get no errors there, it always shows a successful number of bytes written. Unless I misunderstand, this means they are successfully transferred to the client--but my software isn't getting the data.

Do I need to implement some handshaking after each line?

Any other ideas?

Thanks in advance

Jonathan Howland

28th January 2010, 23:24
Why don't you connect to the signal readyRead() that is sent by the QTcpSocket?

Btw... have you noticed there exists a QByteArray...? ;)

29th January 2010, 15:53
Why don't you connect to the signal readyRead() that is sent by the QTcpSocket?

Btw... have you noticed there exists a QByteArray...? ;)

Thanks! The snippet I sent was actually within a readyRead(), and that was the problem---I was reading all the available bytes from the readyread()--I had to get more readyRead() signals to read more bytes, but I was stuck in my slot function, so never got them.

I got rid of all the byte counting, and now just signal the end of the transmission. Its much cleaner code, and it actually seems to work.

And as far as QByteArrays...in reality, my roots are in C programming--well, to be honest, they're in Fortran--, and when I start flailing, I revert to C habits. My new code uses a QByteArray to read the new data--no mallocs, honest!

29th January 2010, 15:59
no mallocs, honest!
OK, we're friends again! ;)

29th January 2010, 16:20
Just to add:
Large data packages don't has to be sent at once through TCP, so you can receive your data in many packages (every time readyRead() will be emitted) so just save expected data size (from that first 'special' package) and then in in slot on readyRead check with bytesAvailable() how many bytes received. If you got less then you are waiting for, then save all the data in some buffer (QByteArray or write directly to file but you can withour readLine() just write what readAll() returns), substract bytesAvailable from all required bytes count and keep waiting for another readyRead() and so on until you get all required data. Then you have complete package in your buffer.