最近发现QDataStream这个好东东,序列化发送数据很方便,与大家分享一下。
客户端:
line.h
#ifndef LINE_H
#define LINE_H
#include <QString> class Line
{
public:
Line(); public:
int pointX;
int pointY;
int pointZ; QString pointName; public:
void getPoint();
void setPoint(); }; #endif // LINE_H
line.cpp
#include "line.h" Line::Line()
{
} void Line::getPoint()
{ } void Line::setPoint()
{ }
myvector.h
#ifndef MYVECTOR_H
#define MYVECTOR_H #include <QVector>
#include <QDebug>
#include "line.h" class MyVector:public QVector<Line>
{
public:
MyVector(); friend QDataStream& operator <<(QDataStream &out,MyVector &myVector); friend QDataStream& operator >>(QDataStream &in,MyVector &myVector); int x;
}; class ChildVector:public MyVector
{
public:
ChildVector(); public:
friend QDataStream& operator <<(QDataStream &out,ChildVector &childVector); friend QDataStream& operator >>(QDataStream &in,ChildVector &childVector); int y;
int z;
int childData; }; #endif // MYVECTOR_H
myvector.cpp
#include "myvector.h" MyVector::MyVector()
{
} QDataStream& operator <<(QDataStream &out,MyVector &myVector)
{
out << quint32(myVector.size());
qDebug() <<"myVector---size()"<<myVector.size()<<endl; for (typename QVector<Line>::const_iterator it = myVector.begin(); it != myVector.end(); ++it)
{
out << it->pointName << it->pointX << it->pointY << it->pointZ;
qDebug()<<"it->pointName####"<<it->pointName<<"it->pointX$$$$$"<<it->pointX<<"it->pointY%%%%"<<it->pointY<<"it->pointZ^^^^^"<<it->pointZ<<endl;
}
out << myVector.x;
return out;
} QDataStream& operator >>(QDataStream &in,MyVector &myVector)
{
myVector.clear();
quint32 c;
in >> c;
myVector.resize(c);
for(quint32 i = ;i < c;++i)
{
Line tLine;
in >> tLine.pointName >> tLine.pointX >> tLine.pointY >> tLine.pointZ;
}
in >> myVector.x;
return in;
} ChildVector::ChildVector()
{ } QDataStream& operator <<(QDataStream &out,ChildVector &childVector)
{
qDebug() <<"myVector---size()"<<childVector.size()<<endl; MyVector *kk = &childVector;
out << *kk;
out << childVector.y << childVector.z<<childVector.childData;
return out;
} QDataStream& operator >>(QDataStream &in,ChildVector &childVector)
{
MyVector *kk = &childVector;
in << *kk;
in >> childVector.y >> childVector.z >> childVector.childData; return in;
}
tcpclient.h
#ifndef TCPCLIENT_H
#define TCPCLIENT_H #include <QObject>
#include <QDataStream>
#include <Qt/qtcpserver.h>
#include <Qt/qtcpsocket.h>
#include <Qt/qhostinfo.h>
#include <Qt/qhostaddress.h> #include "myvector.h" class TcpClient : public QObject
{
Q_OBJECT
public:
//explicit TcpClient(QObject *parent = 0);
TcpClient():m_iOut(&byteArrayWrite,QIODevice::WriteOnly)
{
m_nPort = ;
m_bStatus = false;
tcpClient = NULL; //设置数据流的版本,客户端和服务器端使用的版本要相同
m_iOut.setVersion(QDataStream::Qt_4_6);
//设置发送长度初始值为0
m_iOut<<(quint32);
} signals: public slots:
void slotGetStatus(); public:
void initConnection(QString ip);
void updata(); QDataStream m_iIn;
QDataStream m_iOut;
QByteArray byteArrayWrite; QTcpSocket *tcpClient; //MyVector myVector; QString m_sIpAddress;
int m_nPort;
bool m_bStatus;
}; #endif // TCPCLIENT_H
tcpclient.cpp
#include "tcpclient.h" //TcpClient::TcpClient(QObject *parent) :
// QObject(parent)
//{ //} void TcpClient::initConnection(QString ip)
{
m_sIpAddress = ip;
QHostAddress address;
address.setAddress(ip); if(tcpClient == NULL)
{
tcpClient = new QTcpSocket(this);
} tcpClient->abort(); tcpClient->connectToHost(address,); //connect(tcpClient,SIGNAL(readyRead()),this,SLOT(readData()));
//connect(tcpClient,SIGNAL(disconnected()),this,SLOT(slotDisconnect()));
//connect(tcpClient,SIGNAL(connected()),this,SLOT(sendData()));
m_bStatus = connect(tcpClient,SIGNAL(connected()),this,SLOT(slotGetStatus())); while(!m_bStatus)
{
initConnection(m_sIpAddress);
}
} void TcpClient::slotGetStatus()
{ } void TcpClient::updata()
{
//回到字节流起始位置
m_iOut.device()->seek();
//重置字节流长度
m_iOut << (quint32) (byteArrayWrite.size()-sizeof(quint32));
//往套接字缓存中写入数据,并发送
int writeBlock = tcpClient->write(byteArrayWrite); qDebug()<<"writeBlock******"<<writeBlock; byteArrayWrite.resize(); tcpClient->waitForReadyRead();
}
test.h
#ifndef TEST_H
#define TEST_H #include <QObject>
#include "tcpclient.h" class Test : public QObject
{
Q_OBJECT
public:
explicit Test(QObject *parent = ); signals: public slots:
void getData(); public:
TcpClient *tcpClt;
}; #endif // TEST_H
test.cpp
#include "test.h" Test::Test(QObject *parent) :
QObject(parent)
{
tcpClt = new TcpClient();
} void Test::getData()
{
QString ipAddress("192.168.154.128"); tcpClt->initConnection(ipAddress); ChildVector myVector; Line tLine;
for(int i = ;i < ;i++)
{
tLine.pointName = QString("test")+QString::number(i);
tLine.pointX = + i*;
tLine.pointY = + i*;
tLine.pointZ = + i*;
myVector.append(tLine);
} myVector.x = ;
myVector.y = ;
myVector.z = ;
myVector.childData = ; tcpClt->m_iOut << myVector; tcpClt->updata();
}
服务端接收:
line.h line.cpp,myvector.h myvector.cpp不再添加,同客户端
tcpserver.h
#ifndef TCPSERVER_H
#define TCPSERVER_H #include <Qt/qtcpserver.h>
#include <Qt/qtcpsocket.h>
#include <Qt/qhostinfo.h>
#include <Qt/qhostaddress.h>
#include <Qt/qnetworkinterface.h>
#include <QObject> #include "myvector.h" class TcpServer : public QObject
{
Q_OBJECT
public:
explicit TcpServer(QObject *parent = ,int port = ); ChildVector myVector; signals: public slots:
void readData();
//void deleteSlot();
void slotConnected(); private:
QTcpServer *m_pServer;
QTcpSocket *m_pTcpClientSocket; }; #endif // TCPSERVER_H
tcperver.cpp
#include "tcpserver.h" int GLOBAL_LENGTH = ; TcpServer::TcpServer(QObject *parent, int port) :
QObject(parent)
{
m_pServer = new QTcpServer(this);
if(m_pServer->listen(QHostAddress::Any,port))
{
qDebug()<<"listen Successfully!!!";
}
connect(m_pServer,SIGNAL(newConnection()),this,SLOT(slotConnected()));
} void TcpServer::slotConnected()
{
qDebug()<<"slotConnected!!!";
m_pTcpClientSocket = m_pServer->nextPendingConnection(); bool bConnect = connect(m_pTcpClientSocket,SIGNAL(readyRead()),this,SLOT(readData()));
//connect(m_pTcpClientSocket,SIGNAL(disconnected()),m_pTcpClientSocket,SLOT(deleteLater()));
qDebug()<<"bConnect"<<bConnect;
} void TcpServer::readData()
{
quint32 nextBlockSize = ;
QDataStream in(m_pTcpClientSocket);
in.setVersion(QDataStream::Qt_4_6);
quint32 bufferSize = m_pTcpClientSocket->bytesAvailable();
if(nextBlockSize == )
{
if(m_pTcpClientSocket->bytesAvailable()<sizeof(quint32))
{
return;
}
in>>nextBlockSize;
qDebug()<<"nextBlockSize:"<<nextBlockSize<<"m_pTcpClientSocket->bytesAvailable()"<<m_pTcpClientSocket->bytesAvailable();
} GLOBAL_LENGTH = nextBlockSize/sizeof(quint32); in >> myVector;
//qDebug()<<"-------------"<<myVector.x<<"@@@@@@@@@@"<<"myVector.size()#####"<<myVector.size()<<endl;
for(int i = ; i < myVector.size() ;i++)
{
qDebug()<<"***********"<<myVector.at(i).pointName<<"###";
qDebug()<<"***********"<<myVector.at(i).pointX<<"###";
qDebug()<<"***********"<<myVector.at(i).pointY<<"###";
qDebug()<<"***********"<<myVector.at(i).pointZ<<"###"<<endl;
} qDebug()<<"-------------"<<myVector.x<<"@@@@@@@@@@"<<"myVector.y#####"<<myVector.y<<"@@@@@@@@@@"<<"myVector.y#####"<<myVector.z<<"childData-----"<<myVector.childData<<endl;
if(nextBlockSize==0xFFFF)
{
return;
}
if(bufferSize < nextBlockSize)
{
return;
}
}
以上代码经过编译测试通过,希望对你有所帮助。