[Qt]|[Qt] TCP客户端与服务器断开连接自动重联机制
TCP服务器断开连接自动重联机制 客户端加入定时器实现断线重联(客户端服务端代码见上一篇博客)
编译环境:Qt 5.9.5
ui界面如图: 【[Qt]|[Qt] TCP客户端与服务器断开连接自动重联机制】
文章图片
代码如下
tcpclient.h
#ifndef TCPCLIENT_H
#define TCPCLIENT_H#include
#include
#include
#include
#include
using namespace std;
namespace Ui {
class TcpClient;
}class TcpClient : public QWidget
{
Q_OBJECTpublic:
explicit TcpClient(QWidget *parent = 0);
~TcpClient();
protected:
void newTcpConnect();
void init();
private slots://void on_pushButtonEnter_clicked();
void slotconnectedsuccess();
//处理连接成功的信号
void slotreceive();
//接收服务器传过来的信息
void on_pushButtonSend_clicked();
void slotdisconnected();
//处理离开聊天室的信号void slotTimeout();
//处理timeou信号void connectButton();
private:
Ui::TcpClient *ui;
bool status;
//用来判断是否进入了服务器
size_t strSize;
int port;
QHostAddress *serverIP;
QString userName;
QTcpSocket *tcpsocket;
QTimer m_timer;
};
#endif // TCPCLIENT_H
tcpclient.cpp
#include "tcpclient.h"
#include "ui_tcpclient.h"
#include
#include
#include TcpClient::TcpClient(QWidget *parent) :
QWidget(parent),
ui(new Ui::TcpClient)
{
ui->setupUi(this);
//将进入服务器的标志位置为false
status = false;
//未连接内不能发送信息
ui->pushButtonSend->setEnabled(false);
init();
}TcpClient::~TcpClient()
{
delete ui;
}void TcpClient::init()
{
port = 8888;
ui->lineEditServerPort->setText(QString::number(port));
QString ip = ui->lineEditServerIp->text();
userName = ui->lineEditUserName->text();
tcpsocket = new QTcpSocket(this);
//tcpsocket->connectToHost(ip, port);
connect(ui->pushButtonEnter, &QPushButton::clicked, this, &TcpClient::connectButton);
connect(tcpsocket, &QTcpSocket::connected, this, &TcpClient::slotconnectedsuccess);
connect(tcpsocket, &QTcpSocket::readyRead, this, &TcpClient::slotreceive);
connect(tcpsocket, &QTcpSocket::disconnected, this, &TcpClient::slotdisconnected);
connect(&m_timer, &QTimer::timeout, this, &TcpClient::slotTimeout);
}void TcpClient::connectButton()
{
if(ui->pushButtonEnter->text() == "连接服务器") {
newTcpConnect();
//tcpsocket->disconnectFromHost();
}
else if(ui->pushButtonEnter->text() == "离开服务器") {
tcpsocket->disconnectFromHost();
//newTcpConnect();
}
}//用来处理连接成功的信号
void TcpClient::slotconnectedsuccess()
{
m_timer.stop();
//连接成功后定时器停止
status = true;
ui->textEdit->append("连接成功");
ui->pushButtonSend->setEnabled(true);
ui->pushButtonEnter->setText("离开服务器");
QString msg = userName + ": connected";
tcpsocket->write(msg.toUtf8().data());
}void TcpClient::slotreceive()
{
QByteArray array = tcpsocket->readAll();
QString strHead = QString(array).section("", 0, 0);
QString strText = QString(array).section("", 1, 1);
QString buf = userName + ":";
if(strHead == buf) {
ui->textEdit->append(buf + strText);
}}void TcpClient::on_pushButtonSend_clicked()
{
//获取编辑区内容
QString str = ui->lineEditSend->text();
//加头信息
qint64 textSize = str.size();
QString strHead = QString("%1##%2##%3").arg(userName + ": ").arg(textSize).arg(str);
tcpsocket->write(strHead.toUtf8().data());
ui->lineEditSend->clear();
}void TcpClient::slotdisconnected()
{
m_timer.start(2000);
//连接断开时开启定时器,定时时间为2s
status == false;
ui->textEdit->append("断开连接");
ui->pushButtonSend->setEnabled(false);
ui->pushButtonEnter->setText("连接服务器");
}void TcpClient::slotTimeout()
{
qDebug() << "重联服务器222222222";
if(status == true) {
qDebug() << "重联服务器";
ui->textEdit->append("重联服务器...");
newTcpConnect();
}
}void TcpClient::newTcpConnect()
{
tcpsocket->abort();
port = 8888;
ui->lineEditServerPort->setText(QString::number(port));
QString ip = ui->lineEditServerIp->text();
tcpsocket->connectToHost(ip, port);
if (!tcpsocket->waitForConnected(3000)) {
//连接时长为3s,超过3秒连不上则输出错误信息
//QMessageBox::warning(this, "错误", "连接超时");
}}
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- CVE-2020-16898|CVE-2020-16898 TCP/IP远程代码执行漏洞
- Docker应用:容器间通信与Mariadb数据库主从复制
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量
- 第326天
- Shell-Bash变量与运算符
- 逻辑回归的理解与python示例
- Guava|Guava RateLimiter与限流算法
- 我和你之前距离
- CGI,FastCGI,PHP-CGI与PHP-FPM