Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QTcpSocket client receiving more than one message at once
Audiocrow last edited by
Each message I send from my server application has an ID which I use to identify how the client is supposed to read the message. For example, message 0 might be intended for the client to load data into a specific widget and message 1 might be for a different widget. I don't confirm that the client received a message before sending others, though. Will my client only receive one message at a time even if it's being received in blocks? If not, how can I keep track of which blocks are meant for which messages?
andre last edited by
TCP offers a reliable connection between two systems, so you don't need to send ACK messages. Of course, the connection may experience problems, but if there is a connection, you can be confident your data will be fine. Your data will also arrive in the same order as it was send. However, it is not guaranteed how that data is segmented. It is up to you to make sure that you split it into the appropriate messages again, if that is what you are sending out.
There are several ways of doing that. You can give each message a simple header, with a message type and a message length. If you receive data, you can check if the remaining data is of the right length before you continue to read it. If so, read to the end of the message, and send it on to whatever widget is interested in it. Alternatively, you can use some kind of separator sequence that you put between each message, and that you know won't be in your actual data. That way, you don't need to keep track of the message lenght, you only need to look for your separator.
Another alternative is to not do all this stuff manually at all, and just use a ready-made solution like the classes in "Qxt's network":http://libqxt.bitbucket.org/doc/0.6/qxtnetwork.html module. They have RPC solutions that allow you to just use signals and slots across a network connection.