Skip to content

Instantly share code, notes, and snippets.

@codeyash
Forked from lamprosg/Information
Created March 29, 2016 18:41
Show Gist options
  • Save codeyash/03a239044fd9071e23fc69e991357e25 to your computer and use it in GitHub Desktop.
Save codeyash/03a239044fd9071e23fc69e991357e25 to your computer and use it in GitHub Desktop.
Qt - Multithreaded Server
Create 2 classes.
One class for the server:
We call it ex. MyServer, with Base class QTcpServer
Second class, every connection will have a new thread:
We call it MyThread, with Base class QThread
#include "myserver.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyServer server;
server.StartServer();
return a.exec();
}
#include "myserver.h"
#include "mythread.h"
MyServer::MyServer(QObject *parent):
QTcpServer(parent)
{
}
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::incomingConnections(int socketDescriptor) //Incoming connections
{
MyThread *thread = new MyThread(socketDescriptor,this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
//Start a new thread for the connection
thread->start(); //Which will cause the run() function
}
#include <QTcpServer>
class MyServer : public QTcpServer
{
Q_OBJECT
public:
explicit MyServer(QObject *parent=0);
//Start the server
void StartServer();
signals:
public slots:
protected:
void incomingConnections(int socketDescriptor); //This is where we deal with incoming connections
};
#include "mythread.h"
MyThread::MyThread(int ID, QObject *parent):
QThread(parent)
{
this->socketDescriptor = ID; //Get the socket ID number
}
void MyThread::run()
{
//thread starts here
socket = new QTcpSocket();
if ( !socket->setSocketDescriptor(this->socketDesriptor) ) //Here we set the socket ID
{
emit error (socket->error()); //emit the error signal
return;
}
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()),Qt::DirectConnection ); //Make a direct connection to the thread
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
//client is connected
//IMPORTANT
//This function will cause the thread to stay alive until we tell it to close
//Otherwise the run() function will end and the thread will be dropped / destroyed
exec();
}
void MyThread::readyRead()
{
QByteArray Data = socket->readAll(); //Get all the information from the connected client
//Send the info back, (echo server)
socket->write(Data);
}
void MyThread::disconnected()
{
socket->deleteLater();
exit(0);
}
#include <QThread>
#include <QTcpSocket>
class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(int ID, QObject *parent =0); //We added an ID to the constructor (For the socket ID number)
//Just because it's a thread we will ad a run function. This will run when the socket connects.
//Basically we're overiding the QThread run function
void run();
signals:
void error(QTcpSocket::SocketError socketerror); //If something goes wrong we'll emit that
public slots:
void readyRead();
void disconnected();
private:
QTcpSocket *socket;
int socketDescriptor;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment