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)
{
}