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();
    }
}

Guess you like

Origin blog.csdn.net/qq_25430563/article/details/121306693