IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> qt tcp封装 -> 正文阅读

[网络协议]qt tcp封装

TcpDemo.pro

QT += network

TcpClient.h

#ifndef TCPCLIENT_H
#define TCPCLIENT_H
#include <QThread>
#include <QtNetwork>

class TcpClient:public QThread{
    Q_OBJECT

public:
    static TcpClient& getInstance()
    {
        static TcpClient instance;
        return instance;
    }
    void sendData(QByteArray data);
    void destroyTcp();
    int readIdleTime=60;//空闲时间/秒  60s

private slots:
    void socketError(QAbstractSocket::SocketError error);
    void readData();
    void connected();

private:
    int conState;//-1 销毁线程状态 0:未连接 1:已连接
    int waitReadTimes;//-1 销毁线程状态 0:未连接 1:已连接
    void run();
    TcpClient();
    TcpClient(const TcpClient&){}
    TcpClient& operator==(const TcpClient&){}
    QTcpSocket * mTcpSocket;
    void startConServer();
    void recConServer();

};

#endif // TCPCLIENT_H

TcpClient.cpp

#include "TcpClient.h"
#include <QDebug>
#include "api.h"
#include <qmessagebox.h>
TcpClient::TcpClient(){
    qDebug("==TcpClient======");

}

void TcpClient::startConServer(){
    mTcpSocket=new QTcpSocket();
    qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError");
    qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError&");
    connect(mTcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));//,Qt::QueuedConnection);//Qt::DirectConnection);    connect(mTcpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
    connect(mTcpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
    connect(mTcpSocket, SIGNAL(connected()), this, SLOT(connected()));//连接状态
    mTcpSocket->connectToHost(TCP_IP,TCP_PORT);
    mTcpSocket->waitForConnected(3000);

    while (true) {
        usleep(10);
        if(this->conState==-1){
            qDebug("==while==-1=close====");
            if(this->mTcpSocket){
                qDebug("==Tcp==close=====");
                mTcpSocket->close();
                mTcpSocket->abort();
                mTcpSocket=NULL;
            }
            getInstance().quit();
            getInstance().wait();
            break;
        }else if(this->conState==0){
            qDebug("==while==0====");
            this->recConServer();
        }else if(this->conState==1){
            mTcpSocket->waitForReadyRead(100);
        }
    }

}


void TcpClient::recConServer(){
    qDebug("==recConServer=======");
    if(this->mTcpSocket){
        mTcpSocket->close();
        mTcpSocket->abort();
    }
    mTcpSocket->connectToHost(TCP_IP,TCP_PORT);
    if(mTcpSocket->waitForConnected(3000)){
        if(this->conState!=-1)   this->conState=1;
    }else{
        if(this->conState!=-1)   this->conState=0;
        qDebug("==connect error====%d=========",this->conState);
    }

}

void TcpClient::run(){
    qDebug("==TcpClient==run=====");
    this->conState=0;
    this->startConServer();
}

void TcpClient::destroyTcp(){
    this->conState=-1;
}

void TcpClient::socketError(QAbstractSocket::SocketError error){
    qDebug("==SocketError==%d===",error);
    if(error==5){
        this->waitReadTimes++;
        if(this->waitReadTimes>this->readIdleTime*10){
            if(this->conState!=-1)   this->conState=0;
        }
    }else{
        if(this->conState!=-1)   this->conState=0;
    }

}


void TcpClient::readData(){
    QByteArray bytes = mTcpSocket->readAll();
    qDebug()<<"readData::" << bytes;
    this->waitReadTimes=0;


}


void TcpClient::connected(){
    qDebug("====connected====");
    if(this->conState!=-1)   this->conState=1;
    this->waitReadTimes=0;

}

void TcpClient::sendData(QByteArray data){
    if(this->mTcpSocket){
        this->mTcpSocket->write(data);
        this->mTcpSocket->flush();
    }
}

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-11-14 22:06:46  更:2021-11-14 22:07:40 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年7日历 -2024/7/3 8:29:14-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码