?一直在弄 QT Tcp 客户端断线重连,之前方式为定时器槽触发后发起连接,如下,但定时器貌似不触发??????/
初版//
#include "mysocketImp.h" MySocketImp::MySocketImp(QHostAddress ip, int port, int sockDesc, QObject *parent) : ?? ?MySocket(sockDesc,parent), status(false),m_ip(ip),m_port(port) { ?? ?connect(this, SIGNAL(disconnected()), this, SLOT(dostateDisConnected())); ?? ?connect(this, SIGNAL(connected()), this, SLOT(dostateConnected())); ?? ?connect(&m_timer, SIGNAL(timeout()), this, SLOT(slotTimeout())); ?? ?//m_timer.start(3000); ?? ?//connect(&m_timer, &QTimer::timeout, this, &MySocketImp::slotTimeout); }
MySocketImp::~MySocketImp() {
}
void MySocketImp::Connect(const QHostAddress &ip, const int &port) { ?? ?/*this->connectToHost(ip,port); ?? ?if(!this->waitForConnected(50)) ?? ?{ ?? ??? ?QString str = this->errorString(); ?? ?}*/ ?? ?m_timer.setInterval(3000); ?? ?m_timer.start(); ?? ?this->connectToHost(ip, port); ?? ?if (!this->waitForConnected(50)) ?? ?{ ?? ??? ?//m_timer.stop(); ?? ??? ?QString str = this->errorString(); ?? ??? ?/*m_timer.setInterval(3000); ?? ??? ?m_timer.start();*/ ?? ?}
}
void MySocketImp::slotTimeout() { ?? ?if(!status) ?? ?{ ?? ??? ?this->Connect(m_ip,m_port);//Connect ?? ?} } void MySocketImp::dostateConnected() { ?? ?m_timer.stop(); ?? ?status = true; } void MySocketImp::dostateDisConnected() { ?? ?status = false; ?? ?this->abort(); ?? ?m_timer.setInterval(3000); ?? ?m_timer.start(3000); }
//改版
#include "mysocketImp.h" MySocketImp::MySocketImp(QHostAddress ip, int port, int sockDesc, QObject *parent) : ?? ?MySocket(sockDesc,parent), status(false),m_ip(ip),m_port(port) { ?? ?connect(this, SIGNAL(disconnected()), this, SLOT(dostateDisConnected())); ?? ?connect(this, SIGNAL(connected()), this, SLOT(dostateConnected())); ?? ?//connect(&m_timer, SIGNAL(timeout()), this, SLOT(slotTimeout())); ?? ?//m_timer.start(3000); ?? ?connect(&m_timer, &QTimer::timeout, this, &MySocketImp::slotTimeout); ?? ?m_timer.start(3000); }
MySocketImp::~MySocketImp() {
}
void MySocketImp::Connect(const QHostAddress &ip, const int &port) { ?? ?this->connectToHost(ip,port); ?? ?if(!this->waitForConnected(50)) ?? ?{ ?? ??? ?QString str = this->errorString(); ?? ?} ?? ?//m_timer.setInterval(3000); ?? ?//m_timer.start(); ?? ?//this->connectToHost(ip, port); ?? ?//if (!this->waitForConnected(50)) ?? ?//{ ?? ?//?? ?//m_timer.stop(); ?? ?//?? ?QString str = this->errorString(); ?? ?//?? ?/*m_timer.setInterval(3000); ?? ?//?? ?m_timer.start();*/ ?? ?//} }
void MySocketImp::slotTimeout() { ?? ?if(!status) ?? ?{ ?? ??? ?this->Connect(m_ip,m_port);//Connect ?? ?} } void MySocketImp::dostateConnected() { ?? ?//m_timer.stop(); ?? ?status = true; } void MySocketImp::dostateDisConnected() { ?? ?status = false; ?? ?//this->abort(); ?? ?//m_timer.setInterval(3000); ?? ?//m_timer.start(3000); }
/参考//
class?Widget?:?public?QWidget { ????Q_OBJECT public: ????explicit?Widget(QWidget?*parent?=?0); ????~Widget(); private?slots: ????void?readMessage(); ????void?displayError(QAbstractSocket::SocketError); ????void?sendMessage();
????void?connected(); ????void?disconnected(); ????void?onError(QAbstractSocket::SocketError); ????void?onProgress(); private: ????QTcpSocket*?tcpsocket; ????bool?flag; ????QTimer?timer; ????QString?message; ????QPushButton*?button; };
Widget::Widget(QWidget*?parent): ????QWidget(parent) { ????button?=?new?QPushButton("Send"); ????QVBoxLayout*?layout?=?new?QVBoxLayout(this); ????layout->addWidget(button);
????connect(button,SIGNAL(clicked()),this,SLOT(sendMessage()));
????tcpsocket?=?new?QTcpSocket(this); ????flag?=?false; ????//tcpsocket->abort(); ????//tcpsocket->connectToHost("10.2.100.63",80); ????connect(tcpsocket,SIGNAL(readyRead()),this,SLOT(readMessage())); ????connect(tcpsocket,SIGNAL(connected()),this,SLOT(connected())); ????connect(tcpsocket,SIGNAL(disconnected()),this,SLOT(disconnected())); ????connect(tcpsocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(onError(QAbstractSocket::SocketError)));
????connect(&timer,SIGNAL(timeout()),this,SLOT(onProgress())); ????timer.start(1800); }
Widget::~Widget() { }
void?Widget::onProgress() { ????if(flag?==?false) ????{ ????????tcpsocket->abort(); ????????tcpsocket->connectToHost("10.2.100.63",80); ????} }
void?Widget::readMessage() { ????QString?tag?=?tcpsocket->readAll(); ????std::cout<<"tag:"<<tag.size()<<std::endl; }
void?Widget::displayError(QAbstractSocket::SocketError) { ????qDebug()?<<?tcpsocket->errorString(); }
void?Widget::sendMessage() { ????std::cout<<"write?callback?data."<<std::endl; ????tcpsocket->write("ok");???? }
void?Widget::connected() { ????flag?=?true; }
void?Widget::disconnected() { ????flag?=?false; }
void?Widget::onError(QAbstractSocket::SocketError) { }
|