Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //server .pro
- #-------------------------------------------------
- #
- # Project created by QtCreator 2017-02-06T16:50:39
- #
- #-------------------------------------------------
- QT += core gui network
- greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
- TARGET = server
- 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
- //server .h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
- #include <QTcpServer>
- #include <QTcpSocket>
- #include <QDebug>
- #include <QMessageBox>
- #include <QMainWindow>
- namespace Ui {
- class MainWindow;
- }
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
- public:
- explicit MainWindow(QWidget *parent = 0);
- ~MainWindow();
- private slots:
- void on_pushButton_clicked();
- void newConn();
- void readSocket();
- void on_btnSend_clicked();
- private:
- Ui::MainWindow *ui;
- bool serverStarted = false;
- QTcpServer *tcpServer;
- QTcpSocket *clientSocket = nullptr;
- QDataStream in;
- };
- #endif // MAINWINDOW_H
- //server .cpp
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- tcpServer = new QTcpServer(this);
- }
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- void MainWindow::on_pushButton_clicked()
- {
- if (!serverStarted) {
- if (!tcpServer->listen(QHostAddress::AnyIPv4, 5000)) {
- ui->lblServerStatus->setText("Failed to start server");
- }
- serverStarted = true;
- ui->pushButton->setText("Stop Server");
- ui->lblServerStatus->setText("Server Running");
- connect(tcpServer, SIGNAL(newConnection()),
- this, SLOT(newConn()));
- }
- else{
- clientSocket->disconnectFromHost();
- tcpServer->close();
- serverStarted = false;
- ui->pushButton->setText("Start Server");
- ui->lblServerStatus->setText("Server not running");
- }
- }
- void MainWindow::newConn(){
- ui->lblServerStatus->setText("New Connection Recieved");
- clientSocket = tcpServer->nextPendingConnection();
- connect(clientSocket, SIGNAL(readyRead()),
- this, SLOT(readSocket()));
- qDebug() << "ip = " << clientSocket->peerAddress().toString();
- qDebug() << "name = " << clientSocket->peerName();
- qDebug() << "port = " << QString::number(clientSocket->peerPort());
- }
- void MainWindow::on_btnSend_clicked()
- {
- if (clientSocket != nullptr) {
- if (!clientSocket->isValid()) {
- qDebug() << "tcp socket invalid";
- return;
- }
- if (!clientSocket->isOpen()) {
- qDebug() << "tcp socket not open";
- return;
- }
- QByteArray block;
- QDataStream out(&block, QIODevice::WriteOnly);
- out.setVersion(QDataStream::Qt_4_0);
- out << ui->lineEdit->text();
- // QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
- // connect(clientSocket, &QAbstractSocket::disconnected,
- // clientConnection, &QObject::deleteLater);
- if (!clientSocket->write(block)){
- QMessageBox::information(this, tr("Server"),tr("Could not send message"));
- }
- // clientConnection->disconnectFromHost();
- }
- }
- void MainWindow::readSocket(){
- in.setDevice(clientSocket);
- in.setVersion(QDataStream::Qt_4_0);
- in.startTransaction();
- QString nextFortune;
- in >> nextFortune;
- if (in.commitTransaction())
- ui->lblOut->setText(nextFortune);
- }
- //server .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>499</width>
- <height>217</height>
- </rect>
- </property>
- <property name="windowTitle">
- <string>MainWindow</string>
- </property>
- <widget class="QWidget" name="centralWidget">
- <widget class="QPushButton" name="pushButton">
- <property name="geometry">
- <rect>
- <x>20</x>
- <y>20</y>
- <width>80</width>
- <height>22</height>
- </rect>
- </property>
- <property name="text">
- <string>Start Server</string>
- </property>
- </widget>
- <widget class="QPushButton" name="btnSend">
- <property name="geometry">
- <rect>
- <x>310</x>
- <y>100</y>
- <width>80</width>
- <height>22</height>
- </rect>
- </property>
- <property name="text">
- <string>Send Text</string>
- </property>
- </widget>
- <widget class="QLabel" name="lblOut">
- <property name="geometry">
- <rect>
- <x>20</x>
- <y>60</y>
- <width>331</width>
- <height>16</height>
- </rect>
- </property>
- <property name="text">
- <string>Recieved Text</string>
- </property>
- </widget>
- <widget class="QLineEdit" name="lineEdit">
- <property name="geometry">
- <rect>
- <x>10</x>
- <y>100</y>
- <width>291</width>
- <height>22</height>
- </rect>
- </property>
- </widget>
- <widget class="QLabel" name="lblServerStatus">
- <property name="geometry">
- <rect>
- <x>120</x>
- <y>20</y>
- <width>231</width>
- <height>16</height>
- </rect>
- </property>
- <property name="text">
- <string>Server Status</string>
- </property>
- </widget>
- </widget>
- <widget class="QMenuBar" name="menuBar">
- <property name="geometry">
- <rect>
- <x>0</x>
- <y>0</y>
- <width>499</width>
- <height>19</height>
- </rect>
- </property>
- <widget class="QMenu" name="menuServer">
- <property name="title">
- <string>Server</string>
- </property>
- </widget>
- <addaction name="menuServer"/>
- </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>
Add Comment
Please, Sign In to add comment