Last active
January 16, 2017 17:27
-
-
Save lamprosg/4587087 to your computer and use it in GitHub Desktop.
Qt - Multithreaded 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
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 |
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" | |
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 "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 | |
} |
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> | |
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 | |
}; |
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 "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); | |
} |
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 <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
Caution: When you make a thread per socket, you're in a very high risk of crashing your server