void MainServer::incom() {
QTcpSocket * mySocket
= mainServer.
nextPendingConnection();
ServerThread * serverThread = new ServerThread(mySocket->socketDescriptor(), this);
// connect(serverThread, SIGNAL(finished()), ServerThread, SLOT(deleteLater()));
serverThread->start();
}
/* thread's constructor */
ServerThread
::ServerThread(int socketDescriptor,
QObject *parent
){
fd=socketDescriptor;
tcpSocket->setSocketDescriptor(fd);//according to Fortuneserverthread example
}
//run method is protected
void ServerThread::run()
{
connect(mySocket, SIGNAL(readyRead()), this, SLOT(dataRead()));
connect(this,
SIGNAL(writeLog
(QString,
int,
int,
int, Qt
::GlobalColor)),
(mServer
*)(this
->parent
()),
SLOT(writeLog
(const QString
&,
const int
&,
const int
&,
const int
&,
const Qt
::GlobalColor&)));
exec();
}
void FortuneServerThread::dataRead()
{
....//read data from socket and emit a signal for example:
... emit writeLog(str.append("User is in System"));
quit();
}
void MainServer::incom() {
QTcpSocket * mySocket = mainServer.nextPendingConnection();
ServerThread * serverThread = new ServerThread(mySocket->socketDescriptor(), this);
// connect(serverThread, SIGNAL(finished()), ServerThread, SLOT(deleteLater()));
serverThread->start();
}
/* thread's constructor */
ServerThread::ServerThread(int socketDescriptor,QObject *parent)
: QThread(parent)
{
fd=socketDescriptor;
mySocket = new QTcpSocket();
tcpSocket->setSocketDescriptor(fd);//according to Fortuneserverthread example
}
//run method is protected
void ServerThread::run()
{
connect(mySocket, SIGNAL(readyRead()), this, SLOT(dataRead()));
connect(this, SIGNAL(writeLog(QString, int, int, int, Qt::GlobalColor)), (mServer *)(this->parent()), SLOT(writeLog(const QString&, const int&, const int&, const int&, const Qt::GlobalColor&)));
exec();
}
void FortuneServerThread::dataRead()
{
....//read data from socket and emit a signal for example:
... emit writeLog(str.append("User is in System"));
quit();
}
To copy to clipboard, switch view to plain text mode
Bookmarks