1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
| #include "tcpclientconnect.h" #include <QHostAddress> #include <QDataStream> #include <QSize> #include <QBuffer> TcpClientConnect::TcpClientConnect(QObject *parent) : QObject(parent) { m_tcpsocket=new QTcpSocket(this); m_tcpsocket->setSocketOption(QTcpSocket::LowDelayOption,0); m_tcpsocket->setSocketOption(QTcpSocket::KeepAliveOption,0); m_isGetPartData=false; m_requestDataSize=0; connect(m_tcpsocket,&QTcpSocket::readyRead,this,&TcpClientConnect::handleGetRecieveData); connect(m_tcpsocket,&QTcpSocket::connected,this,&TcpClientConnect::handleSocketConnected); connect(m_tcpsocket,&QTcpSocket::disconnected,this,&TcpClientConnect::handleSocketDisconnected);
m_pHeartBeat=new QTimer; connect(m_pHeartBeat,&QTimer::timeout,this,&TcpClientConnect::handleSendHeartbeatMsg);
}
void TcpClientConnect::handleSendOutData(const sendStruct &data) { if((!m_tcpsocket)||m_tcpsocket->state()!=QAbstractSocket::ConnectedState) return; QDataStream out(m_tcpsocket); out<<qint32(data.size())<<data; m_tcpsocket->flush(); }
void TcpClientConnect::handleGetRecieveData() { if((!m_tcpsocket)||m_tcpsocket->state()!=QAbstractSocket::ConnectedState) return; if(m_isGetPartData==false){ if(m_tcpsocket->bytesAvailable()<sizeof(int)) return; else { QDataStream in(m_tcpsocket); in>>m_requestDataSize; m_isGetPartData=true; } } if(m_isGetPartData==true){ if(m_tcpsocket->bytesAvailable()<m_requestDataSize) return; else { QDataStream in(m_tcpsocket); sendStruct receiveData; in>>receiveData; m_requestDataSize=0; m_isGetPartData=false;
if(receiveData.Type==1){ QString msg; QDataStream textStream(&(receiveData.ByteData),QIODevice::ReadOnly); textStream>>msg; emit GetStrMsg(msg);
} else if(receiveData.Type==2){ QImage image; QBuffer buffer(&(receiveData.ByteData)); buffer.open(QIODevice::ReadOnly); image.load(&buffer,"JPG");
emit GetImageMsg(image);
} else if(receiveData.Type==3){ QImage image; QBuffer buffer(&(receiveData.ByteData)); buffer.open(QIODevice::ReadOnly); image.load(&buffer,"JPG");
emit GetVideoImageMsg(image);
}else if(receiveData.Type==100){ qDebug()<<receiveData.Description; m_heartBeatCount=0; } if(m_tcpsocket->bytesAvailable()) handleGetRecieveData(); } } }
void TcpClientConnect::handleSocketConnected() {
m_pHeartBeat->start(3*1000); m_heartBeatCount=0;
} void TcpClientConnect::handleSocketDisconnected() { m_pHeartBeat->stop(); m_heartBeatCount=MAX_HEARTBEAT_COUNT+1; }
void TcpClientConnect::handleSendHeartbeatMsg() { if(m_heartBeatCount<MAX_HEARTBEAT_COUNT){ sendStruct sendtextData; sendtextData.Type=100; sendtextData.Description="This is a heartbeat msg to server."; QDataStream stream(&sendtextData.ByteData,QIODevice::WriteOnly); stream<<QString(""); handleSendOutData(sendtextData); m_heartBeatCount++; }else{ disconnectFromServer(); connectToServer(); }
}
void TcpClientConnect::sendText(QString msg) { sendStruct sendtextData; sendtextData.Type=1; sendtextData.Description="this is text"; QDataStream pointStream(&sendtextData.ByteData,QIODevice::WriteOnly); pointStream<<msg; handleSendOutData(sendtextData); } void TcpClientConnect::sendImage(const QImage& img) { sendStruct sendImageData; sendImageData.Type=2; sendImageData.Description=QString("this is image");
QBuffer buffur(&(sendImageData.ByteData)); buffur.open(QIODevice::ReadWrite);
img.save(&buffur,"JPG"); handleSendOutData(sendImageData); } void TcpClientConnect::sendVideoImage(const QImage& img) { sendStruct sendImageData; sendImageData.Type=3; sendImageData.Description=QString("this is video image");
QBuffer buffur(&(sendImageData.ByteData)); buffur.open(QIODevice::ReadWrite);
img.save(&buffur,"JPG"); handleSendOutData(sendImageData); }
bool TcpClientConnect::connectToServer() { m_tcpsocket->connectToHost(QHostAddress("192.168.16.200"),6868); if(m_tcpsocket->state()!=QTcpSocket::ConnectedState){ m_tcpsocket->waitForConnected(1500); } if(m_tcpsocket->state()==QTcpSocket::ConnectedState){ return true; }else{ return false; }
}
bool TcpClientConnect::disconnectFromServer() { m_tcpsocket->disconnectFromHost(); if(m_tcpsocket->state()==QTcpSocket::UnconnectedState){ return true; }else{ return false; } }
|