QTcpSocket transaction not working properly
-
I send data from my Android application written in Qt. I simply send messages from server (Android app) to client (desktop app) using this:
client->write(data);
My slot connected to ReadyRead was like this:
void Client::readResponse() { auto response = tcpSocket->readAll(); emit incomingMessage(response); }
I saw from debugger that data comes in 2 different reads which causes incomingMessage signal emitted twice. Therefore I decided to use transactions and changed code to this:
void Client::readResponse() { in.startTransaction(); QByteArray response; in >> response; if (!in.commitTransaction()) return; incomingMessage(response); }
But now, I cannot even read short messages. It was working with old code if message is short but It just doesn't work now. What do I do wrong?
Threads I found related to this:
https://forum.qt.io/topic/83969/how-to-handle-socket-transactions-uncommitted
https://forum.qt.io/topic/103794/qudpsocket-and-qdatastream
https://stackoverflow.com/questions/47740923/qtcpsocket-data-arrives-late -
When you want to use QDataStream on the reader side you also have to use it on the sender side.
-
Maybe this example might be helpful: https://wiki.qt.io/WIP-How_to_create_a_simple_chat_application