I'm about to write a small server using QTcpServer
which is intended to be connected by e. g. a telnet client. Only text will be sent.
Qt's fortune cookie server example uses a QDataStream
to send text via the following code using a QTcpSocket
:
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_5_10);
out << "Some Text";
socket->write(block);
If one does not want to send binary data, is there any drawback to send Unicode data directly (everyone involved knows we're using UTF-8) like so:
socket->write(QString::fromUtf8("Some text").toUtf8);
?
There are no drawbacks to sending UTF-8 data directly, but you need to have some means of separating the packets (units) of data on the receiving end. If it's all text, it'd be done by splitting the incoming data into lines at the receiving end. QDataStream
doesn't care about any of it and, at the wire level, sends a 32-bit length of the sent string of bytes, followed by those bytes. So there's never any ambiguity as to how long the packet's payload is, no matter its contents: it could even be 0-bytes long.