Table of contents
3. Classes required for TCP-based communication
4.2 TCP network program design
1 Introduction
The network has TCP and UDP. This article mainly completes the TCP network design through QT, and communicates with the microcontroller through ESP8266.
2. TCP communication process
3. Classes required for TCP-based communication
QTcpSever server class, used to monitor client connections and establish connections with clients.
The socket class of QTcpSocket communication, both the client and the server need to use.
Both QTcpSever and QTcpSocket belong to the network module network.
4. QT terminal design
4.1 Project establishment
1、
2、
3、
4、
5、
6、
7、
4.2 TCP network program design
QT provides the QTcpServer class, which can directly instantiate a client, which can be indexed in help as follows:
The QTcpServer class provides a TCP-based server. More...
Header: #include <QTcpServer>
qmake: QT += network
Inherits: QObject
First, add QT += network in the .pro file to perform network programming
QT += core gui network
Then add the required header file in widget.h, the header file is as follows:
#include <QTcpServer>
#include <QTcpSocket>
#include <QNetworkInterface>
4.2.1 QT interface design
As follows:
1、
2、
3、
4、
5、
6、
4.2.2 UI layout
overall vertical layout
Set it to read-only for the receiving box, as follows:
4.2.3 Control renaming
5、widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
#include <QNetworkInterface>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
QTcpServer *tcpserver;//声明一个QTcpserver的对象,用于监听
QTcpSocket *tcpsocket;//创建服务器的套接字,用于与客户端进行通信
private slots:
void newConnection_Slot();
void readyRead_Slot();
void on_open_Button_clicked();
void on_close_Button_clicked();
void on_send_Button_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
6、widget.c
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
tcpserver = new QTcpServer(this);
tcpsocket = new QTcpSocket(this);
//当服务器发现有人要来连接时,就会发出newconnection 的信号,从而触发槽函数newConnection_Slot()(自行编写的槽函数)
connect(tcpserver,SIGNAL(newConnection()),this,SLOT(newConnection_Slot()));
}
//建立接收客户端连接的槽函数,有人连接就触发这个槽函数
void Widget::newConnection_Slot()
{
//获取这个服务器sserver与客户端通信的套接字
tcpsocket = tcpserver->nextPendingConnection();
connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(readyRead_Slot()));
}
void Widget::readyRead_Slot()
{
QString buf;
buf = tcpsocket->readAll();
ui->rece_Edit->appendPlainText(buf);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_open_Button_clicked()
{
//服务端点击启动服务器,服务器就开始监听
//监听----------启动服务器
//QHostAddress::Any 地址,接纳所有的地址
//端口号 ui->sportEdit->text()获得输入的字符串,转换成无符号短整型
tcpserver->listen(QHostAddress::Any,ui->port_line->text().toUShort());
}
void Widget::on_close_Button_clicked()
{
tcpserver->close();
}
void Widget::on_send_Button_clicked()
{
tcpsocket->write(ui->send_line->text().toLocal8Bit().data());
}