1. 程式人生 > >基於QT的多執行緒伺服器

基於QT的多執行緒伺服器

// thread.cpp

#include"thread.h"
Thread::Thread(intsocketDescriptor,QObject*parent)
:QThread(parent)
{
m_socketDescriptor=socketDescriptor;
}
Thread::~Thread()
{
}
voidThread::run()
{
m_tcpSocket=newQTcpSocket;
if(!m_tcpSocket->setSocketDescriptor(m_socketDescriptor))
{
emiterror(m_tcpSocket->error());
return;
}
connect(m_tcpSocket,SIGNAL(readyRead()),this,SLOT(readData_slot()),Qt::DirectConnection);//Qt::DirectConnection執行緒內傳遞消
connect(m_tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(tcpSocket_error_slot()));
connect(m_tcpSocket,SIGNAL(disconnected()),this,SLOT(quit()));//socket斷開連線,執行緒退出
qDebug()<<"run"<<m_tcpSocket->peerAddress().toString();
qDebug()<<"run"<<m_tcpSocket->peerPort();
exec();
}
voidThread::readData_slot()
{
QDataStreamin(m_tcpSocket);
in.setVersion(QDataStream::Qt_5_0);
QDataStreamout(m_tcpSocket);
out.setVersion(QDataStream::Qt_5_0);
QString
data;
in>>data;
qDebug()<<"readData_slot"<<data;
data="hellothisisserver";
out<<data;
qDebug()<<data;
}
voidThread::tcpSocket_error_slot()
{
qDebug()<<m_tcpSocket->errorString();
}

// server.cpp

#include"server.h"
Server::Server(QObject*parent)
:QTcpServer(parent)
{
}
Server::~Server()
{
}
voidServer::incomingConnection(qintptrsocketDescriptor)
{
Thread*thread=newThread(socketDescriptor,this);
connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater()));
thread->start();
}

// dialog.cpp

#include"dialog.h"
Dialog::Dialog(QWidget*parent)
:QWidget(parent)
{
m_statusLabel=newQLabel;
m_statusLabel->setWordWrap(true);
m_quitButton=newQPushButton(tr("Quit"));
m_quitButton->setAutoDefault(false);
QStringipAddress;
QList<QHostAddress>ipAddressesList=QNetworkInterface::allAddresses();
//usethefirstnon-localhostIPv4address
for(inti=0;i<ipAddressesList.size();++i)
{
if(ipAddressesList.at(i)!=QHostAddress::LocalHost&&
ipAddressesList.at(i).toIPv4Address())
{
ipAddress=ipAddressesList.at(i).toString();
break;
}
}
//ifwedidnotfindone,useIPv4localhost
if(ipAddress.isEmpty())
{
ipAddress=QHostAddress(QHostAddress::LocalHost).toString();
}
m_statusLabel->setText(tr("Theserverisrunningon\n\nIP:%1\nport:%2\n\n"
"RuntheClientexamplenow.")
.arg(ipAddress).arg(m_server.serverPort()));
connect(m_quitButton,SIGNAL(clicked()),this,SLOT(close()));
QHBoxLayout*buttonLayout=newQHBoxLayout;
buttonLayout->addStretch(1);
buttonLayout->addWidget(m_quitButton);
buttonLayout->addStretch(1);
QVBoxLayout*mainLayout=newQVBoxLayout;
mainLayout->addWidget(m_statusLabel);
mainLayout->addLayout(buttonLayout);
setLayout(mainLayout);
setWindowTitle(tr("ThreadedServer"));
if(!m_server.listen(QHostAddress::Any,1314))
{
QMessageBox::critical(this,tr("ThreadedServer"),
tr("Unabletostarttheserver:%1.")
.arg(m_server.errorString()));
close();
return;
}
}
Dialog::~Dialog()
{
}
這是我參照http://m.blog.csdn.net/blog/sdu_sky/8104175這位大神的部落格和QT的多執行緒伺服器原始碼寫出來的,

但是我又兩個地方不明白,在dialog.cpp中

if(!m_server.listen(QHostAddress::Any,1314))
這一句怎樣就能在監聽成功之後建立一個新的執行緒,
voidServer::incomingConnection(qintptrsocketDescriptor)
還有這個函式沒有在建構函式裡實現,也沒有顯式的呼叫???這是怎麼回事,望高手指導