部分代码:
void serverThread::slotStart()
{
this->listen(QHostAddress::Any, 2000);//服务器监听端口
}
void serverThread::incomingConnection(qintptr socketDescriptor)
{
MySocket *psocket = new MySocket(socketDescriptor);
QThread *pThread = new QThread(this);
connect(psocket, SIGNAL(updatWidget(QByteArray)), this, SLOT(slotUpdate(QByteArray)));
connect(psocket, SIGNAL(readyRead()), psocket, SLOT(slotDealRecvData()));
connect(psocket, SIGNAL(disconnected()), psocket, SLOT(deleteLater()));
connect(psocket, SIGNAL(disconnected()), pThread, SLOT(quit()));
psocket->moveToThread(pThread);
pThread->start();
}
MySocket::MySocket(int socketdesc, QTcpSocket *parent )
{
this->setSocketDescriptor(socketdesc);//设置socket描述符
connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(SocketErr(QAbstractSocket::SocketError)));
}
MySocket::~MySocket()
{
}
void MySocket::slotDealRecvData()
{
MySocket *socket = (MySocket*)sender();
QByteArray array = this->readAll();
emit updatWidget(array);
}
完整源码在资源里