Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //client .pro
- #-------------------------------------------------
- #
- # Project created by QtCreator 2017-02-06T16:50:14
- #
- #-------------------------------------------------
- QT += core gui network
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
- TARGET = client
- TEMPLATE = app
- # The following define makes your compiler emit warnings if you use
- # any feature of Qt which as been marked as deprecated (the exact warnings
- # depend on your compiler). Please consult the documentation of the
- # deprecated API in order to know how to port your code away from it.
- DEFINES += QT_DEPRECATED_WARNINGS
- # You can also make your code fail to compile if you use deprecated APIs.
- # In order to do so, uncomment the following line.
- # You can also select to disable deprecated APIs only up to a certain version of Qt.
- #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
- SOURCES += main.cpp\
- mainwindow.cpp
- HEADERS += mainwindow.h
- FORMS += mainwindow.ui
- // client .h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QMainWindow>
- #include <QTcpSocket>
- #include <QDataStream>
- #include <QMessageBox>
- #include <QHostAddress>
- namespace Ui {
- class MainWindow;
- }
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit MainWindow(QWidget *parent = 0);
- ~MainWindow();
- private slots:
- void on_btnConnect_clicked();
- void displayError(QAbstractSocket::SocketError socketError);
- void readSocket();
- void clientDisconnected();
- void on_btnSend_clicked();
- private:
- Ui::MainWindow *ui;
- QTcpSocket *tcpSocket;
- QDataStream in;
- };
- #endif // MAINWINDOW_H
- //client .cpp
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- tcpSocket = new QTcpSocket(this);
- in.setDevice(tcpSocket);
- in.setVersion(QDataStream::Qt_4_0);
- connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readSocket()));
- connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
- connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- void MainWindow::on_btnConnect_clicked()
- {
- tcpSocket->connectToHost(QHostAddress("127.0.0.1"), 5000);
- if (tcpSocket->waitForConnected()) {
- ui->lblState->setText("Connected to Server");
- }
- else{
- ui->lblState->setText("Failed to connect");
- }
- if (!tcpSocket->isValid()) {
- qDebug() << "tcp socket invalid";
- return;
- }
- if (!tcpSocket->isOpen()) {
- qDebug() << "tcp socket not open";
- return;
- }
- QByteArray block;
- QDataStream out(&block, QIODevice::WriteOnly);
- out.setVersion(QDataStream::Qt_4_0);
- out << ui->edtText->text();
- // QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
- // connect(clientSocket, &QAbstractSocket::disconnected,
- // clientConnection, &QObject::deleteLater);
- if (!tcpSocket->write(block)){
- QMessageBox::information(this, tr("Client"),tr("Could not send message"));
- }
- }
- void MainWindow::displayError(QAbstractSocket::SocketError socketError)
- {
- switch (socketError) {
- case QAbstractSocket::RemoteHostClosedError:
- break;
- case QAbstractSocket::HostNotFoundError:
- QMessageBox::information(this, tr("Fortune Client"),
- tr("The host was not found. Please check the "
- "host name and port settings."));
- break;
- case QAbstractSocket::ConnectionRefusedError:
- QMessageBox::information(this, tr("Fortune Client"),
- tr("The connection was refused by the peer. "
- "Make sure the fortune server is running, "
- "and check that the host name and port "
- "settings are correct."));
- break;
- default:
- QMessageBox::information(this, tr("Fortune Client"),
- tr("The following error occurred: %1.")
- .arg(tcpSocket->errorString()));
- }
- }
- void MainWindow::readSocket()
- {
- in.startTransaction();
- QString instring;
- in >> instring;
- if (in.commitTransaction()){
- ui->lblRecieved->setText(instring);
- }
- else{
- ui->lblRecieved->setText("Error reading from datastream");
- }
- }
- void MainWindow::on_btnSend_clicked()
- {
- if (!tcpSocket->isValid()) {
- qDebug() << "tcp socket invalid";
- return;
- }
- if (!tcpSocket->isOpen()) {
- qDebug() << "tcp socket not open";
- return;
- }
- QByteArray block;
- QDataStream out(&block, QIODevice::WriteOnly);
- out.setVersion(QDataStream::Qt_4_0);
- out << ui->edtText->text();
- // QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
- // connect(clientSocket, &QAbstractSocket::disconnected,
- // clientConnection, &QObject::deleteLater);
- if (!tcpSocket->write(block)){
- QMessageBox::information(this, tr("Client"),tr("Could not send message"));
- }
- // clientConnection->disconnectFromHost();
- }
- void MainWindow::clientDisconnected(){
- QMessageBox::information(this, tr("Client"),tr("Disconnected from Server"));
- }
- //client .ui
- <?xml version="1.0" encoding="UTF-8"?>
- <ui version="4.0">
- <class>MainWindow</class>
- <widget class="QMainWindow" name="MainWindow">
- <property name="geometry">
- <rect>
- <x>0</x>
- <y>0</y>
- <width>483</width>
- <height>305</height>
- </rect>
- </property>
- <property name="windowTitle">
- <string>MainWindow</string>
- </property>
- <widget class="QWidget" name="centralWidget">
- <widget class="QPushButton" name="btnSend">
- <property name="geometry">
- <rect>
- <x>320</x>
- <y>140</y>
- <width>80</width>
- <height>22</height>
- </rect>
- </property>
- <property name="text">
- <string>Send Text</string>
- </property>
- </widget>
- <widget class="QLineEdit" name="edtText">
- <property name="geometry">
- <rect>
- <x>20</x>
- <y>140</y>
- <width>291</width>
- <height>22</height>
- </rect>
- </property>
- </widget>
- <widget class="QLabel" name="lblRecieved">
- <property name="geometry">
- <rect>
- <x>20</x>
- <y>100</y>
- <width>331</width>
- <height>16</height>
- </rect>
- </property>
- <property name="text">
- <string>Recieved Text</string>
- </property>
- </widget>
- <widget class="QPushButton" name="btnConnect">
- <property name="geometry">
- <rect>
- <x>10</x>
- <y>20</y>
- <width>151</width>
- <height>31</height>
- </rect>
- </property>
- <property name="text">
- <string>Connect to Server</string>
- </property>
- </widget>
- <widget class="QLabel" name="lblState">
- <property name="geometry">
- <rect>
- <x>200</x>
- <y>30</y>
- <width>121</width>
- <height>16</height>
- </rect>
- </property>
- <property name="text">
- <string>State</string>
- </property>
- </widget>
- </widget>
- <widget class="QMenuBar" name="menuBar">
- <property name="geometry">
- <rect>
- <x>0</x>
- <y>0</y>
- <width>483</width>
- <height>19</height>
- </rect>
- </property>
- <widget class="QMenu" name="menuClient">
- <property name="title">
- <string>Client</string>
- </property>
- </widget>
- <addaction name="menuClient"/>
- </widget>
- <widget class="QToolBar" name="mainToolBar">
- <attribute name="toolBarArea">
- <enum>TopToolBarArea</enum>
- </attribute>
- <attribute name="toolBarBreak">
- <bool>false</bool>
- </attribute>
- </widget>
- <widget class="QStatusBar" name="statusBar"/>
- </widget>
- <layoutdefault spacing="6" margin="11"/>
- <resources/>
- <connections/>
- </ui>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement