You assume that non-trivial data sent in one write() is received in one block. This is rarely the case. For example at mainwindow.cpp line 47 you assume you have 10 bytes to read as a byte count in text format; you may not have 10 bytes here. Then you assume the remaining bytes are a complete payload, you may have no bytes, part of the byte count plus payload, all of a payload, or even all of a payload plus the start of another transmission (I didn't look at your server).
You need to buffer data in response to readyRead() signals until you know you have a complete protocol data unit and then decode it.
Bookmarks