简体   繁体   English

当主线程无限循环运行时,服务器未收到用QTcpSocket :: write写入的字节吗?

[英]Bytes written with QTcpSocket::write has not been received by server when Main Thread running in infinite loop?

I am working on an application, which interacts with server. 我正在开发一个与服务器交互的应用程序。

while starting, it is loading some data from database, when database size is large then application is running in very long loop let say i have 10 lac records in db and loop will iterate 10 lac times. 启动时,它正在从数据库加载一些数据,当数据库很大时,应用程序将在很长的循环中运行,可以说我在db中有10个lac记录,循环将迭代10个lac时间。

After a regular interval of time (set by user) let say 1 seconds, my application is sending heartbeat message to server in different thread. 经过一段固定的时间间隔(由用户设置)后,例如说1秒钟,我的应用程序正在将心跳消息发送到不同线程中的服务器。

In normal cases, it is working fine but when application runs in long loop, heartbeat message has not been received by server although written bytes returned by QTcpSocket::write() are same what i am writing (successful), but tcpDump is not showing any byte received. 在正常情况下,它工作正常,但当应用程序在长循环中运行时,服务器未收到心跳消息,尽管QTcpSocket::write()返回的写入字节与我写入的内容相同(成功),但tcpDump未显示接收到的任何字节。

i have used 我用过

tcpSocket->setSocketOption(QAbstractSocket::LowDelayOption , 1); 
tcpSocket->setSocketOption(QAbstractSocket::KeepAliveOption , 600);

Try adding processEvents() inside your loop (let's say every 1000th cycle or every 100ms) to keep your application responsive. 尝试在循环中添加processEvents() (假设每1000个周期或每100ms),以保持应用程序的响应速度。 Of course, it's a sign of bad design if you encounter such problems. 当然,如果遇到此类问题,则表明设计不良。 Consider re-design you application to use multiple threads, ie the main thread and a worker thread for long operations (your loop). 考虑重新设计您的应用程序以使用多个线程,即用于长时间操作(您的循环)的主线程和辅助线程。 More about thread support on Qt can be found here . 在Qt上可以找到更多有关线程支持的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM