当前位置:网站首页>QT socket设置连接超时时间
QT socket设置连接超时时间
2022-06-11 06:05:00 【坤仔N】
QT版本:5.15.0 msvc2019
QTcpSocket操作默认都是异步操作,connectToHost连接也是立刻返回,没有返回值,通过信号获取是否成功,这个问题也不大,但是这个默认超时时间30s有点长,没有明显的其他成员函数可以修改这个超时时间,网上找到一般是这两个方法: 1. 使用waitForConnected改成同步的方式等待,设置等待时间,超时可以选择取消连接,一般需要在线程内操作,不然卡UI 2. 自己建一个QTimer定时器,超时后检查是否已经连接了,没连接可以选择取消连接 在查看源码时发现了第三种方法: 在qabstractsocket.cpp源码文件的QAbstractSocketPrivate::_q_connectToNextAddress()函数中, 创建了一个连接超时的定时器,默认使用QNetworkConfigurationPrivate::DefaultTimeout(30000ms)值作为超时时间,同时判断socket的_q_networksession属性是否存在,存在则从属性获取超时时间,_q_networksession属性为QSharedPointer<QNetworkSession>对象
void QAbstractSocketPrivate::_q_connectToNextAddress()
{
......
if (!connectTimer) {
connectTimer = new QTimer(q);
QObject::connect(connectTimer, SIGNAL(timeout()),
q, SLOT(_q_abortConnectionAttempt()),
Qt::DirectConnection);
}
int connectTimeout = QNetworkConfigurationPrivate::DefaultTimeout;// 默认值为30000ms
#ifndef QT_NO_BEARERMANAGEMENT // ### Qt6: Remove section
QSharedPointer<QNetworkSession> networkSession = qvariant_cast< QSharedPointer<QNetworkSession> >(q->property("_q_networksession"));
if (networkSession) {
QNetworkConfiguration networkConfiguration = networkSession->configuration();
connectTimeout = networkConfiguration.connectTimeout();
}
#endif
connectTimer->start(connectTimeout);
......
}
实际内部已经定义了一个QTimer,判断来连接是否超时,只需要设置这个超时时间即可
在文档中QNetworkSession标志为已经废弃,不建议使用,不过暂时没找到其他方法设置,就先用着,设置超时方法如下:
QNetworkConfigurationManager manager;
QNetworkConfiguration config = manager.defaultConfiguration();
config.setConnectTimeout(3000);
QSharedPointer<QNetworkSession> spNetworkSession(new QNetworkSession(config));
socket->setProperty("_q_networksession", QVariant::fromValue(spNetworkSession));
之所以需要设置短的超时时间,是因为项目使用QModbusTcpClient连接下位机,经常出现连接超时,重连又会成功,需要缩短超时时间,以达到快速连接上下位机的目的。连接超时具体原因暂时没找到。使用wireShark查看消息,发现PC端发送SYN握手信息后,下位机回复了RST ACK,连续多次都是这样子。
网上查找到的解决方法都不行
1. Qt默认使用IPv6连接的,看了拦截的消息,使用的是IPv4。也特意修改连接参数添加指定IPv4协议connectToHost(strIP, port, QIODevice::ReadWrite, QAbstractSocket::IPv4Protocol);
2. 可能是代理问题造成的,那么设置成无代理也没有解决
QNetworkProxyFactory::setUseSystemConfiguration(false); socket->setProxy(QNetworkProxy::NoProxy);
3. 设置socket option,可能有点效果(效果更多估计是连接完成后),不过依然出现连接超时
socket->setSocketOption(QAbstractSocket::LowDelayOption, 1); socket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
还有设置收发buffer大小
socket->setSocketOption(QAbstractSocket::SendBufferSizeSocketOption, 512); socket->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption, 512);
边栏推荐
- 通过R语言且只用基础package来制作一个小游戏
- Summarize the five most common BlockingQueue features
- Eureka集群搭建
- How to use perforce helix core with CI build server
- Distributed framework ray - detailed introduction to starting ray and connecting clusters
- Managing VHDS using batch
- ERROR 1215 (HY000): Cannot add foreign key constraint
- Sword finger offer 04: find in 2D array
- jenkins-不同风格的项目构建
- ThymeleafEngine模板引擎
猜你喜欢

Free get | full function version of version control software

Shandong University machine learning experiment VI k-means

ThymeleafEngine模板引擎

Installing and using sublist3r in Kali

Record the first data preprocessing process

All questions and answers of database SQL practice niuke.com

Graphsage paper reading

Topic collection of FIFO minimum depth calculation
![[reading this article is enough!!! Easy to understand] confidence level understanding (95% confidence level and confidence interval)](/img/d6/e61ba5bad2b2847378c4547ce0780d.jpg)
[reading this article is enough!!! Easy to understand] confidence level understanding (95% confidence level and confidence interval)

Box model
随机推荐
PgSQL reports an error: current transaction is aborted, commands ignored until end of transaction block
Topic collection of FIFO minimum depth calculation
做亚马逊测评要了解的知识点有哪些?
[daily exercise] 217 Duplicate element exists
Super details to teach you how to use Jenkins to realize automatic jar package deployment
Stock K-line drawing
Growth Diary 01
Super explanation
Méthode de la partie du tableau
Principle of copyonwritearraylist copy on write
Fix [no Internet, security] problem
数组部分方法
Error reporting injection of SQL injection
Moteur de modèle de moteur thymeleaf
学好C语言从关键字开始
跨境电商测评自养号团队应该怎么做?
Do we really need conference headphones?
Nlp-d46-nlp match D15
ERROR 1215 (HY000): Cannot add foreign key constraint
Sword finger offer 32: print binary tree from top to bottom