Advertisement
Guest User

Untitled

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