Guest User

Untitled

a guest
Mar 18th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. #ifndef MYSERVER_H
  2. #define MYSERVER_H
  3.  
  4. #include <QMainWindow>
  5. #include <QTcpServer>
  6. #include <QTcpSocket>
  7.  
  8. class myServer: public QTcpServer
  9. {
  10. Q_OBJECT
  11.  
  12. public:
  13. myServer();
  14. ~myServer();
  15.  
  16. public:
  17.  
  18. QTcpSocket* socket;
  19. QByteArray Data;
  20. MainWindow* instance = MainWindow::GetInstance();
  21.  
  22. public slots:
  23. void startServer();
  24. void incomingConnection(int socketDescriptor);
  25. void sockReady();
  26. void sockDisc();
  27. };
  28.  
  29. #endif // MYSERVER_H
  30.  
  31. #include "myserver.h"
  32. #include "mainwindow.h"
  33. #include "ui_mainwindow.h"
  34. #include <QWidget>
  35. #include <QGraphicsItem>
  36.  
  37. myServer::myServer(){}
  38. myServer::~myServer(){}
  39.  
  40. void myServer::startServer()
  41. {
  42. if(this->listen(QHostAddress::Any,5555))
  43. {
  44. qDebug()<<"LISTEN";
  45. }
  46. else{
  47. qDebug()<<"NOT LISTEN";
  48. }
  49. }
  50.  
  51. void myServer::incomingConnection(int socketDescriptor)
  52. {
  53. socket = new QTcpSocket(this);
  54. socket->setSocketDescriptor(socketDescriptor);
  55.  
  56. connect(socket,SIGNAL(readyRead()),this,SLOT(sockReady()));
  57. connect(socket,SIGNAL(disconnected()),this,SLOT(sockDisc()));
  58.  
  59. qDebug()<<socketDescriptor<< "Client connected";
  60. socket->write("You are connect");
  61. qDebug()<<"Send client connect status - YES";
  62. }
  63.  
  64. void myServer::sockReady()
  65. {
  66. instance->drawConnection();
  67. instance->drawingenemyLife();
  68. instance->show();
  69. }
  70.  
  71. void myServer::sockDisc()
  72. {
  73. qDebug()<<"DISCONECT";
  74. instance->drawDisConnection();
  75. instance->show();
  76. socket->deleteLater();
  77. }
  78.  
  79. void MainWindow::mousePressEvent(QMouseEvent *ev)
  80. {
  81. //Здесь надо вызвать socket->write("SOME TEXT");
  82. }
  83.  
  84. class MainWindow : public QMainWindow
  85. {
  86. Q_OBJECT
  87. ...
  88. signals:
  89. void messageCreated(const QString& str);
  90. ...
  91. };
  92.  
  93. class myServer : public QObject
  94. {
  95. Q_OBJECT
  96. ...
  97. public slots:
  98. void slotSendMessage(const QString& str)
  99. {
  100. socket->write(str.toUtf8())
  101. }
  102. ...
  103. };
  104.  
  105. void MainWindow::mousePressEvent(QMouseEvent* ev)
  106. {
  107. emit messageCreated("Very important message");
  108. }
  109.  
  110. MainWindow w;
  111. myServer s;
  112. ...
  113. QObject::connect(&m, &MainWindow::messageCreated, &s, &myServer::slotSendMessage);
Add Comment
Please, Sign In to add comment