源码: ????NetToolsTest
QT Widge 其它文章请点击这里: ????QT Widget 学习笔记
姊妹篇: ????
Qt TCP/UDP 网络协议入门实操(一)TCP通讯
Qt TCP/UDP 网络协议入门实操(二)UDP通讯
Qt TCP/UDP 网络协议入门实操(补充)
此文只是最简单的入门学习,需进一步可以看 飞扬青云 大佬的源码 QWidgetDemo, 选择 nettool ,已经是非常完善的网络调试助手了
关于TCP/IP协议可详看:TCP/IP协议详解 - 知乎 具体就不介绍
总的来说,TCP/IP 通讯分为客户端和服务器通讯,而底层 Qt 都已经都封装好了,如果仅仅只是简单使用,还是相当简单的
在此只需弄懂 QTcpServer(监听套接字)和QTcpSocket(通讯套接字)
监听套接字,顾名思义,监听关于各种通讯的状态,一旦进行通讯,监听套接字会启动通讯套接字,进行通讯
一、TCP 客户端
● Qt 中使用 TCP 客户端通讯演示 左侧为成熟的网络调试助手
● 实操流程图
客户端使用 connectToHost 函数主动连接服务器后,服务器会触发 newConnectio 这个槽函数,并进行取出 QTcpServer(监听套接字),将相关内容取出并赋给QTcpSocket(通讯套接字)。
客户端向服务器发送数据,触发readyRead(),进行处理,彼此传递时,原理都是这样的。
● 代码
tcpclient.cpp
#include "tcpclient.h"
#include "ui_tcpclient.h"
TcpClient::TcpClient(QWidget *parent) :
QWidget(parent),
ui(new Ui::TcpClient)
{
ui->setupUi(this);
initForm();
}
TcpClient::~TcpClient()
{
delete ui;
}
void TcpClient::initForm()
{
socket = new QTcpSocket(this);
connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
}
void TcpClient::on_btnConnect_clicked()
{
TcpServerIP = ui->txtServerIP->text().trimmed();
TcpServerPort = ui->txtServerPort->text().trimmed().toInt();
if (ui->btnConnect->text() == "连接") {
socket->abort();
socket->connectToHost(TcpServerIP, static_cast<quint16>(TcpServerPort));
ui->btnConnect->setText("断开");
} else {
socket->abort();
ui->btnConnect->setText("连接");
}
}
void TcpClient::readData() {
QByteArray data = socket->readAll();
if (data.length() <= 0) return;
QString strData = "[接收] " + TcpServerIP + " " + QString::number(TcpServerPort) + " " + data + "";
ui->txtMain->setTextColor(QColor("red"));
ui->txtMain->append(strData);
}
void TcpClient::on_btnSend_clicked()
{
QString data = ui->cboxData->currentText();
if (data.length() <= 0) return;
QByteArray buffer = data.toUtf8();
socket->write(buffer);
QString strData = "[发送] " + TcpServerIP + " " + QString::number(TcpServerPort) + " " + buffer + "";
ui->txtMain->setTextColor(QColor("darkgreen"));
ui->txtMain->append(strData);
}
tcpclient.h
#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <QWidget>
#include <QtNetwork>
namespace Ui {
class TcpClient;
}
class TcpClient : public QWidget
{
Q_OBJECT
public:
TcpClient(QWidget *parent = nullptr);
~TcpClient();
void initForm();
private slots:
void readData();
private slots:
void on_btnConnect_clicked();
void on_btnSend_clicked();
private:
Ui::TcpClient *ui;
QTcpSocket *socket;
QString TcpServerIP;
int TcpServerPort;
};
#endif
二、TCP 服务器
● Qt 中使用 TCP 服务器通讯演示
● 实操流程图
● 代码
tcpserver.cpp
#include "tcpserver.h"
#include "ui_tcpserver.h"
#define TIMEMS qPrintable(QTime::currentTime().toString("HH:mm:ss zzz"))
TcpServer::TcpServer(QWidget *parent) :
QWidget(parent),
ui(new Ui::TcpServer)
{
ui->setupUi(this);
initForm();
}
TcpServer::~TcpServer()
{
delete ui;
}
void TcpServer::initForm() {
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
}
void TcpServer::on_btnListen_clicked()
{
if (ui->btnListen->text() == "监听") {
if (server->listen(QHostAddress::Any , 8888)) {
ui->txtMain->setTextColor(QColor("darkgreen"));
ui->btnListen->setText("关闭");
} else {
ui->txtMain->append("监听失败");
}
} else {
socket->abort();
server->close();
ui->btnListen->setText("监听");
}
}
void TcpServer::newConnection()
{
while (server->hasPendingConnections())
{
socket = server->nextPendingConnection();
QString str = QString("[ip:%1,port:%2]").arg(socket->peerAddress().toString()).arg(socket->peerPort());
ui->txtMain->append(str + " Connect to the server");
ui->cboxListenIP->addItem(str);
connect(socket, &QTcpSocket::readyRead, this, &TcpServer::readData);
}
}
void TcpServer::readData() {
QByteArray data = socket->readAll();
if (data.length() <= 0) return;
ui->txtMain->setTextColor(QColor("red"));
QString strData = "Receive: " + data;
ui->txtMain->append(strData);
}
void TcpServer::on_btnSend_clicked()
{
QString data = ui->cboxData->currentText();
if (data.length() <= 0) return;
QByteArray buffer = data.toUtf8();
socket->write(buffer);
QString strData = "Send: " + buffer;
ui->txtMain->setTextColor(QColor("darkgreen"));
ui->txtMain->append(strData);
}
tcpserver.h
#ifndef TCPSERVER_H
#define TCPSERVER_H
#include <QWidget>
#include <QtNetwork>
namespace Ui {
class TcpServer;
}
class TcpServer : public QWidget
{
Q_OBJECT
public:
explicit TcpServer(QWidget *parent = nullptr);
~TcpServer();
void initForm();
private:
Ui::TcpServer *ui;
QTcpServer *server;
QTcpSocket *socket;
private slots:
void newConnection();
void readData();
private slots:
void on_btnListen_clicked();
void on_btnSend_clicked();
};
#endif
源码: ????NetToolsTest
QT Widge 其它文章请点击这里: ????QT Widget 学习笔记
姊妹篇: ????
Qt TCP/UDP 网络协议入门实操(一)TCP通讯
Qt TCP/UDP 网络协议入门实操(二)UDP通讯
Qt TCP/UDP 网络协议入门实操(补充)
参考:
飞扬青云——Qt编写网络调试助手
QT从入门到入土(九)——TCP/IP网络通信(以及文件传输)
|