QTcpSocket Disconencted()信号未发出

时间:2018-10-13 03:57:49

标签: c++ qt qt5 qtcpsocket qtimer

我的代码似乎运行正常! {(QObject:无法为处于不同线程中的父级创建子级:父级线程:QThread(0x221f650),当前线程:QThread (0x23a7950):此错误已解决}

但是,当我拔下LAN时, disconnected()信号没有发出,因此 Reconenction计时器没有启动,因此我的代码停止! 我尝试以这样的方式编写代码:即使设备无法连接,**侦听计时器**也会在每 10秒之后启动,并会在超时后自动连接放置 when_ReconnectionTimer_timeout()一次又一次尝试,直到连接成功!但是似乎断开的信号不起作用!

#include "fduprocess.h"
#include<QDebug>
#include<QThread>
fduprocess::fduprocess(QObject *parent) : QObject(parent)
{
}

void fduprocess::timerEvent(QTimerEvent *event)
{
    if(event->timerId()== _iStatusPollTimer)
    {
        if(_ClientSocketInstance.state() == QAbstractSocket::ConnectedState)
        {
            _ClientSocketInstance.write("alarmstat\r\n"); //25
            _ClientSocketInstance.write("selectedin\r\n"); // 4
            _ClientSocketInstance.write("sigoutstat\r\n"); //13
            _ClientSocketInstance.write("disablestat\r\n"); //5    
            _ClientSocketInstance.write("pwrstat\r\n"); //5    
            _ClientSocketInstance.write("siginstat\r\n");//5    
        }
    }
}

void fduprocess::tryit()
{
    qDebug()<< "...came inside tryit to tryit.........";
    connect(&_ReconnectionTimerInstance,SIGNAL(timeout()), this, SLOT(when_ReconnectionTimer_timeout()));
    qDebug()<<"process thread"<< QThread::currentThreadId();
    qDebug()<< "...conencted to whentimeout.........";
    connect(&_ClientSocketInstance,SIGNAL(connected()), this, SLOT(when_ClientSocketInstance_connected()));
    connect(&_ClientSocketInstance,SIGNAL(disconnected()), this, SLOT(when_ClientSocketInstance_disconnected()));
    connect(&_ClientSocketInstance,SIGNAL(readyRead()), this, SLOT(when_ClientSocketInstance_readyRead()));
    connect(&_ClientSocketInstance,SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(when_ClientSocketInstance_error(QAbstractSocket::SocketError)));
    _strIPAddress = "192.168.1.135";
    _usiPort = 23;
    qDebug()<< "...end tryit........";
    _ReconnectionTimerInstance.setSingleShot(true);
    _ReconnectionTimerInstance.setInterval(10000);
    _ReconnectionTimerInstance.start();
    _iStatusPollTimer= startTimer(2000);    
}

void fduprocess::when_ReconnectionTimer_timeout()
{
    qDebug()<<"Reconnecting..";
    // qDebug()<< _ClientSocketInstance.children();
    _ClientSocketInstance.connectToHost(_strIPAddress, _usiPort);
    qDebug()<<"tryin gto reconect..";
}

void fduprocess::when_ClientSocketInstance_connected()
{ 
    qDebug()<<"Connected......";        
    _ClientSocketInstance.write("endrun_1");
    _ClientSocketInstance.flush();
    _ClientSocketInstance.write("\n");
    _ClientSocketInstance.write("\n");
    _ClientSocketInstance.write("\n");
    _ClientSocketInstance.flush();   
}

void fduprocess::when_ClientSocketInstance_disconnected()
{
    qDebug()<<"Disconnected";
    _ReconnectionTimerInstance.start();
}

void fduprocess::when_ClientSocketInstance_readyRead()
{
    QByteArray get = _ClientSocketInstance.readLine();
    qDebug() << "getting "<<get ;
}

这是fduprocess.h

#ifndef FDUPROCESS_H
#define FDUPROCESS_H
#include"fdu.h"
#include <QObject>
#include<QTimer>
#include<QTimerEvent>
#include<QTcpSocket>

class fduprocess : public QObject
{
    Q_OBJECT
    QString                 _strIPAddress;
    quint16                 _usiPort;
    public:
    explicit fduprocess(QObject *parent = 0);
    QTcpSocket              _ClientSocketInstance;
    QTimer               _ReconnectionTimerInstance ;
   int                     _iStatusPollTimer;
     void timerEvent(QTimerEvent *event);
   signals:
   public slots:
    void tryit();
    void doOntimeout();
    void when_ReconnectionTimer_timeout();
    void when_ClientSocketInstance_connected();
    void when_ClientSocketInstance_disconnected();
    void when_ClientSocketInstance_readyRead();
    void when_ClientSocketInstance_error(QAbstractSocket::SocketError error);
};

#endif // FDUPROCESS_H

这是 fdu.cpp

#include "fdu.h"
#include "ui_fdu.h"
#include"fduprocess.h"
fdu::fdu(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::fdu)
{
    ui->setupUi(this);
    QThread *workerThread =  new QThread;
    fduprocess *worker = new fduprocess;

 qDebug()<< "...goind to fduprocess........";

//    worker->_ReconnectionTimerInstance = setSingleShot(true);
//    worker->_ReconnectionTimerInstance.start(1000);
//    worker->_iStatusPollTimer = startTimer(500);
    worker->moveToThread(workerThread);
    worker->_ClientSocketInstance.moveToThread(workerThread);
    worker->_ReconnectionTimerInstance.moveToThread(workerThread);
    connect(workerThread,SIGNAL(started()),worker,SLOT(tryit()));
    connect(workerThread,SIGNAL(finished()),worker,SLOT(deleteLater()));
     workerThread->start();
    qDebug()<<"fdu thread "<<QThread::currentThreadId();

}

fdu::~fdu()
{
    delete ui;
}

main.cpp

#include "fdu.h"
#include <QApplication>
#include<QDebug>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    fdu w;
    w.show();
  qDebug()<<"main thrad" <<QThread::currentThreadId();
    return a.exec();
}

我犯错的人可以吗?

由于我处于学习曲线中,请忽略我的命名约定和缩进

1 个答案:

答案 0 :(得分:0)

解决了!看来我对某些概念感到困惑! 状态为“正在连接”,因此不能断开连接!

_ClientSocketInstance.disconnectFromHost();
    _ReconnectionTimerInstance.start();

内部 when_ClientSocketInstance_error(QAbstractSocket::SocketError error)

这将实现我想要的目标。 抱歉,由于我的概念混乱而造成的所有错误。 感谢您的耐心等待!