PDA

View Full Version : QTcpSocket readyRead and buffer size



pdoria
24th January 2008, 19:34
Hi all,

Does anyone know the size at which the buffer is filled when readyRead signal is emitted?

Thanks in advance.
Pedro Doria Meunier.

seveninches
25th January 2008, 04:25
As soon as there is something in the buffer (even if it's a single byte). Look in bool QAbstractSocketPrivate::_q_canReadNotification() in src/network/qabstractsocket.cp

pdoria
1st February 2008, 02:08
Txs for your answer.

I've got a situation here where I cannot determine whether it's the device tx scheme or the size of the socket read buffer than come into play... :confused:

The thing is:
The device reports n Bytes to be read (the physical thing).
and the readyRead gets fired at 255 bytes.

So when I try to parse the data I'm n bytes short.
Dirty solution:
In the method connected to the readyRead signal I compare the tcpSocket.bytesAvailable() to the # of bytes the device says it wants to transmit.
In a nutshell:

if (totalBytes < dataLength)
{
tcpSocket.waitForReadyRead(5000);
}

Bottom line (and question ;) ):
Is there anyway to wait until n Bytes are available in the buffer instead of n milliseconds?

Thanks in advance.
Pedro Doria Meunier.

aamer4yu
1st February 2008, 10:52
TCP is a stream based protocol... you will get data... but no fixed length.

to read N bytes... u can connect the readyRead to a slot, and have a static buffer there. When the bytes read equals N bytes, u can call some member function to process the N bytes.


MySocket::OnReadyRead()
{
static buffer[1024];
buffer = readAll();
if(buffer.lenght() >= m_N)
{
processNbytes(buffer);
buffer.clear();
}
}

Bitto
2nd February 2008, 11:11
It's 1000000 times better to just leave the data in the socket, and return. The next time this signal is fired, you'll probably have enough. So:



void MyClass::slotConnectedToReadyRead()
{
// Bad:
static QByteArray data; // static data is bad
data += readAll(); // unnecessary copy
if (data.size() < 256)
return;

// Good:
if (bytesAvailable() < 256)
return;
QByteArray data = read(256); // local variable is good
}