Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Last active February 7, 2021 06:48
Show Gist options
  • Select an option

  • Save lamprosg/4593408 to your computer and use it in GitHub Desktop.

Select an option

Save lamprosg/4593408 to your computer and use it in GitHub Desktop.
Qt - QTcpServer using QThreadPool (The way to go of making a server)
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)
#include <QThread>
#include "myserver.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 <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:
};
#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
}
#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:
};
@kiseokko
Copy link
Copy Markdown

for Qt naming rule, "MyServer::startServer" is better than "MyServer::StartServer"

@curtwagner1984
Copy link
Copy Markdown

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.

@brucezcg
Copy link
Copy Markdown

brucezcg commented Feb 7, 2021

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