QT socket 心跳包检测手段及与发包之间的处理方法

QT socket 心跳包检测手段:

开一个1s的定时器来定时发送心跳包:

构造函数中写

    tmrKeepAlive = new QTimer(this);
    tmrKeepAlive->setInterval(1000);
    tmrKeepAlive->setSingleShot(true);

 connect(tmrKeepAlive, SIGNAL(timeout()), this, SLOT(tmrKeepAliveTimeout()));//关联心跳包定时处理函数

 

void tcpSocket::tmrKeepAliveTimeout()
{
    QCoreApplication::processEvents();

    sendMSGKeepAlive();

    tmrKeepAlive->setSingleShot(true);
    tmrKeepAlive->start();
}
 

此外如果在0.8s的时候需要发送一个指令给下位机,则需要将定时器清零,重新计时

   在write之前先将定时器关闭,然后将定时器开启,之后发送本次需要发送的数据:

     tmrKeepAlive->stop();
     tmrKeepAlive->start();
     qint64 ret=tcpClient->write((char *)&msg, PPCORE_LUASRV_SOCKET_HEADER_SIZE + size);

 

 

 


版权声明:本文为dbdxnuliba原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。