让 Win10 上网本 Debug 模式 QUDPSocket 信号&槽 收发不丢包的方法总结

前两篇文章里,我们探讨了不少UDP丢包的解决方案。经过几年的摸索测试,其实方法非常简单, 无需修改代码。

1. Windows 下设置UDP缓存

这个方法可以一劳永逸解决UDP的收发丢包问题,只要添加注册表项目并重启即可。即使用Qt的信号与槽,QUdpSocket用信号-槽在Debug模式下也一包不丢。把下文存储为"udp.reg",而后双击,即可修改注册表。

reg 复制代码
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\AFD\Parameters]
"DefaultReceiveWindow"=dword:00100000
"FastSendDatagramThreshold"=dword:00002800
"DefaultSendWindow"=dword:00100000

设置好后,即使在Debug模式下,也一包不丢:

txt 复制代码
qtcpp_demo/udp_loss_test/build/debug/udp_loss_test.exe
Start...
QUdpSocket LOOP:
        Send 10000, Recv 10000, Lost 0.
Start...
QUdpSocket Signal and Slots:
        Send 10000, Recv 10000, Lost 0.
Start...
Local Socket :
        Send 10000, Recv 10000, Lost 0.
Finished sending. Closing socket.
Exiting.

2. Linux 下设置UDP缓存

Linux 下,默认的缓存已经足够了,Debug版本也不会丢包。如果需要,则设置缓存:

basg 复制代码
sudo sysctl -w net.core.rmem_default=100000
net.core.rmem_default = 100000
sudo sysctl -w net.core.rmem_max=1000000
net.core.rmem_max = 1000000

参考:
https://www.baeldung.com/linux/udp-socket-buffer

3. 代码级别的设置

想要单独用代码设置1个套接字的缓存,则使用:

(参考:https://blog.csdn.net/test1280/article/details/79776938)

c 复制代码
    int  rcvBufSize = 1024*1024*16;
	 int optlen = sizeof(rcvBufSize);
    if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &rcvBufSize, optlen) < 0)
    {
        printf("setsockopt error=%d(%s)!!!\n", errno, strerror(errno));
        goto error;
    }
    printf("set udp socket(%d) recv buff size to %d OK!!!\n", sockfd, rcvBufSize);

4. 测试工程

测试工程参考:

https://gitcode.net/coloreaglestdio/qtcpp_demo/-/tree/master/udp_loss_test

5 效果

在上网本Intel® Celeron® CPU J1800 @ 2.41GHz 2.41 GHz下,Qt 6.8.0 MINGW64 Debug模式,

txt 复制代码
Starting C:\projects\qtcpp_demo\udp_loss_test\build\Desktop_Qt_6_8_0_shared_MinGW_w64_UCRT64_MSYS2-Debug\debug\udp_loss_test.exe...
Start...
QUdpSocket LOOP:
	Send 10000, Recv 10000, Lost 0.
Start...
QUdpSocket Signal and Slots:
	Send 10000, Recv 10000, Lost 0.
Start...
Local Socket :
	Send 10000, Recv 10000, Lost 0.
Finished sending. Closing socket.
Exiting.
相关推荐
Jony_3 天前
高可用移动网络连接
网络协议
chilix3 天前
Linux 跨网段路由转发配置
网络协议
埃博拉酱4 天前
VS Code Remote SSH 连接 Windows 服务器卡在"下载 VS Code 服务器":prcdn DNS 解析失败的诊断与 BITS 断点续传
windows·ssh·visual studio code
唐宋元明清21884 天前
.NET 本地Db数据库-技术方案选型
windows·c#
DianSan_ERP4 天前
电商API接口全链路监控:构建坚不可摧的线上运维防线
大数据·运维·网络·人工智能·git·servlet
加号35 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
tryCbest5 天前
Windows环境下配置pip镜像源
windows·pip
呉師傅5 天前
火狐浏览器报错配置文件缺失如何解决#操作技巧#
运维·网络·windows·电脑
gihigo19985 天前
基于TCP协议实现视频采集与通信
网络协议·tcp/ip·音视频
百事牛科技5 天前
保护文档安全:PDF限制功能详解与实操
windows·pdf