In the fortuneserver sample of Qt, a QString is sent by the method sendFortune()
. Therefore one QString is selected from the QStringList fortunes
:
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << fortunes.at(qrand() % fortunes.size());
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
clientConnection->write(block);
Is it also possible to send another type of data, like files, images or multiple strings? Or is it just possible to send a single string?
My second question: What is the advantage of sending data as QByteArry and why do I have to define (quint16)
by setting up the QDataStream?