【Qt】Qt实践记录2——TCP通信服务器和客户端demo

收获总结:
  • 通过这个项目,我掌握了Qt的Tcp服务器类QTcpServer和Tcp套接字类QTcpSocket的使用。

  • 从读取数据的槽函数调用read() 或 readAll() 读取数据,以及发送按钮的函数调用tcpSocket->write()中,我进一步理解了套接字这个概念。

  • 还练习了Qt ui布局。

  • 还进一步掌握了做gif图演示效果。


项目目标:

熟悉QT网络编程中TCP编程

技术栈与环境:

开发语言: C++ GUI框架: Qt 6.9.0 开发环境: Qt Creator 16.0.1

编译器: MinGW 64-bit 系统平台:Windows 11

最终效果展示:

功能描述:

一个服务器窗口,一个客户端窗口,互相发送消息。^_^

UI布局:

实现:

需要使用到Qt的网络模块:

需要使用的库:

#include <QTcpServer>

#include <QTcpSocket>

窗体类增加成员:

QTcpServer *tcpServer;

QTcpSocket *tcpSocket;

需要使用的类的方法:

Server:

1、QTcpServer的方法QTcpServer():new一个实例

2、QTcpSocket的方法QTcpSocket():new一个实例

3、newConnection: newConnection()信号是QTcpServer类内置的信号。当服务器监听到新的客户端连接请求时,会自动触发此信号。

4、readyRead: 这是 QTcpSocket 的内置信号,当套接字接收到新的数据时自动触发。

5、listen: Call listen() to have the server listen for incoming connections. The newConnection() signal is then emitted each time a client connects to the server.

bool QTcpServer::listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0)

Tells the server to listen for incoming connections on address address and port port. If port is 0, a port is chosen automatically. If address is QHostAddress::Any, the server will listen on all network interfaces.

Returns true on success; otherwise returns false.

6、tcpServer->close():

void QTcpServer::close()

Closes the server. The server will no longer listen for incoming connections.

7、tcpSocket->write():

8、connectToHost:

功能实现方法:

Server端:点击打开按钮,开始监听,使用lisetn从指定端口(输入的)监听任意主机;当服务器监听到新的客户端连接请求时,会自动触发newConnection信号,将tcpServer的此信号和自定义的连接槽函数绑定;在自定义的槽函数将QTcpSocket的readyRead信号和自定义的读取槽函数绑定;在自定义的读取槽函数中读取套接字的数据,显示到ui上;关闭按钮:关服务器;发送按钮:往套接字里写数据。

cpp 复制代码
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    tcpServer = new QTcpServer(this);
    tcpSocket = new QTcpSocket(this);
    //QTcpSocket 类的实例,表示一个TCP套接字,通常用于客户端或服务器与客户端的通信

    connect(tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection_Slot()));
    //关联tcpServer发出的newConnection()信号和this(此Widget实例)下自定义的newConnection_Slot()槽函数
    //newConnection()信号是QTcpServer类内置的信号。当服务器监听到新的客户端连接请求时,会自动触发此信号。
}

Widget::~Widget()
{
    delete ui;
}

void Widget::newConnection_Slot()
{
    tcpSocket = tcpServer->nextPendingConnection();  //获得已经连接的客户端的Socket信息
    connect(tcpSocket, &QTcpSocket::readyRead, this, &Widget::readyRead_Slot);
    //readyRead: 这是 QTcpSocket 的内置信号,当套接字接收到新的数据时自动触发。
    //触发条件:操作系统内核的套接字接收缓冲区中有数据可读(例如对方发送了数据,或网络传输的数据到达本地)。
    //当网络数据到达套接字时,Qt的事件循环会检测到可读事件,并触发 readyRead() 信号。
    //通过信号与槽机制,自动调用 readyRead_Slot() 槽函数,实现异步、非阻塞的数据处理。

}

Client:类似,更简单。因为不用使用QTcpServer类。

可优化点:

搞搞多客户端的。^_^

相关推荐
The_Second_Coming4 小时前
ELK 学习笔记
笔记·学习·elk
wdfk_prog4 小时前
[Linux]学习笔记系列 -- [kernel][time]timekeeping
linux·笔记·学习
charlie1145141915 小时前
从零开始理解 CSS:让网页“活”起来的语言2
前端·css·笔记·学习·选择器·样式表·原生
im_AMBER5 小时前
Leetcode 46
c语言·c++·笔记·学习·算法·leetcode
卡提西亚5 小时前
C++笔记-20-对象特性
开发语言·c++·笔记
_Brooke_7 小时前
CS336笔记2-Architectures,Hyperparameters
笔记
被遗忘的旋律.8 小时前
Linux驱动开发笔记(十九)——IIC(AP3216C驱动+MPU6050驱动)
linux·驱动开发·笔记
wyiyiyi9 小时前
【数据结构+算法】进栈顺序推算、卡特兰数与逆波兰表达式
汇编·数据结构·笔记·算法
Larry_Yanan9 小时前
QML学习笔记(五十二)QML与C++交互:数据转换——时间和日期
开发语言·c++·笔记·qt·学习·ui·交互