Last active
February 7, 2021 06:48
-
-
Save lamprosg/4593408 to your computer and use it in GitHub Desktop.
Qt - QTcpServer using QThreadPool
(The way to go of making a server)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Classes: | |
| MyServer, with Base class QTcpServer | |
| MyRunnable (this is a QRunnable task), with Base class QRunnable. (With no QObject, cause it's not a QObject) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <QThread> | |
| #include "myserver.h" | |
| int main(int argc, char *argv[]) | |
| { | |
| QCoreApplication a(argc, argv); | |
| MyServer server; | |
| server.StartServer(); | |
| return a.exec(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <QRunnable> | |
| #include <QTcpSocket> | |
| class MyRunnable : public QRunnable | |
| { | |
| Q_OBJECT | |
| public: | |
| MyRunnable(); | |
| int socketDescriptor; | |
| protected: | |
| void run(); //This is not a thread. This is a task executed on a thread, in a threadpool | |
| signals: | |
| public slots: | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "myserver.h" | |
| MyServer::MyServer(QObject *parent): | |
| QTcpServer(parent) | |
| { | |
| //Create a threadpool | |
| pool = new QThreadPool(this); | |
| //How many threads I want at any given time | |
| //If there are more connections, they will be qued until a threads is closed | |
| pool->setMaxThreadCount(5); // ex. 5 threads | |
| } | |
| void MyServer::StartServer() | |
| { | |
| if (!this->listen(QHostAddress::Any, 1234)) | |
| { | |
| QMessageBox::critical(this, tr("Server"), | |
| tr("Unable to start the server: %1.") | |
| .arg(this->errorString())); | |
| } | |
| else | |
| { | |
| //Server is now listening.. | |
| } | |
| } | |
| void MyServer::incomingConnection(int handle) //Handle incoming connections | |
| { | |
| MyRunnable *task = new MyRunnable(); | |
| task->setAutoDelete(true); //Delete that object when you're done (instead of using signals and slots) | |
| task->socketDescriptor = handle; | |
| pool->start(task); //Uses a QRunnable object | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <QTcpServer> | |
| #include <QThreadPool> | |
| #include "myrunnable.h" | |
| class MyServer : public QTcpServer | |
| { | |
| Q_OBJECT | |
| public: | |
| explicit MyServer(QObject *parent=0); | |
| //Starts the server | |
| void StartServer(); | |
| protected: | |
| void incomingConnection(int handle); //handle is the socket descriptor | |
| private: | |
| QThreadPool *pool; | |
| signals: | |
| public slots: | |
| }; |
Are you sure?
QRunnable is not inherited from QObject.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This will auto disconnect from every client that connects to it. Every client that connects will get the 'Hello World" message and then get disconnected.
It would be great if the QRunnable would maintain a connection to the client until the client says he wants to close it.