首页| 论坛| 消息

标题:多线程Qtcpsocket状态不改变的问题
作者:14dzqiu
日期:2016-10-07 16:20
内容:

简单说一下,首先我在主线程开了一个tcpserver,在重现它的虚函数incomingConnection(qintptr handle),让它能够发出信号 emit incomingsock(handle),并连接到主线程的一个槽CreateThread,在这个槽中我创建线程来监控这些新连接,并在线程的run函数中new一个qtcpsocket来控制这个连接,但是我在连接中发现我的Qtcpsocket->state()是不变的,就算我把下位机的WiFi模块断电,qDebug出来的一样还是connectedState。

一下是我的各个代码:
首先是我对QTcpserver的继承
#include "mytcpserver.h"
//#include "tcpthread.h"
myTcpServer::myTcpServer(QWidget * parent):QTcpServer(parent)
{
}
void myTcpServer::incomingConnection(qintptr handle)
{
emit incomingsock(handle);
}


随后我在主线程中建立它,并连接信号与槽
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new[/co ..


#1 [kaon 10-08 18:32]
If no data is exchanged for a certain while, TCP will start sending keep-alive segments (basically, ACK segments with the acknowledgement number set to the current sequence number less one). The other peer then replies with another acknowledgement. If this acknowledgment is not received within a certain number of probe segments, the connection is automatically dropped. The little problem is that the kernel starts sending keep-alive segments after 2 hours since when the connection becomes idle! Therefore, you need to change this value (if your OS allows that) or implement your own keep-alive mechanism in your protocol (like many protocols do, e.g. SSH). Linux allows you to change it using setsockopt:
http://stackoverflow.com/questions/10445122/qtcpsocket-state-always-connected-even-unplugging-ethernet-wire
#2 回 kaon 的帖子 [14dzqiu 10-09 10:54]
kaon:http://stackoverflow.com/questions/10445122/qtcpsocket-state-always-connected-even-unplugging-ethernet-wire (2016-10-08 18:32) 
受益匪浅,但是我想知道QTcpsocket里面那些disconnect这些信号是什么情况才会发出的呢?

回复 发表
主题 版块