PDA

View Full Version : QTcpSocket speed problem



benelgiac
30th April 2007, 18:16
Hi! I have a speed problem with QTcpSocket...

I have to receive a series of variable-length packets in the classic tag-length-value format. That is, I have an header with the message type, its length and then the rest of the packet.

I am trying to use asynchronous sockets to read and store packets. I am not used to this approach because I have always used blocking sockets and threads, but anyway I would like to learn. This is my first approach (pseudo-code):


// This is the pseudo-code inside the readyRead() signal handling slot
// packet is a struct build imitating the structure of my packet.

QDataStream in(tcpSocket)
if (tcpSocket->bytesAvailable() < tag_size)
return;
in.readRawData((char*) &packet.tag, tag_size);

if (tcpSocket->bytesAvailable() < length_size)
return;
in.readRawData((char*) &packet.length, length_size);

if (tcpSocket->bytesAvailable() < packet.length)
return;
in.readRawData((char*) &packet.optionalPart, packet.length);

This is super-simplified... actually if the routines returns says at the second step ( tag read but length not available), the data already read from the buffer are kept and subsequent reads put the new data in queue to the buffer.

Anyway, this works fine but its slow! No packets are lost but the reception speed is so slow that the receiving buffer grows to hundreds of megabytes in a minute or two. Consider that I am trying to receive (in gross) something around 5000 packets per second, where each packet is in average 256 byte. What I get instead is around 100 packet per seconds.

Is that too much to handle with async socket?

Sorry for the pseudo-code but I am a little bit in a rush. If this topic is interesting for someone, I'll post some more details.

Thanks to anyone that is willing to give me some hints.

Bye!

jacek
30th April 2007, 20:31
What happens to the buffer if you simply discard the data after reading it?

benelgiac
30th April 2007, 22:01
Good question. I'll try it as soon as I get back to my workplace... I cannot access the server from here unfortunately.

After some more googling, I found some posts where people say that while handling readyRead(), after calling bytesAvailable(), it would be wise to read ALL the available data on the socket in one time. Does someone agree with that? Anyway, it sounds awkward to me: it would be like not having a tcp buffer at all.

Again, I'll try it in the next days. Ciao

marcel
30th April 2007, 22:03
Yes I agree, at least in theory. The socket buffer most likely is much smaller than 5000*256 bytes.

You could read it all( or less ) in a buffer and then parse it.

regards

Bitto
1st May 2007, 14:50
Oasn, very often performance problems in QTcpSocket are caused by one of the following


Not reading (QTcpSocket will reallocate to grow its internal buffer)
Reading with the QByteArray functions (a new bytearray is allocated for each packet)
Running network code in the same thread as intensive GUI code (a progress bar will prevent the socket from being read from while continuously drawing itself, which again causes the socket buffer to fill up and start rejecting packets)