if (blockSize == 0) { if (sockets->bytesAvailable() < (int)sizeof(quint32)) continue; in >> blockSize; }
struct MyStruct { int i; ... }; Q_DECLARE_METATYPE(MyStruct)
int main(int argc,char** argv){ QApplication app(argc,argv); promp p1,p2,p3,p4,p5; p1.val1=1; p1.val2=10; p1.val3=true; p2.val1=2; p2.val2=20; p2.val3=true; p3.val1=3; p3.val2=30; p3.val3=true; p4.val1=4; p4.val2=40; p4.val3=true; p5.val1=5; p5.val2=50; p5.val3=true; QVector<promp> players; players.append(p1); players.append(p2); players.append(p3); players.append(p4); players.append(p5); QFile f("temp.t"); { f.open(QIODevice::WriteOnly); f.resize(0); QDataStream s(&f); s<<players; f.close(); } p1.val1=0; p1.val2=0; p1.val3=false; p2.val1=0; p2.val2=0; p2.val3=false; p3.val1=0; p3.val2=0; p3.val3=false; p4.val1=0; p4.val2=0; p4.val3=false; p5.val1=0; p5.val2=0; p5.val3=false; players.clear(); { f.open(QIODevice::ReadOnly); QDataStream s(&f); s>>players; f.close(); for(int i=0; i<players.size();i++) qWarning()<<"p"<<i<<"\t"<< players.at(i).val1 << players.at(i).val2 << players.at(i).val3; } return 1;}
Запускается D:\qt\projects\LIC_NEW\release\CRYPTER.exe...p 0 1 10 true p 1 2 20 true p 2 3 30 true p 3 4 40 true p 4 5 50 true D:\qt\projects\LIC_NEW\release\CRYPTER.exe завершился с кодом 1
#include <QtCore>#include <QApplication>#include <QtNetwork>#include "client.h"QDataStream &operator>>(QDataStream &in, promp &inp){ in>>inp.val1; in>>inp.val2; in>>inp.val3; return in;}QDataStream &operator>>(QDataStream &in, promp *inp){ in>>inp->val1; in>>inp->val2; in>>inp->val3; return in;}QDataStream &operator<<(QDataStream &out, const promp &inp){ out<<inp.val1<<inp.val2; out<<inp.val3; return out;}QDataStream &operator<<(QDataStream &out, const promp *inp){ out<<inp->val1<<inp->val2; out<<inp->val3; return out;}int main(int argc,char** argv){ QApplication app(argc,argv); promp p1,p2,p3,p4,p5; p1.val1=1; p1.val2=10; p1.val3=true; p2.val1=2; p2.val2=20; p2.val3=true; p3.val1=3; p3.val2=30; p3.val3=true; p4.val1=4; p4.val2=40; p4.val3=true; p5.val1=5; p5.val2=50; p5.val3=true; QVector<promp> players; players.append(p1); players.append(p2); players.append(p3); players.append(p4); players.append(p5); QTcpSocket sock; sock.connectToHost("localhost",3474); sock.waitForConnected(10000); if(sock.state()!=QAbstractSocket::UnconnectedState){ QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_7); //выделяешь место в начале соощения размером 4 байта out << (quint32)0; //записываешь данные out << players; //возвращаешься в начало на те 4 байта out.device()->seek(0); //записываешь размер данных (тот самый BlockSize) out << (quint32)(block.size() - sizeof(quint32)); //for(int i=0; i<sockets.size(); i++) sockets->write(block); sock.write(block);//высылаешь sock.waitForBytesWritten(); sock.flush(); qWarning()<<"ok"<<block.size(); } else{ qWarning()<<"cannot connect"; } sock.close(); return 1;}
#ifndef CLIENT_H#define CLIENT_Hstruct promp{public: int val1; int val2; bool val3;};#endif // CLIENT_H
#ifndef SERVER_H#define SERVER_H#include <QtNetwork>struct promp{public: int val1; int val2; bool val3;};class Serv:public QObject{ Q_OBJECTpublic: Serv(QObject* parent=0):QObject(parent){ QTcpServer *s=new QTcpServer(this); s->listen(QHostAddress::Any,3474); connect(s,SIGNAL(newConnection()),this,SLOT(nc())); }public slots: void nc(){ QTcpSocket *so=((QTcpServer*)sender())->nextPendingConnection(); connect(so,SIGNAL(readyRead()),this,SLOT(read())); } void read(){ QTcpSocket *so=(QTcpSocket*)sender(); QByteArray arr=so->readAll(); qWarning()<<arr.size(); }};#endif // SERVER_H
#include <QApplication>#include <QtNetwork>#include <QtCore>#include "server.h"QDataStream &operator>>(QDataStream &in, promp &inp){ in>>inp.val1; in>>inp.val2; in>>inp.val3; return in;}QDataStream &operator>>(QDataStream &in, promp *inp){ in>>inp->val1; in>>inp->val2; in>>inp->val3; return in;}QDataStream &operator<<(QDataStream &out, const promp &inp){ out<<inp.val1<<inp.val2; out<<inp.val3; return out;}QDataStream &operator<<(QDataStream &out, const promp *inp){ out<<inp->val1<<inp->val2; out<<inp->val3; return out;}int main(int argc,char** argv){ QApplication app(argc,argv); Serv *s= new Serv(0); return app.exec();}
Запускается D:\qt\projects\LIC_NEW\release\CRYPTER.exe...ok 53 D:\qt\projects\LIC_NEW\release\CRYPTER.exe завершился с кодом 1Запускается D:\qt\projects\LIC_NEW\release\CRYPTER.exe...ok 53 D:\qt\projects\LIC_NEW\release\CRYPTER.exe завершился с кодом 1
Запускается D:\qt\projects\serv\debug\serv.exe...53 53 Программа неожиданно завершилась.D:\qt\projects\serv\debug\serv.exe завершился с кодом 62097
#include <QtCore>#include <QApplication>struct promp{ int val1; int val2; bool val3;};Q_DECLARE_METATYPE(promp)int main(int argc,char** argv){ QApplication app(argc,argv); qRegisterMetaType<promp>("promp"); promp p1; p1.val1=1; p1.val2=10; p1.val3=false; QVariant v=qVariantFromValue(p1); QFile f("t.temp"); f.open(QIODevice::WriteOnly); f.resize(0); QDataStream st(&f); st<<v; f.close(); return 1;}
QVariant::save: unable to save type 258.
out<<p1.val1<<p1.val2<<p1.val3;
qRegisterMetaType<promp>("promp");
Q_DECLARE_METATYPE(promp)