在前两篇文章里,我们探讨了不少UDP丢包的解决方案。经过几年的摸索测试,其实方法非常简单, 无需修改代码。
1. Windows 下设置UDP缓存
这个方法可以一劳永逸解决UDP的收发丢包问题,只要添加注册表项目并重启即可。即使用Qt的信号与槽,QUdpSocket用信号-槽在Debug模式下也一包不丢。把下文存储为“udp.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模式下,也一包不丢:
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版本也不会丢包。如果需要,则设置缓存:
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)
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模式,
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.