I want to detect how many bytes are available for reading:
while( socket->ConnectedState == QTcpSocket::ConnectedState ) {
if(socket->bytesAvailable() > 0)
qDebug("%d", test->bytesAvailable());
}
I'm sure the socket is connected, but it seems the return value of byteAvailable
is always 0 no matter how many bytes I wrote from the other end.
But when I detect after disconnection:
if (! socket->waitForDisconnected(-1)) {
qDebug("Failed to receive disconnect message from client");
}
else {
qDebug("%d", socket->bytesAvailable());
qDebug(socket->readAll());
qDebug("Disconnected from client");
}
}
Information sent to debug window is reasonable: 8 ABCDEFGH
.
What may cause this to happen ?
PS:
This also seems to be a dead loop as it keeps running even after I disconnect from the other end. Maybe cause by this reason: QTcpSocket state always connected, even unplugging ethernet wire
QRunnable
thanQThread
, is there event loop inQRunnable
? – CDT