Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // myserver.h
- #ifndef MYSERVER_H
- #define MYSERVER_H
- #include <QTcpServer>
- class MyServer : public QTcpServer
- {
- Q_OBJECT
- public:
- explicit MyServer(QObject *parent = 0);
- void startServer();
- signals:
- public slots:
- protected:
- void incomingConnection(qintptr socketDescriptor);
- };
- #endif // MYSERVER_H
- #ifndef MYTHREAD_H
- #define MYTHREAD_H
- #include <QThread>
- #include <QTcpSocket>
- #include <QDebug>
- class MyThread : public QThread
- {
- Q_OBJECT
- public:
- explicit MyThread(qintptr ID, QObject *parent = 0);
- void run();
- signals:
- void error(QTcpSocket::SocketError socketerror);
- public slots:
- void readyRead();
- void disconnected();
- private:
- QTcpSocket *socket;
- qintptr socketDescriptor;
- };
- #endif // MYTHREAD_H
- #include "myserver.h"
- #include "mythread.h"
- MyServer::MyServer(QObject *parent) :
- QTcpServer(parent)
- {
- }
- void MyServer::startServer()
- {
- int port = 1234;
- if(!this->listen(QHostAddress::Any, port))
- {
- qDebug() << "Could not start server";
- }
- else
- {
- qDebug() << "Listening to port " << port << "...";
- }
- }
- // This function is called by QTcpServer when a new connection is available.
- void MyServer::incomingConnection(qintptr socketDescriptor)
- {
- // We have a new connection
- qDebug() << socketDescriptor << " Connecting...";
- // Every new connection will be run in a newly created thread
- MyThread *thread = new MyThread(socketDescriptor, this);
- // connect signal/slot
- // once a thread is not needed, it will be beleted later
- connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
- thread->start();
- }
- #include "mythread.h"
- MyThread::MyThread(qintptr ID, QObject *parent) :
- QThread(parent)
- {
- this->socketDescriptor = ID;
- }
- void MyThread::run()
- {
- // thread starts here
- qDebug() << " Thread started";
- socket = new QTcpSocket();
- // set the ID
- if(!socket->setSocketDescriptor(this->socketDescriptor))
- {
- // something's wrong, we just emit a signal
- emit error(socket->error());
- return;
- }
- // connect socket and signal
- // note - Qt::DirectConnection is used because it's multithreaded
- // This makes the slot to be invoked immediately, when the signal is emitted.
- connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
- connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
- // We'll have multiple clients, we want to know which is which
- qDebug() << socketDescriptor << " Client connected";
- // make this thread a loop,
- // thread will stay alive so that signal/slot to function properly
- // not dropped out in the middle when thread dies
- exec();
- }
- void MyThread::readyRead()
- {
- // get the information
- QByteArray Data = socket->readAll();
- // will write on server side window
- qDebug() << socketDescriptor << " Data in: " << Data;
- socket->write(Data);
- }
- void MyThread::disconnected()
- {
- qDebug() << socketDescriptor << " Disconnected";
- socket->deleteLater();
- exit(0);
- }
- #include <QCoreApplication>
- #include "myserver.h"
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- // Make a server and starts it
- MyServer server;
- server.startServer();
- return a.exec();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement