最近发现QDataStream这个好东东,序列化发送数据很方便,与大家分享一下。
客户端:
line.h
1 #ifndef LINE_H 2 #define LINE_H 3 #include <QString> 4 5 class Line 6 { 7 public: 8 Line(); 9 10 public: 11 int pointX; 12 int pointY; 13 int pointZ; 14 15 QString pointName; 16 17 public: 18 void getPoint(); 19 void setPoint(); 20 21 }; 22 23 #endif // LINE_H
line.cpp
1 #include "line.h" 2 3 Line::Line() 4 { 5 } 6 7 8 void Line::getPoint() 9 { 10 11 } 12 13 void Line::setPoint() 14 { 15 16 }
myvector.h
1 #ifndef MYVECTOR_H 2 #define MYVECTOR_H 3 4 #include <QVector> 5 #include <QDebug> 6 #include "line.h" 7 8 class MyVector:public QVector<Line> 9 { 10 public: 11 MyVector(); 12 13 friend QDataStream& operator <<(QDataStream &out,MyVector &myVector); 14 15 friend QDataStream& operator >>(QDataStream &in,MyVector &myVector); 16 17 int x; 18 }; 19 20 class ChildVector:public MyVector 21 { 22 public: 23 ChildVector(); 24 25 public: 26 friend QDataStream& operator <<(QDataStream &out,ChildVector &childVector); 27 28 friend QDataStream& operator >>(QDataStream &in,ChildVector &childVector); 29 30 int y; 31 int z; 32 int childData; 33 34 }; 35 36 #endif // MYVECTOR_H
myvector.cpp
1 #include "myvector.h" 2 3 MyVector::MyVector() 4 { 5 } 6 7 8 QDataStream& operator <<(QDataStream &out,MyVector &myVector) 9 { 10 out << quint32(myVector.size()); 11 qDebug() <<"myVector---size()"<<myVector.size()<<endl; 12 13 for (typename QVector<Line>::const_iterator it = myVector.begin(); it != myVector.end(); ++it) 14 { 15 out << it->pointName << it->pointX << it->pointY << it->pointZ; 16 qDebug()<<"it->pointName####"<<it->pointName<<"it->pointX$$$$$"<<it->pointX<<"it->pointY%%%%"<<it->pointY<<"it->pointZ^^^^^"<<it->pointZ<<endl; 17 } 18 out << myVector.x; 19 return out; 20 } 21 22 23 24 QDataStream& operator >>(QDataStream &in,MyVector &myVector) 25 { 26 myVector.clear(); 27 quint32 c; 28 in >> c; 29 myVector.resize(c); 30 for(quint32 i = 0;i < c;++i) 31 { 32 Line tLine; 33 in >> tLine.pointName >> tLine.pointX >> tLine.pointY >> tLine.pointZ; 34 } 35 in >> myVector.x; 36 return in; 37 } 38 39 ChildVector::ChildVector() 40 { 41 42 } 43 44 QDataStream& operator <<(QDataStream &out,ChildVector &childVector) 45 { 46 qDebug() <<"myVector---size()"<<childVector.size()<<endl; 47 48 MyVector *kk = &childVector; 49 out << *kk; 50 out << childVector.y << childVector.z<<childVector.childData; 51 return out; 52 } 53 54 QDataStream& operator >>(QDataStream &in,ChildVector &childVector) 55 { 56 MyVector *kk = &childVector; 57 in << *kk; 58 in >> childVector.y >> childVector.z >> childVector.childData; 59 60 return in; 61 }
tcpclient.h
1 #ifndef TCPCLIENT_H 2 #define TCPCLIENT_H 3 4 #include <QObject> 5 #include <QDataStream> 6 #include <Qt/qtcpserver.h> 7 #include <Qt/qtcpsocket.h> 8 #include <Qt/qhostinfo.h> 9 #include <Qt/qhostaddress.h> 10 11 #include "myvector.h" 12 13 class TcpClient : public QObject 14 { 15 Q_OBJECT 16 public: 17 //explicit TcpClient(QObject *parent = 0); 18 TcpClient():m_iOut(&byteArrayWrite,QIODevice::WriteOnly) 19 { 20 m_nPort = 8080; 21 m_bStatus = false; 22 tcpClient = NULL; 23 24 //设置数据流的版本,客户端和服务器端使用的版本要相同 25 m_iOut.setVersion(QDataStream::Qt_4_6); 26 //设置发送长度初始值为0 27 m_iOut<<(quint32)0; 28 } 29 30 signals: 31 32 public slots: 33 void slotGetStatus(); 34 35 public: 36 void initConnection(QString ip); 37 void updata(); 38 39 QDataStream m_iIn; 40 QDataStream m_iOut; 41 QByteArray byteArrayWrite; 42 43 QTcpSocket *tcpClient; 44 45 //MyVector myVector; 46 47 QString m_sIpAddress; 48 int m_nPort; 49 bool m_bStatus; 50 }; 51 52 #endif // TCPCLIENT_H
tcpclient.cpp
1 #include "tcpclient.h" 2 3 //TcpClient::TcpClient(QObject *parent) : 4 // QObject(parent) 5 //{ 6 7 //} 8 9 void TcpClient::initConnection(QString ip) 10 { 11 m_sIpAddress = ip; 12 QHostAddress address; 13 address.setAddress(ip); 14 15 if(tcpClient == NULL) 16 { 17 tcpClient = new QTcpSocket(this); 18 } 19 20 tcpClient->abort(); 21 22 tcpClient->connectToHost(address,8080); 23 24 25 26 //connect(tcpClient,SIGNAL(readyRead()),this,SLOT(readData())); 27 //connect(tcpClient,SIGNAL(disconnected()),this,SLOT(slotDisconnect())); 28 //connect(tcpClient,SIGNAL(connected()),this,SLOT(sendData())); 29 m_bStatus = connect(tcpClient,SIGNAL(connected()),this,SLOT(slotGetStatus())); 30 31 while(!m_bStatus) 32 { 33 initConnection(m_sIpAddress); 34 } 35 } 36 37 void TcpClient::slotGetStatus() 38 { 39 40 } 41 42 void TcpClient::updata() 43 { 44 //回到字节流起始位置 45 m_iOut.device()->seek(0); 46 //重置字节流长度 47 m_iOut << (quint32) (byteArrayWrite.size()-sizeof(quint32)); 48 //往套接字缓存中写入数据,并发送 49 int writeBlock = tcpClient->write(byteArrayWrite); 50 51 qDebug()<<"writeBlock******"<<writeBlock; 52 53 byteArrayWrite.resize(0); 54 55 tcpClient->waitForReadyRead(1000); 56 }
test.h
1 #ifndef TEST_H 2 #define TEST_H 3 4 #include <QObject> 5 #include "tcpclient.h" 6 7 class Test : public QObject 8 { 9 Q_OBJECT 10 public: 11 explicit Test(QObject *parent = 0); 12 13 signals: 14 15 public slots: 16 void getData(); 17 18 public: 19 TcpClient *tcpClt; 20 }; 21 22 #endif // TEST_H
test.cpp
1 #include "test.h" 2 3 Test::Test(QObject *parent) : 4 QObject(parent) 5 { 6 tcpClt = new TcpClient(); 7 } 8 9 void Test::getData() 10 { 11 QString ipAddress("192.168.154.128"); 12 13 tcpClt->initConnection(ipAddress); 14 15 ChildVector myVector; 16 17 Line tLine; 18 for(int i = 0;i < 10;i++) 19 { 20 tLine.pointName = QString("test")+QString::number(i); 21 tLine.pointX = 100 + i*10; 22 tLine.pointY = 150 + i*100; 23 tLine.pointZ = 5000 + i*1000; 24 myVector.append(tLine); 25 } 26 27 myVector.x = 1000; 28 myVector.y = 2000; 29 myVector.z = 3000; 30 myVector.childData = 30; 31 32 tcpClt->m_iOut << myVector; 33 34 tcpClt->updata(); 35 }
服务端接收:
line.h line.cpp,myvector.h myvector.cpp不再添加,同客户端
tcpserver.h
1 #ifndef TCPSERVER_H 2 #define TCPSERVER_H 3 4 #include <Qt/qtcpserver.h> 5 #include <Qt/qtcpsocket.h> 6 #include <Qt/qhostinfo.h> 7 #include <Qt/qhostaddress.h> 8 #include <Qt/qnetworkinterface.h> 9 #include <QObject> 10 11 #include "myvector.h" 12 13 class TcpServer : public QObject 14 { 15 Q_OBJECT 16 public: 17 explicit TcpServer(QObject *parent = 0,int port = 8080); 18 19 ChildVector myVector; 20 21 signals: 22 23 public slots: 24 void readData(); 25 //void deleteSlot(); 26 void slotConnected(); 27 28 private: 29 QTcpServer *m_pServer; 30 QTcpSocket *m_pTcpClientSocket; 31 32 33 }; 34 35 #endif // TCPSERVER_H
tcperver.cpp
1 #include "tcpserver.h" 2 3 int GLOBAL_LENGTH = 0; 4 5 TcpServer::TcpServer(QObject *parent, int port) : 6 QObject(parent) 7 { 8 m_pServer = new QTcpServer(this); 9 if(m_pServer->listen(QHostAddress::Any,port)) 10 { 11 qDebug()<<"listen Successfully!!!"; 12 } 13 connect(m_pServer,SIGNAL(newConnection()),this,SLOT(slotConnected())); 14 } 15 16 void TcpServer::slotConnected() 17 { 18 qDebug()<<"slotConnected!!!"; 19 m_pTcpClientSocket = m_pServer->nextPendingConnection(); 20 21 bool bConnect = connect(m_pTcpClientSocket,SIGNAL(readyRead()),this,SLOT(readData())); 22 //connect(m_pTcpClientSocket,SIGNAL(disconnected()),m_pTcpClientSocket,SLOT(deleteLater())); 23 qDebug()<<"bConnect"<<bConnect; 24 } 25 26 void TcpServer::readData() 27 { 28 quint32 nextBlockSize = 0; 29 QDataStream in(m_pTcpClientSocket); 30 in.setVersion(QDataStream::Qt_4_6); 31 quint32 bufferSize = m_pTcpClientSocket->bytesAvailable(); 32 if(nextBlockSize == 0) 33 { 34 if(m_pTcpClientSocket->bytesAvailable()<sizeof(quint32)) 35 { 36 return; 37 } 38 in>>nextBlockSize; 39 qDebug()<<"nextBlockSize:"<<nextBlockSize<<"m_pTcpClientSocket->bytesAvailable()"<<m_pTcpClientSocket->bytesAvailable(); 40 } 41 42 GLOBAL_LENGTH = nextBlockSize/sizeof(quint32); 43 44 in >> myVector; 45 //qDebug()<<"-------------"<<myVector.x<<"@@@@@@@@@@"<<"myVector.size()#####"<<myVector.size()<<endl; 46 for(int i = 0; i < myVector.size() ;i++) 47 { 48 qDebug()<<"***********"<<myVector.at(i).pointName<<"###"; 49 qDebug()<<"***********"<<myVector.at(i).pointX<<"###"; 50 qDebug()<<"***********"<<myVector.at(i).pointY<<"###"; 51 qDebug()<<"***********"<<myVector.at(i).pointZ<<"###"<<endl; 52 } 53 54 qDebug()<<"-------------"<<myVector.x<<"@@@@@@@@@@"<<"myVector.y#####"<<myVector.y<<"@@@@@@@@@@"<<"myVector.y#####"<<myVector.z<<"childData-----"<<myVector.childData<<endl; 55 if(nextBlockSize==0xFFFF) 56 { 57 return; 58 } 59 if(bufferSize < nextBlockSize) 60 { 61 return; 62 } 63 }
以上代码经过编译测试通过,希望对你有所帮助。