PDA

View Full Version : How to modify "Threaded Fortune Server Example" in order to...



cydside
27th November 2011, 11:15
...recieve from client the fortune phrase's index(an integer data es. 'i' for QStringList.at[i]) and the server to answer with the requested phrase at index 'i'.
Could you indicate, in your experience, a way to approach to that problem?
Do I need to re-implement the QTcpSocket class in thread method run()?


void FortuneThread::run()
{
QTcpSocket tcpSocket;
//! [1] //! [2]
if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
emit error(tcpSocket.error());
return;
}
//! [2] //! [3]

QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << text;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
//! [3] //! [4]

tcpSocket.write(block);
tcpSocket.disconnectFromHost();
tcpSocket.waitForDisconnected();
}
//! [4]

What is the best solution to read data from socket and then write?

I really appreciate your help, thanks.