当前位置:网站首页>QT socket setting connection timeout
QT socket setting connection timeout
2022-06-11 06:20:00 【Kun Zai n】
QT edition :5.15.0 msvc2019
QTcpSocket All operations are asynchronous by default ,connectToHost The connection also returns immediately , no return value , Whether the signal acquisition is successful , It's not a big problem either , But the default timeout 30s A little longer , There is no obvious other member function that can modify this timeout , These two methods are generally used to find them on the Internet : 1. Use waitForConnected Change to synchronous mode and wait , Set the wait time , You can choose to cancel the connection in case of timeout , Generally, it needs to operate within the thread , Otherwise card UI 2. Build one by yourself QTimer Timer , After timeout, check whether it is connected , If you are not connected, you can choose to cancel the connection When looking at the source code, I found a third method : stay qabstractsocket.cpp Source file QAbstractSocketPrivate::_q_connectToNextAddress() Function , Create a connection timeout timer , By default QNetworkConfigurationPrivate::DefaultTimeout(30000ms) Value as timeout , At the same time judge socket Of _q_networksession Whether the attribute exists , If it exists, get the timeout from the attribute ,_q_networksession The attribute is QSharedPointer<QNetworkSession> object
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;// The default value is 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);
......
}
In fact, there is already a QTimer, Determine whether the connection timed out , Just set the timeout
In document QNetworkSession It is marked as abandoned , Not recommended , But I can't find any other way to set it , Just use it first , Set the timeout method as follows :
QNetworkConfigurationManager manager;
QNetworkConfiguration config = manager.defaultConfiguration();
config.setConnectTimeout(3000);
QSharedPointer<QNetworkSession> spNetworkSession(new QNetworkSession(config));
socket->setProperty("_q_networksession", QVariant::fromValue(spNetworkSession));
The reason why you need to set a short timeout , Because the project uses QModbusTcpClient Connect the lower computer , Connection timeouts often occur , Reconnection will be successful again , Need to shorten the timeout , To achieve the purpose of quickly connecting the upper and lower computers . The specific reason for the connection timeout has not been found yet . Use wireShark view message , Find out PC End send SYN After the handshake message , The next computer replied RST ACK, It has been like this many times in a row .
The solutions found on the Internet are not good
1. Qt By default IPv6 Connected , Read the intercepted message , It uses IPv4. You can also modify the connection parameters to add the specified IPv4 agreement connectToHost(strIP, port, QIODevice::ReadWrite, QAbstractSocket::IPv4Protocol);
2. It may be caused by agency problems , Then setting it to no proxy does not solve the problem
QNetworkProxyFactory::setUseSystemConfiguration(false); socket->setProxy(QNetworkProxy::NoProxy);
3. Set up socket option, It may have some effect ( The effect is more likely to be after the connection is completed ), However, the connection timeout still occurs
socket->setSocketOption(QAbstractSocket::LowDelayOption, 1); socket->setSocketOption(QAbstractSocket::KeepAliveOption, 1);
There are also settings for sending and receiving buffer size
socket->setSocketOption(QAbstractSocket::SendBufferSizeSocketOption, 512); socket->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption, 512);
边栏推荐
- Shandong University machine learning experiment 5 SVM
- Basic usage of MySQL
- Moteur de modèle de moteur thymeleaf
- The meaning in the status column displayed by PS aux command
- Excellent practice | how to avoid a bloody case caused by a line of wrong code?
- [reading this article is enough!!! Easy to understand] confidence level understanding (95% confidence level and confidence interval)
- Invert an array with for
- Servlet
- On the social moral and ethical issues behind short videos (personal point of view, for reference only)
- FIFO最小深度计算的题目合集
猜你喜欢
![Chapter 4 of machine learning [series] naive Bayesian model](/img/77/7720afe4e28cd55284bb365a16ba62.jpg)
Chapter 4 of machine learning [series] naive Bayesian model
![Chapter 1 of machine learning [series] linear regression model](/img/e2/1f092d409cb57130125b0d59c8fd27.jpg)
Chapter 1 of machine learning [series] linear regression model

FPGA面试题目笔记(一)——FPGA开发流程、亚稳态和竞争冒险、建立保持时间、异步FIFO深度等

ThymeleafEngine模板引擎

修复鼠标右键没有vscode快捷入口的问题

Zvuldrill installation and customs clearance tutorial

箭头函数的this指向

Convert text label of dataset to digital label

学好C语言从关键字开始

FPGA interview notes (II) -- synchronous asynchronous D flip-flop, static and dynamic timing analysis, frequency division design, retiming
随机推荐
Nlp-d46-nlp match D15
Chapter 6 of machine learning [series] random forest model
Super explanation
jenkins-不同风格的项目构建
Quantitative understanding (Quantitative deep revolutionary networks for effective information: a whitepaper)
URL in flask_ for
jenkins-用户权限管理
Examinelistactivity of Shandong University project training
Shuffleerror:error in shuffle in fetcher solution
通过R语言且只用基础package来制作一个小游戏
On the social moral and ethical issues behind short videos (personal point of view, for reference only)
Shandong University machine learning experiment 5 SVM
ERROR 1215 (HY000): Cannot add foreign key constraint
Servlet
Deployment of Flink
The meaning in the status column displayed by PS aux command
Sqli-libs post injection question 11-17 actual combat
Shandong University machine learning experiment VI k-means
Verilog realizes binocular camera image data acquisition and Modelsim simulation, and finally matlab performs image display
What should the cross-border e-commerce evaluation team do?