Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. // myserver.h
  2.  
  3. #ifndef MYSERVER_H
  4. #define MYSERVER_H
  5.  
  6. #include <QTcpServer>
  7.  
  8. class MyServer : public QTcpServer
  9. {
  10. Q_OBJECT
  11. public:
  12. explicit MyServer(QObject *parent = 0);
  13. void startServer();
  14. signals:
  15.  
  16. public slots:
  17.  
  18. protected:
  19. void incomingConnection(qintptr socketDescriptor);
  20.  
  21. };
  22.  
  23. #endif // MYSERVER_H
  24.  
  25. #ifndef MYTHREAD_H
  26. #define MYTHREAD_H
  27.  
  28. #include <QThread>
  29. #include <QTcpSocket>
  30. #include <QDebug>
  31.  
  32. class MyThread : public QThread
  33. {
  34. Q_OBJECT
  35. public:
  36. explicit MyThread(qintptr ID, QObject *parent = 0);
  37.  
  38. void run();
  39.  
  40. signals:
  41. void error(QTcpSocket::SocketError socketerror);
  42.  
  43. public slots:
  44. void readyRead();
  45. void disconnected();
  46.  
  47. private:
  48. QTcpSocket *socket;
  49. qintptr socketDescriptor;
  50. };
  51.  
  52. #endif // MYTHREAD_H
  53.  
  54. #include "myserver.h"
  55. #include "mythread.h"
  56.  
  57. MyServer::MyServer(QObject *parent) :
  58. QTcpServer(parent)
  59. {
  60. }
  61.  
  62. void MyServer::startServer()
  63. {
  64. int port = 1234;
  65.  
  66. if(!this->listen(QHostAddress::Any, port))
  67. {
  68. qDebug() << "Could not start server";
  69. }
  70. else
  71. {
  72. qDebug() << "Listening to port " << port << "...";
  73. }
  74. }
  75.  
  76. // This function is called by QTcpServer when a new connection is available.
  77. void MyServer::incomingConnection(qintptr socketDescriptor)
  78. {
  79. // We have a new connection
  80. qDebug() << socketDescriptor << " Connecting...";
  81.  
  82. // Every new connection will be run in a newly created thread
  83. MyThread *thread = new MyThread(socketDescriptor, this);
  84.  
  85. // connect signal/slot
  86. // once a thread is not needed, it will be beleted later
  87. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
  88.  
  89. thread->start();
  90. }
  91.  
  92. #include "mythread.h"
  93.  
  94. MyThread::MyThread(qintptr ID, QObject *parent) :
  95. QThread(parent)
  96. {
  97. this->socketDescriptor = ID;
  98. }
  99.  
  100. void MyThread::run()
  101. {
  102. // thread starts here
  103. qDebug() << " Thread started";
  104.  
  105. socket = new QTcpSocket();
  106.  
  107. // set the ID
  108. if(!socket->setSocketDescriptor(this->socketDescriptor))
  109. {
  110. // something's wrong, we just emit a signal
  111. emit error(socket->error());
  112. return;
  113. }
  114.  
  115. // connect socket and signal
  116. // note - Qt::DirectConnection is used because it's multithreaded
  117. // This makes the slot to be invoked immediately, when the signal is emitted.
  118.  
  119. connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
  120. connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
  121.  
  122. // We'll have multiple clients, we want to know which is which
  123. qDebug() << socketDescriptor << " Client connected";
  124.  
  125. // make this thread a loop,
  126. // thread will stay alive so that signal/slot to function properly
  127. // not dropped out in the middle when thread dies
  128.  
  129. exec();
  130. }
  131.  
  132. void MyThread::readyRead()
  133. {
  134. // get the information
  135. QByteArray Data = socket->readAll();
  136.  
  137. // will write on server side window
  138. qDebug() << socketDescriptor << " Data in: " << Data;
  139.  
  140. socket->write(Data);
  141. }
  142.  
  143. void MyThread::disconnected()
  144. {
  145. qDebug() << socketDescriptor << " Disconnected";
  146.  
  147.  
  148. socket->deleteLater();
  149. exit(0);
  150. }
  151.  
  152. #include <QCoreApplication>
  153. #include "myserver.h"
  154.  
  155. int main(int argc, char *argv[])
  156. {
  157. QCoreApplication a(argc, argv);
  158.  
  159. // Make a server and starts it
  160. MyServer server;
  161. server.startServer();
  162.  
  163. return a.exec();
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement