Guest User

Untitled

a guest
Feb 6th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.81 KB | None | 0 0
  1. //server .pro
  2.  
  3. #-------------------------------------------------
  4. #
  5. # Project created by QtCreator 2017-02-06T16:50:39
  6. #
  7. #-------------------------------------------------
  8.  
  9. QT       += core gui network
  10.  
  11. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  12.  
  13. TARGET = server
  14. TEMPLATE = app
  15.  
  16. # The following define makes your compiler emit warnings if you use
  17. # any feature of Qt which as been marked as deprecated (the exact warnings
  18. # depend on your compiler). Please consult the documentation of the
  19. # deprecated API in order to know how to port your code away from it.
  20. DEFINES += QT_DEPRECATED_WARNINGS
  21.  
  22. # You can also make your code fail to compile if you use deprecated APIs.
  23. # In order to do so, uncomment the following line.
  24. # You can also select to disable deprecated APIs only up to a certain version of Qt.
  25. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
  26.  
  27.  
  28. SOURCES += main.cpp\
  29.         mainwindow.cpp
  30.  
  31. HEADERS  += mainwindow.h
  32.  
  33. FORMS    += mainwindow.ui
  34.  
  35.  
  36. //server .h
  37.  
  38. #ifndef MAINWINDOW_H
  39. #define MAINWINDOW_H
  40.  
  41. #include <QTcpServer>
  42. #include <QTcpSocket>
  43. #include <QDebug>
  44. #include <QMessageBox>
  45. #include <QMainWindow>
  46.  
  47. namespace Ui {
  48. class MainWindow;
  49. }
  50.  
  51. class MainWindow : public QMainWindow
  52. {
  53.     Q_OBJECT
  54.  
  55. public:
  56.     explicit MainWindow(QWidget *parent = 0);
  57.     ~MainWindow();
  58.  
  59. private slots:
  60.     void on_pushButton_clicked();
  61.     void newConn();
  62.     void readSocket();
  63.  
  64.     void on_btnSend_clicked();
  65.  
  66. private:
  67.     Ui::MainWindow *ui;
  68.  
  69.     bool serverStarted = false;
  70.     QTcpServer *tcpServer;
  71.     QTcpSocket *clientSocket = nullptr;
  72.     QDataStream in;
  73. };
  74.  
  75. #endif // MAINWINDOW_H
  76.  
  77.  
  78. //server .cpp
  79.  
  80. #include "mainwindow.h"
  81. #include "ui_mainwindow.h"
  82.  
  83. MainWindow::MainWindow(QWidget *parent) :
  84.     QMainWindow(parent),
  85.     ui(new Ui::MainWindow)
  86. {
  87.     ui->setupUi(this);
  88.     tcpServer = new QTcpServer(this);
  89.  
  90. }
  91.  
  92. MainWindow::~MainWindow()
  93. {
  94.     delete ui;
  95. }
  96.  
  97. void MainWindow::on_pushButton_clicked()
  98. {
  99.     if (!serverStarted) {
  100.  
  101.         if (!tcpServer->listen(QHostAddress::AnyIPv4, 5000)) {
  102.             ui->lblServerStatus->setText("Failed to start server");
  103.         }
  104.         serverStarted = true;
  105.         ui->pushButton->setText("Stop Server");
  106.         ui->lblServerStatus->setText("Server Running");
  107.         connect(tcpServer, SIGNAL(newConnection()),
  108.                 this, SLOT(newConn()));
  109.     }
  110.     else{
  111.         clientSocket->disconnectFromHost();
  112.         tcpServer->close();
  113.         serverStarted = false;
  114.         ui->pushButton->setText("Start Server");
  115.         ui->lblServerStatus->setText("Server not running");
  116.     }
  117. }
  118.  
  119. void MainWindow::newConn(){
  120.     ui->lblServerStatus->setText("New Connection Recieved");
  121.     clientSocket = tcpServer->nextPendingConnection();
  122.  
  123.     connect(clientSocket, SIGNAL(readyRead()),
  124.             this, SLOT(readSocket()));
  125.  
  126.     qDebug() << "ip = " << clientSocket->peerAddress().toString();
  127.     qDebug() << "name = " << clientSocket->peerName();
  128.     qDebug() << "port = " << QString::number(clientSocket->peerPort());
  129.  
  130. }
  131.  
  132.  
  133. void MainWindow::on_btnSend_clicked()
  134. {
  135.     if (clientSocket != nullptr) {
  136.         if (!clientSocket->isValid()) {
  137.             qDebug() << "tcp socket invalid";
  138.             return;
  139.         }
  140.         if (!clientSocket->isOpen()) {
  141.             qDebug() << "tcp socket not open";
  142.             return;
  143.         }
  144.  
  145.         QByteArray block;
  146.         QDataStream out(&block, QIODevice::WriteOnly);
  147.         out.setVersion(QDataStream::Qt_4_0);
  148.  
  149.         out << ui->lineEdit->text();
  150.  
  151. //        QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
  152. //        connect(clientSocket, &QAbstractSocket::disconnected,
  153. //                clientConnection, &QObject::deleteLater);
  154.  
  155.         if (!clientSocket->write(block)){
  156.             QMessageBox::information(this, tr("Server"),tr("Could not send message"));
  157.         }
  158. //        clientConnection->disconnectFromHost();
  159.  
  160.     }
  161. }
  162.  
  163. void MainWindow::readSocket(){
  164.     in.setDevice(clientSocket);
  165.     in.setVersion(QDataStream::Qt_4_0);
  166.  
  167.     in.startTransaction();
  168.  
  169.     QString nextFortune;
  170.     in >> nextFortune;
  171.  
  172.     if (in.commitTransaction())
  173.         ui->lblOut->setText(nextFortune);
  174. }
  175.  
  176.  
  177. //server .ui
  178.  
  179. <?xml version="1.0" encoding="UTF-8"?>
  180. <ui version="4.0">
  181.  <class>MainWindow</class>
  182.  <widget class="QMainWindow" name="MainWindow">
  183.   <property name="geometry">
  184.    <rect>
  185.     <x>0</x>
  186.     <y>0</y>
  187.     <width>499</width>
  188.     <height>217</height>
  189.    </rect>
  190.   </property>
  191.   <property name="windowTitle">
  192.    <string>MainWindow</string>
  193.   </property>
  194.   <widget class="QWidget" name="centralWidget">
  195.    <widget class="QPushButton" name="pushButton">
  196.     <property name="geometry">
  197.      <rect>
  198.       <x>20</x>
  199.       <y>20</y>
  200.       <width>80</width>
  201.       <height>22</height>
  202.      </rect>
  203.     </property>
  204.     <property name="text">
  205.      <string>Start Server</string>
  206.     </property>
  207.    </widget>
  208.    <widget class="QPushButton" name="btnSend">
  209.     <property name="geometry">
  210.      <rect>
  211.       <x>310</x>
  212.       <y>100</y>
  213.       <width>80</width>
  214.       <height>22</height>
  215.      </rect>
  216.     </property>
  217.     <property name="text">
  218.      <string>Send Text</string>
  219.     </property>
  220.    </widget>
  221.    <widget class="QLabel" name="lblOut">
  222.     <property name="geometry">
  223.      <rect>
  224.       <x>20</x>
  225.       <y>60</y>
  226.       <width>331</width>
  227.       <height>16</height>
  228.      </rect>
  229.     </property>
  230.     <property name="text">
  231.      <string>Recieved Text</string>
  232.     </property>
  233.    </widget>
  234.    <widget class="QLineEdit" name="lineEdit">
  235.     <property name="geometry">
  236.      <rect>
  237.       <x>10</x>
  238.       <y>100</y>
  239.       <width>291</width>
  240.       <height>22</height>
  241.      </rect>
  242.     </property>
  243.    </widget>
  244.    <widget class="QLabel" name="lblServerStatus">
  245.     <property name="geometry">
  246.      <rect>
  247.       <x>120</x>
  248.       <y>20</y>
  249.       <width>231</width>
  250.       <height>16</height>
  251.      </rect>
  252.     </property>
  253.     <property name="text">
  254.      <string>Server Status</string>
  255.     </property>
  256.    </widget>
  257.   </widget>
  258.   <widget class="QMenuBar" name="menuBar">
  259.    <property name="geometry">
  260.     <rect>
  261.      <x>0</x>
  262.      <y>0</y>
  263.      <width>499</width>
  264.      <height>19</height>
  265.     </rect>
  266.    </property>
  267.    <widget class="QMenu" name="menuServer">
  268.     <property name="title">
  269.      <string>Server</string>
  270.     </property>
  271.    </widget>
  272.    <addaction name="menuServer"/>
  273.   </widget>
  274.   <widget class="QToolBar" name="mainToolBar">
  275.    <attribute name="toolBarArea">
  276.     <enum>TopToolBarArea</enum>
  277.    </attribute>
  278.    <attribute name="toolBarBreak">
  279.     <bool>false</bool>
  280.    </attribute>
  281.   </widget>
  282.   <widget class="QStatusBar" name="statusBar"/>
  283.  </widget>
  284.  <layoutdefault spacing="6" margin="11"/>
  285.  <resources/>
  286.  <connections/>
  287. </ui>
Add Comment
Please, Sign In to add comment