void NewConnectionThread::run(){ clientSocket = parentLocalServerRef->getServerRef()->nextPendingConnection(); // connect(clientSocket, SIGNAL(readyRead()), this, SLOT(read())); while (clientSocket->bytesAvailable() < (int)sizeof(quint32)) { clientSocket->waitForReadyRead(1); } connect(clientSocket, SIGNAL(disconnected()), clientSocket, SLOT(deleteLater())); QByteArray newClientName = clientSocket->peek(30); qDebug() << " New client name : " << newClientName << "\n"; QString currentDate = parentLocalServerRef->getCurrentDate(); qDebug() << " Sending to " << newClientName << " current date : " << currentDate << "\n"; QByteArray response = currentDate.toLatin1(); clientSocket->write(response); clientSocket->flush(); qDebug() << " Finish sending. Close the Thread."; stopProcess = true; //while (!stopProcess) {}}
NewConnectionThread::NewConnectionThread(LocalServer* parentLocalServerRef, QObject *parent) : QThread(parent), parentLocalServerRef(parentLocalServerRef){ stopProcess = false;}void NewConnectionThread::run(){ clientSocket = parentLocalServerRef->getServerRef()->nextPendingConnection(); connect(clientSocket, SIGNAL(readyRead()), this, SLOT(read())); //while (!stopProcess) {}}void NewConnectionThread::read(){ connect(clientSocket, SIGNAL(disconnected()), clientSocket, SLOT(deleteLater())); QByteArray newClientName = clientSocket->peek(30); qDebug() << " New client name : " << newClientName << "\n"; QString currentDate = parentLocalServerRef->getCurrentDate(); qDebug() << " Sending to " << newClientName << " current date : " << currentDate << "\n"; QByteArray response = currentDate.toLatin1(); clientSocket->write(response); clientSocket->flush(); qDebug() << " Finish sending. Close the Thread."; stopProcess = true;}
_________localserver.h#ifndef LOCALSERVER_H#define LOCALSERVER_H#include <QObject>#include <QString>#include <QLocalServer>#include <QMutex>#include <QDateTime>class LocalServer : public QObject{ Q_OBJECT QString pipeName; QLocalServer* localServer; QDateTime currentDate; QMutex mutex;public: explicit LocalServer(QString pipeName = "pipeJAkutenshi", QObject *parent = 0); QLocalServer *getServerRef(); QString getCurrentDate();signals:public slots: void newLocalSocketConnection();};#endif // LOCALSERVER_H_______localserver.cpp#include "localserver.h"#include "newconnectionthread.h"LocalServer::LocalServer(QString pipeName, QObject *parent) : QObject(parent), pipeName(pipeName){ localServer = new QLocalServer(); localServer->listen(pipeName); qDebug() << "Server pipe" << pipeName << "is started and waiting for new clients...\n"; connect(localServer, SIGNAL(newConnection()), this, SLOT(newLocalSocketConnection()));}QLocalServer *LocalServer::getServerRef() { return localServer; }QString LocalServer::getCurrentDate(){ mutex.lock(); currentDate = QDateTime::currentDateTime(); QString stringCurrentDate = currentDate.toString("dd.MM.yyyy hh:mm:ss"); mutex.unlock(); return stringCurrentDate;}void LocalServer::newLocalSocketConnection(){ qDebug() << "New client!\n"; NewConnectionThread* newClientConnection = new NewConnectionThread(this); newClientConnection->start(); qDebug() << "Server is created new Thread and waiting for new clients...\n";}_________newconnectionthread.h#ifndef NEWCONNECTIONTHREAD_H#define NEWCONNECTIONTHREAD_H#include <QThread>#include "localserver.h"class NewConnectionThread : public QThread{ Q_OBJECT QLocalSocket* clientSocket; bool stopProcess;public: explicit NewConnectionThread(LocalServer* parentLocalServerRef, QObject *parent = 0); void run(); LocalServer* parentLocalServerRef;signals:public slots: void read();};#endif // NEWCONNECTIONTHREAD_H_________newconnectionthread.cppNewConnectionThread::NewConnectionThread(LocalServer* parentLocalServerRef, QObject *parent) : QThread(parent), parentLocalServerRef(parentLocalServerRef){ stopProcess = false;}void NewConnectionThread::run(){ clientSocket = parentLocalServerRef->getServerRef()->nextPendingConnection(); connect(clientSocket, SIGNAL(readyRead()), this, SLOT(read())); //while (!stopProcess) {}}void NewConnectionThread::read(){ connect(clientSocket, SIGNAL(disconnected()), clientSocket, SLOT(deleteLater())); QByteArray newClientName = clientSocket->peek(30); qDebug() << " New client name : " << newClientName << "\n"; QString currentDate = parentLocalServerRef->getCurrentDate(); qDebug() << " Sending to " << newClientName << " current date : " << currentDate << "\n"; QByteArray response = currentDate.toLatin1(); clientSocket->write(response); clientSocket->flush(); qDebug() << " Finish sending. Close the Thread."; stopProcess = true;}_________main.cpp#include <QCoreApplication>#include "localserver.h"int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); LocalServer* server = new LocalServer(); return a.exec();}
C++ (Qt)QThread *moveToThread (QObject *object) { QThread *thread = new QThread; object->moveToThread (thread); QObject::connect (thread, SIGNAL (finished ()), object, SLOT (deleteLater ())); QObject::connect (thread, SIGNAL (finished ()), thread, SLOT (deleteLater ())); thread->start (); return thread; }