PDA

View Full Version : QTcpSocket problem



MSUdom5
23rd February 2010, 12:38
I'm an experienced Qt developer, but this is first time I've tried to use QTcpSocket. I've got a server (not written in Qt) that sends me 40kB of data upon request. I'm pretty certain the server works ok because I have clients written in other languages that work with the server without any problems. However, I'm trying to write a client using QTcpSocket, and I'm having some trouble. I establish the connection, request the 40kB of data from the server, but I'm only getting 1460 bytes. I've verified that this data is, in fact, the first 1460 bytes of the 40kB packet. I've used different combinations of bytesAvailable(), waitForReadyRead(), etc. to wait for the rest of the data, but it never comes. If I tell the server to send 1455 bytes, the client indeed receives 1455 bytes. If I tell the server to send 1465 bytes, however, I still only get 1460. Just an FYI: I'm using a QByteArray to store data on the client end, not that that adds any useful information.

Anyone know what could be causing this?

Thanks,
MSUdom5

mrvk
23rd February 2010, 13:20
Did you connect to the readyRead() signal which is emitted when the new data is available. Once the first packet of data is read, you should be receiving this signal again where in you can read the next data packet.

mcosta
23rd February 2010, 13:26
TCP protocol provides "stream decomposition" in packets with a maximum size; in your case 1460.
In general you have to enter in a loop to receive entire packet.

Can you post your Qt code?

MSUdom5
23rd February 2010, 15:23
Thanks for the feedback. Using the readyRead signal fixed my problem.

I made the mistake of assuming that the read() function would read all incoming packets until it hit a timeout condition. (Which is how some other libraries work.) Basically, I fixed my problem by using:



QByteArray packet;
while(socket.waitForReadyRead(timeout))
packet += socket.read(maxbytes);


Thanks again for the help,
MSUdom5