0

I have a client in Qt that is continuously writing data using QTcpSocket->write( ) method.

In the other hand, my server uses QTcpSocket->readyRead() signal which is a connected to a slot that uses readAll() method to retrieve the information.

My problem is that sometimes the server is busy doing something else so when readAll is called, it returns a QString cointaining multiple received messages, all concatenated.

Since I can't change the message form (to add an explicit ending character for instance), is there a way to differenciate one message from the other? I've looked the documentation to see if maybe write function is adding an specific character at the end of the stream but couldn't find anything.

PS: I will handle it using the server only for receiving messages and putting them into a queue so there is less chances that it is busy when messages arrive but it is not a perfect solution!

Javi Ortiz
  • 568
  • 1
  • 7
  • 22
  • Thanks, it seems that the other question is referring more to what happens when the message is splitted. My case is like the opposite: What happens when various messages are concatenated. In addition and unfortunately, I can't change the message itself so the provided code is not useful for me. – Javi Ortiz Sep 22 '16 at 08:17
  • 2
    Correct, the question concerns split messages, but the answer discusses that and multiple messages arriving; ideally you need each message to be proceeded by its size, so you know how much to read when the messages arrive. If you can't change the stream, then unless the messages are of a standard, known size, you won't be able to differentiate between them. – TheDarkKnight Sep 22 '16 at 08:22
  • Can you change source code of your client? Or you have only binaries? – Kirill Chernikov Sep 22 '16 at 09:42
  • Yes, I think I can change it... Maybe I can send another message after the original one so I can use it as a separator or something like that – Javi Ortiz Sep 23 '16 at 06:12
  • TCP is a streaming protocol. On the receiving end all you are guaranteed is that you will get a chunk at a time. The size of the chunk is arbitrary. It can be one byte. Or it can be a megabyte. Or anything in between. Sometimes it can even be zero bytes. If the protocol was designed with for a message-oriented transport, it cannot be placed directly on top of TCP. You could use `QDataStream` and wrap each message in a `QByteArray`: that way you can separate them out on the receiving end easily by leveraging [read transactions](http://stackoverflow.com/a/39149727/1329652). – Kuba hasn't forgotten Monica Sep 23 '16 at 16:20

0 Answers0