QT服务器多线程编程
1、ServerWorkThread类:创建QTcpSocket,用于向客户端传回服务器信息
#include <QThread>
#include <QtNetwork>
#include <QTcpSocket>
class ServerWorkThread:public QThread
{
Q_OBJECT
Public:
ServerWorkThread(int socketDescriptor, QObject* parent=0);
void run();
signals:
private:
int m_socketDescriptor;
};
#include <QByteArray>
#include <QDataStream>
ServerWorkThread ::ServerWorkThread(int socketDescriptor, QObject* parent)
:QThread(parent), m_socketDescriptor(socketDescriptor)
{
}
void ServerWorkThread ::run()
{
QTcpScocket tcpSocket;
if(!tcpSocket.setSocketDescriptor(m_socketDescriptor))
{
return;
}
QByteArray block;
QDataStream out(&block, QIODevice