1 Star 3 Fork 2

冲锋的羊驼 / QtTcpMultithreading

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

QtTcpMultithreading

介绍

基于Qt开发的多线程tcp,可以在多线程安全使用

软件架构

软件架构说明

安装教程

  1. 需要已经安装Qt,测试版本为Qt5.15.2;

  2. 在环境变量中加入Qt的bin路径(qmake.exe所在路径);

  3. OPTION CompileTest 默认为ON,如果不需要编译,需要设置为OFF,在CPM中使用如下:

    # 先加入组件
    include(CPM)
    
    # 拉去并使用库
    CPMAddPackage(
            # 库命名
            NAME QtTcpMultithreading
            # 库的地址
            GIT_REPOSITORY ssh://git@192.168.101.129:3022/libs/cpplibs/QtTcpMultithreading.git
            # 库的tag标签或者分支名字
            GIT_TAG v0.1
            # 设置options
            OPTIONS "CompileTest OFF"
    )
    

使用说明

tcp server使用:
#include "QtTcpMultithreading.h"

// 用于记录新连接的socket
ZTcp::TcpClient_T *client_{nullptr};

// 用于新链接的socket收到信息时,进行处理的逻辑函数,替换为你的代码
void MainWindow::dealRead(const QByteArray &arg) {
  qDebug() << "receive:" << arg;
  static int num{0};
  QtConcurrent::run([=] {
    for (int i{0}; i != 10; ++i) {
      QByteArray msg{QString{"%1:%2"}.arg(num++).arg("world").toUtf8()};
      // 多线程中调用socket,可以在任意地方调用
      client_->write("world");
    }
  });
}

void MainWindow::on_btnTest0_clicked() {
  // 初始化 server
  ZTcp::TcpServer_T *server{new ZTcp::TcpServer_T{this}};
  // 监听地址、端口
  server->listen(QHostAddress{"127.0.0.1"}, 9966);
  // 当有新的请求连接时,触发sigNewClient信号
  connect(server, &ZTcp::TcpServer_T::sigNewClient, [=](ZTcp::TcpClient_T *client) {
    // 可以记录下来,在其他多线程中调用
    client_ = client;
    // 使用read注册回调函数dealRead
    client_->read([this](auto &&arg) { dealRead(std::forward<decltype(arg)>(arg)); });
  });
}
tcp client使用:
#include "QtTcpMultithreading.h"

// 用于记录连接的socket
ZTcp::TcpClient_T *client_{nullptr};

void MainWindow::on_btnTest0_clicked() {
  // 准备回复的消息
  QString msg{"myTest"};
  // 实例化client
  client_ = new ZTcp::TcpClient_T{this};
    
  // 绑定client自身的ip和端口
  // client_->bind(QHostAddress{"127.0.0.1"},18899);
    
  // 设置读取到消息时的处理函数
  client_->read([](QByteArray msg) {
    qDebug() << "client receive:" << msg;
  });

  // 连接到server
  client_->connectToHost(QHostAddress{"127.0.0.1"}, 9966);
  // 启动
  client_->start();
}

void MainWindow::on_btnTest1_clicked() {
  // 使用记录的client在多线程发送消息
  QtConcurrent::run([=]{
    client_->write("client hello");
  });
}
MIT License Copyright (c) 2023 冲锋的羊驼 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于Qt开发的多线程tcp,可以在多线程安全使用 展开 收起
C++ 等 3 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/Coder_Zr/QtTcpMultithreading.git
git@gitee.com:Coder_Zr/QtTcpMultithreading.git
Coder_Zr
QtTcpMultithreading
QtTcpMultithreading
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891