QT += coreQT += networkQT -= guiTARGET = PoolSrvCONFIG += consoleCONFIG -= app_bundleTEMPLATE = appSOURCES += main.cpp \ myserver.cpp \ myrunnable.cppHEADERS += \ myserver.h \ myrunnable.h
#ifndef MYRUNNABLE_H#define MYRUNNABLE_H#include <QRunnable>#include <QTcpSocket>#include <QDebug>class MyRunnable : public QRunnable{public: explicit MyRunnable(); int SocketDescriptor;protected: void run(); };#endif // MYRUNNABLE_H
#ifndef MYSERVER_H#define MYSERVER_H#include <QTcpServer>#include <QThreadPool>#include <QDebug>#include "myrunnable.h"class MyServer : public QTcpServer{ Q_OBJECTpublic: explicit MyServer(QObject *parent = 0); void StartServer();protected: void incomingConnection(int handle);signals: public slots:private: QThreadPool * pool; };#endif // MYSERVER_H
#include <QtCore/QCoreApplication>#include "myserver.h"#include "myrunnable.h"int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); MyServer Server; Server.StartServer(); return a.exec();}
#include "myrunnable.h"MyRunnable::MyRunnable() :{}void MyRunnable::run(){ if(!SocketDescriptor) return; QTcpSocket socket; socket.setSocketDescriptor(SocketDescriptor); socket.write("hello world"); socket.flush(); socket.waitForBytesWritten(); socket.close();}
#include "myserver.h"MyServer::MyServer(QObject *parent) : QTcpServer(parent){ pool = new QThreadPool(this); pool->setMaxThreadCount(5);}void MyServer::StartServer(){ if(this->listen(QHostAddress::Any,1234)) { qDebug() << "Server started"; } else { qDebug() << "Server did not start!"; }}void MyServer::incomingConnection(int handle){ MyRunnable *task = new MyRunnable(); task->setAutoDelete(true); task->SocketDescriptor = hendle; pool->start(task);}
#error "123"#include "myserver.h"MyServer::MyServer(QObject *parent) : QTcpServer(parent){ pool = new QThreadPool(this); pool->setMaxThreadCount(5);}void MyServer::StartServer(){ if(this->listen(QHostAddress::Any,1234)) { qDebug() << "Server started"; } else { qDebug() << "Server did not start!"; }}void MyServer::incomingConnection(int handle){ MyRunnable *task = new MyRunnable(); task->setAutoDelete(true); task->SocketDescriptor = hendle; pool->start(task);}
//MyServer Server; //Server.StartServer();
C++ (Qt)MyRunnable::MyRunnable() : QRunnable (){}
C++ (Qt)void MyServer::incomingConnection(int handle){ MyRunnable *task = new MyRunnable(); task->setAutoDelete(true); task->SocketDescriptor = handle; pool->start(task);}
// MyServer Server;// Server.StartServer();