QtcpSocket without signals/slots
-
01-07-2021 - |
Question
Is it possible to use QTcpSocket without using readyRead slots ?
I tried this code for reading datas from a tcp server :
QByteArray ba;
forever
{
sck.waitForReadyRead();
ba.append( sck.readAll() );
if (sck.bytesAvailable() == 0)
break;
}
but sometimes I don't receive all the datas from server !?
Solution
Yes, it is possible. TCP is stream-oriented, you can't exit from the iteration like that. You have an example in QAbstractSocket documentation.
As you can see, you'll have to keep reading until the connection is closed (the waitForReadyRead() method returns false) or until you send some termination sequence, that is up to you.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow