How should QLocalSocket/QDataStream be read?
I have a program that communicates with another via named pipes using QLocalSocket
and QDataStream
. The recieveMessage()
slot below is connected to the QLocalSocket
's readyRead()
signal.
void MySceneClient::receiveMessage()
{
qint32 msglength;
(*m_stream) >> msglength;
char* msgdata = new char[msglength];
int read = 0;
while (read < msglength) {
read += m_stream->readRawData(&msgdata[read], msglength - read);
}
...
}
I find that the application sometimes hangs on readRawData()
. That is, it succesfully reads the 4 byte header, but then never returns from readRawData()
.
If I add...
if (m_socket->bytesAvailable() < 5)
return;
...to the start of this function, the application works fine (with the short test message).
I am guessing then (the documentation is very sparse) that there is some sort of deadlock occurring, and that I must use the bytesAvailable()
signal to gradually build up the buffer rather than blocking.
Why is this? And what is the correct approach to reading from QLocalSocket?