PDA

View Full Version : QTcpSocket server keeps reading the messages from the client



saifulkhan
24th October 2013, 03:07
void FortuneThread::run()
{
QTcpSocket tcpSocket;
if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
emit error(tcpSocket.error());
return;
}

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));

tcpSocket.write(block);

// Here !!

tcpSocket.disconnectFromHost();
tcpSocket.waitForDisconnected();
}


Say, in the fortune thread example, after sending (write) the text to the client, I want this particular thread to keep alive and keep reading different messages sent from the client, until client closes the connection (I guess by when client will invoke QTcpSocket ::abort() or QTcpSocket::disconnectfromHost()). Can you please suggest me how can I do this?

Thanks

anda_skoa
24th October 2013, 08:51
waitForReadyRead?

scarecr0w132
24th October 2013, 10:35
I would use this http://www.bogotobogo.com/Qt/Qt5_QTcpServer_Multithreaded_Client_Server.php
And do
tcpSocket.disconnectFromHost();
tcpSocket.waitForDisconnected();
from client

wysota
24th October 2013, 11:11
I would use this http://www.bogotobogo.com/Qt/Qt5_QTcpServer_Multithreaded_Client_Server.php
And do
tcpSocket.disconnectFromHost();
tcpSocket.waitForDisconnected();
from client

I wouldn't. The tutorial is wrong.

spirit
24th October 2013, 11:24
I wouldn't. The tutorial is wrong.
So agree with you.

toufic.dbouk
24th October 2013, 13:47
The tutorial is wrong.

So agree with you.
May you explain why is that tutorial wrong ?