QT tcp server 多连接

需求 qt 在 tcp 编程时,希望 server 是多连接的,可以同时连接多个 client 解决 服务器接到新客户端连接后,会分配一个 SocketDescriptor, 然后 emit newConnection(), 服务器收到新信息,会 emit readRead(). 但是上面这两个信号都不会传递 SocketDescriptor, 所以无法分辨客户端。 override incomingConnection 重写 incomingConnection 可以获取 socketDescriptor, 然后继承 QTcpSocket 新建一个 tcp 的类,来获取 socketDescriptor. 虽然比较麻烦,但是后面多线程处理会比较方便。 nextPendingConnection nextPendingConnection 可以获取新连接的客户端,但是也是得不到 SocketDescriptor, 不过可以通过 QTcpSocket 的 peerAddress() 和 peerPort() 来获取客户端的 ip 和 端口,可以作为分辨客户端的依据。 readyRead 也可以使用 QTcpSocket::readyRead 这个信号来绑定 lambda 接收并回复的表达式。 参考 QT TCP网络编程 QT 之TCP网络编程(非常值得看的一篇博客!) QT TCP服务端如何判断客户端已断开连接 qt之QAbstractSocket incomingConnection qtcpsocket 【QT实现TCP数据发送和接收】

2023-06-25 · 1 min · 64 words · RamLife

QT QDataStream function commitTransaction return false

需求 最近在做一个 tcp demo 的时候,发现直接 read 没有问题,但是当使用 QDataStream 的 commitTransaction 来读取,返回的就是 false 这个就比较奇怪。 解决 通过 wireshark 抓包,发现 server 数据是已经发出来了,只是 client 解析错误。重新检查发送部分的代码,经过调整后,发现,向 QDataStream 里面写数据的时候,只能写 QString, 如果直接写字符串,就会读取错误。直接对比两种情况的打印,也发现,确实两种情况下,保存到 QByteArray 中的数据是不一样的。 参考 Question about QDataStream and its functions startTransaction and commitTransaction QIODevice 类 - 什么是设备? 什么是序列化? Serializing Qt Data Types QDataStream 类 - 序列化工具

2023-06-24 · 1 min · 50 words · RamLife

QT slot 所属线程

需求 qt 中 slot 可以有两种形式,一种是单独的一个函数,还有一种是 lambda 表达式,那么他们所属的线程是什么呢? 解决 单独的槽函数,所属线程是主线程 lambda 表达式,所属线程是发送信号对应的子线程。 参考 换了多种方法,还是没完美解决、、、

2023-06-24 · 1 min · 13 words · RamLife

assert 宏用于单元测试

需求 在单元测试中,需要能够自动判断结果和预期是否相符,如果不符,就报错。 解决 可以直接参考 st 的 assert_param 宏,根据需要,自己改造下,增加了额外的自定义错误说明语句。 // assert #include <stdint.h> #ifdef USE_ASSERT #define ASSERT_FAIL(expr, msg) ((expr) ? (void)0U : assert_fail((char*) msg, (char*)__FILE__, __LINE__)) void assert_fail(char* msg, char* file, uint32_t line); #else #define ASSERT_FAIL(expr) ((void)0U) #endif /* USE_ASSERT */ #ifdef USE_ASSERT void assert_fail(char* msg, char* file, uint32_t line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ //SEGGER_RTT_printf(0, "assert_failed: file %s on line %d", file, line); Log::printf(Log::kError, "assert_failed1: file %s on line %d\n", file, line); while(1); } #endif /* USE_ASSERT */ void CurrentAverageTest() { ASSERT_FAIL((1 == 1), "CurrentAverageTest, result 1 error"); } 只要在编译参数中,增加 USE_ASSERT, 开启,即可使用宏来自动判断结果是否相符。...

2023-06-21 · 1 min · 117 words · RamLife

git 普通仓库转为裸仓库

需求 普通项目目录中的 .git 仓库希望转为 gitea 之类所使用的裸仓库。 解决 使用 git clone --mirror 或者 git clone --bare. 两者的区别是: bare 会把仓库中已有的相应的分支整理出来。本地没有的分支和相关合并信息全部舍弃。 mirror 不管本地仓库中有没有这个分支,都把相关的分支和合并信息全部保留。 参考 如何将普通的Git仓库转换为裸仓库? How to convert a normal Git repository to a bare one? What’s the difference between git clone –mirror and git clone –bare When creating a git repository that will be on the server, can I convert it to a bare repository? {duplicate} {Git} 裸代码仓库和镜像代码仓库全解 git clone –mirror 和 git clone –bare 有什么区别

2023-06-20 · 1 min · 74 words · RamLife

gitea actions 功能

需求 了解 gitea 的 ci/cd 需要用到的 actions 解决 gitea 推出了 Actions 来实现 CI/CD 方案。需要在 Gitea 1.19.0 及以上的版本才能支持。需要在 gitea/conf/app.ini 中添加如下配置: # 添加此配置 [actions] ENABLED = true 后面具体配置可以参考: Gitea Actions 搭建 Gitea Actions 带你体验 CI/CD 自动化工作流 体验 Gitea Actions 体验 Gitea Actions https://blog.gitea.com/2022/12/feature-preview-gitea-actions/ 参考 体验 Gitea Actions 使用Gitee Action自动持续集成 GitHub Actions 入门教程 GitHub Actions 实现自动部署静态博客 Gitea Actions 搭建 【Gitea Action 第一篇】上手体验 GitHub Actions 快速入门 针不戳!GitHub Actions 入坑指南 了解 GitHub Actions 关于自定义操作 About custom actions

2023-06-20 · 1 min · 72 words · RamLife

c++ R"()"

需求 C++ 中 R"()" 的作用? 解决 正常的字符串在写的时候,碰到特殊字符需要使用 \ 来进行转义,如果希望直接就是原始字符,那么就需要使用 R"()" 了。 QString json = "{\ \"error\": {\ \"code\": 101,\ \"message\" : \"operation failed!\"\ },\ \"result\" : false\ }"; QString json = R"({ "error": { "code": 101, "message": "operation failed!" }, "result": false })"; 参考 C++源码转义技巧 R"()"

2023-06-19 · 1 min · 46 words · RamLife

c++ Wreorder

需求 编译时警告 warning "will be initialized after [-Wreorder] 解决 构造函数时,初始化成员变量的顺序与类声明中的变量顺序不对应,就会报这个错误。 参考 C++: warning “will be initialized after {-Wreorder} 警告: 在此处初始化后被初始化 {-Wreorder}

2023-06-19 · 1 min · 21 words · RamLife

QT QObject 多线程问题

需求 qt 多线程编程时,经常碰到 QObject: Cannot create children for a parent that is in a different thread . 解决 正常就两种方法解决: 把这个 QObject 放到子线程中的 run 里面去创建和执行。 使用 moveToThread 把这个 QObject 转移给子线程。 参考 简单例子理解 Qt 中 QObject: Cannot create children for a parent that is in a different thread. 问题 【Qt】Qt出现QObject: Cannot create children for a parent that is in a different thread.问题 (Parent is QSerialPort(0x4ab1ab0), parent‘s thread is QThread(0xbe3860)……问题解决办法 Qt Socket多线程编程出现的问题

2023-06-19 · 1 min · 69 words · RamLife

c++ 使用 sha1

需求 希望在 C++ 中使用 sha1 加密。 解决 openssl 也可以提供 sha1 加密,但是听说有滴血漏洞,所以考虑使用 cryptopp. 下载 cryptopp 之后,需要在 virtual studio 中编译,但是因为没有安装,并且我用的还是 mingw 编译环境,所以尝试在 qt 中编译。 使用 qt 命令行,切换到源码目录,然后 qmake --project 来生成 qt 工程。 使用 qt creator 打开之后,修改 pro 文件,把 TEMPLATE = app 改为 TEMPLATE = lib, 重新编译。 编译会有很多报错,基本都是 intel 的一些指令没有,可以去 https://www.intel.com/content/www/us/en/docs/intrinsics-guide/index.html#expand=3828 中搜索错误信息,然后找 CPUID Flags 这一行对应的提示,在这个提示前增加 -m 作为 flag, 放到 pro 文件中. 如下: QMAKE_CXXFLAGS += "-msse2" 有太多的报错, 所以暂时先放弃了,等后面有时间再搞。获取可以尝试安装 vs, 然后配置为 mingw 编译环境,也是一种办法。或者找找单纯的 sha1 加密库。...

2023-06-18 · 1 min · 135 words · RamLife