31 lines
811 B
C++
31 lines
811 B
C++
|
|
#include "ThreadedServerConnection.h"
|
||
|
|
|
||
|
|
ThreadedServerConnection::ThreadedServerConnection(int socketDescriptor, const QString &fortune, QObject *parent) :
|
||
|
|
QThread(parent), socketDescriptor(socketDescriptor), text(fortune)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
void ThreadedServerConnection::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_5_1);
|
||
|
|
//out << (quint16)0;
|
||
|
|
out << text;
|
||
|
|
//out.device()->seek(0);
|
||
|
|
//out << (quint16)(block.size() - sizeof(quint16));
|
||
|
|
//! [3] //! [4]
|
||
|
|
tcpSocket.write(block);
|
||
|
|
tcpSocket.disconnectFromHost();
|
||
|
|
tcpSocket.waitForDisconnected();
|
||
|
|
}
|