QT TCP CONNECTOR 1.0

Данное приложение написано в ходе изучение работы Qt с TCP-сокетами, для их тестирования. Программа умеет подключаться, отсылать сообщение и принимать ответ.

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTcpSocket>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_Connect_clicked();
    void on_pushButton_Send_clicked();
    void socket_Read_Data();
    void socket_Disconnected();

private:
    Ui::MainWindow *ui;
    QTcpSocket *socket;
};

#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("TCP Connector 1.0");
    socket = new QTcpSocket();

    //Connect signal slot
    QObject::connect(socket, &QTcpSocket::readyRead, this, &MainWindow::socket_Read_Data);
    QObject::connect(socket, &QTcpSocket::disconnected, this, &MainWindow::socket_Disconnected);

    ui->pushButton_Send->setEnabled(false);
    ui->lineEdit_IP->setText("madmentat.ru");
    ui->lineEdit_Port->setText("5555");

}

MainWindow::~MainWindow()
{
    delete this->socket;
    delete ui;
}

void MainWindow::on_pushButton_Connect_clicked()
{
    if(ui->pushButton_Connect->text() == tr("Connect"))
    {
        QString IP;
        int port;
        //Get IP address
        IP = ui->lineEdit_IP->text();
        //Get the port number
        port = ui->lineEdit_Port->text().toInt();
        //Cancel existing connections
        socket->abort();
        //Connecting servers
        socket->connectToHost(IP, port);
        //Waiting for the connection to succeed
        if(!socket->waitForConnected(30000))
        {
            qDebug() << "Connection failed!";
            return;
        }
        qDebug() << "Connect successfully!";
        //Send key enablement
        ui->pushButton_Send->setEnabled(true);
        //Modify keyword text
        ui->pushButton_Connect->setText("Disconnect");
    }
    else
    {
        //Disconnect
        socket->disconnectFromHost();
        //Modify keyword text
        ui->pushButton_Connect->setText("Connect");
        ui->pushButton_Send->setEnabled(false);
    }
}

void MainWindow::on_pushButton_Send_clicked()
{
    qDebug() << "Send: " << ui->textEdit_Send->toPlainText();
    //Get the content of the text box and send it as ASCII code
    socket->write(ui->textEdit_Send->toPlainText().toLatin1());
    socket->flush();
    ui->textEdit_Recv->clear();
}

void MainWindow::socket_Read_Data()
{
    QByteArray buffer;
    //Read Buffer Data
    buffer = socket->readAll();
    if(!buffer.isEmpty())
    {
        QString str = ui->textEdit_Recv->toPlainText();
        str+=tr(buffer);
        //Refresh display
        ui->textEdit_Recv->setText(str);
    }
}

void MainWindow::socket_Disconnected()
{
    //Send button failure
    ui->pushButton_Send->setEnabled(false);
    //Modify keyword text
    ui->pushButton_Connect->setText("Connect");
    qDebug() << "Disconnected!";
}

 Сырцы