Using the socket to transfer objects

Source: Internet
Author: User
Tags julian day object serialization

The use of QT network communication is always simple to send data-receive data, the common is to send a string/byte stream, and then based on the string/byte stream to extract useful information. This can meet the actual needs in the communication that is not very complex. but also has the disadvantage, is every time writes the network communication is the verbal agreement one protocol, lets the sender and the receiver both satisfy this agreement can carry on the normal communication. So if every time I write such a program, I will first specify the good one communication protocol, and then write the corresponding code.

so how to transfer more complex data, and can not use the above verbal protocol?

There is a concept of object serialization in Java,c#, and QT also has the ability to transfer objects in a serialized way to make it easier to transmit some of the complex object data we construct.

QT Serialization

 

The
Qdatastream allows you to serialize some of the Qt data types. The table below lists the data types that Qdatastream can serialize and how they is represented. The format described below is version 12. It is always the best to cast integers-a Qt integer type, such as qint16 or Quint32, when reading and writing. This ensures is know exactly what size integers you is reading and writing, no matter what the underlying PL Atform and architecture The application happens to is running on.
Serialization is the ability to turn a class into a series of bytes, which can also be constructed out of the original class using deserialization. These bytes can be stored directly on the hard disk, but can also be sent over the network. Using these features, we can begin to do some interesting network communication.


of course, not all classes are able to satisfy serializable, and QT describes classes that satisfy serialization .

BOOL Booleanqint8 signed Byteqint16 signed 16-bit Integerqint32 signed 32-bit Integerqint64 signed 64-bit i ntegerquint8 unsigned bytequint16 unsigned 16-bit integerquint32 unsigned 32-bit integerquint64 unsigned 64-bi T integerfloat 32-bit floating point number using the, IEEE 754 formatdouble 64-bit floating point number us  ing the standard IEEE 754 formatconst char * The string length (quint32) The string bytes, excluding the terminating 0QBitArray the array size (QUINT32) the array bits, i.e. (size + 7)/8 Bytesqbrush The Brush style (quint8) Th e Brush Color (qcolor) If style is Custompattern, the Brush pixmap (qpixmap) Qbytearray if the byte array is null:0x  FFFFFFFF (quint32) otherwise:the array size (QUINT32) followed by the array bytes, i.e. size Bytesqcolor Color Spec    (qint8) Alpha value (quint16) Red value (quint16) Green value (quint16) Blue value (quint16) Pad value (quint16) QC Ursor ShaPE ID (qint16) If shape is bitmapcursor:the bitmap (qpixmap), Mask (Qpixmap), and Hot Spot (Qpoint) Qdate Julian Day (Quint32) Qdatetime Date (qdate) time (qtime) 0 for Qt::localtime, 1 for QT::UTC (quint8) qeasingcurve type (quint8) f UNC (Quint64) hasconfig (bool) If Hasconfig is True then these fields Follow:list period (double) Amplitud  E (double) overshoot (double) Qfont the family (QString) the point size (qint16) The style hint (quint8) the    Char set (QUINT8) the weight (quint8) the font bits (quint8) Qhash<key, t> the number of items (QUINT32) For all items, the key (key) and value (T) Qicon the number of pixmap entries (Quint32) for all pixmap entries:t He Pixmap (qpixmap) the file name (QString) the pixmap size (qsize) the mode (QUINT32) the state (Quint32) Qima GE If The image is null a "null image" marker is saved; Otherwise the image is saved in PNG or BMP format (depending on the streamVersion). If you want control of the format, stream the image to a qbuffer (using Qimageio) and stream that. Qkeysequence a qlist<int>, where each integer is A key in the key sequenceqlinkedlist<t> the number of I TEMs (Quint32) the items (t) qlist<t> the number of items (Quint32) the items (t) Qmap<key, t> the N    Umber of items (quint32) for all items, the key (key) and value (T) Qmatrix (OBSOLETE) M11 (double) M12 (double) M21 (Double) M22 (double) dx (double) dy (double) qmatrix4x4 M11 (double) M12 (double) M13 (double) m M21 (Double) M22 (double) M23 (double) M24 (double) M31 (double) M32 (double) ) M34 (double) M41 (double) M42 (double) M43 (double) M44 (double) qpair<t1, t2> First (T1) Seco nd (T2) qpalettethe disabled, active, and inactive color groups, each of which consists of the Following:foreground (QB Rush) button (Qbrush) Light (QBrush) midlight (Qbrush) Dark (Qbrush) mid (Qbrush) text (Qbrush) brighttext (Qbrush) ButtonText (Qbrush ) Base (Qbrush) background (Qbrush) Shadow (Qbrush) Highlight (Qbrush) highlightedtext (qbrush) Link (QB    Rush) linkvisited (qbrush) Qpen the pen styles (quint8) the pen width (quint16) The pen color (qcolor) qpicture The size of the picture data (QUINT32), the raw bytes of picture data (char) Qpixmap Save it as a PNG image. Qpoint the X coordinate (qint32) the Y coordinate (qint32) qquaternion the scalar component (double) The X coor Dinate (double) The Y coordinate (double) the z coordinate (double) qrect left (qint32) Top (Qint32) Right ( Qint32) Bottom (Qint32) Qregexp the regexp pattern (QString) case sensitivity (quint8) Regular expression Synta    X (quint8) Minimal Matching (quint8) qregion the size of the data, i.e. 8 + * (number of rectangles) (Quint32) Ten (Qint32) the number of Rectangles (Quint32) the rectangles in sequential order (qrect) qsize width (qint32) height (qint32) QString If The string is Null:0xffffffff (quint32) otherwise:the string length in bytes (Quint32) followed by the data in UTF-1    6QTime Milliseconds since midnight (quint32) Qtransform M11 (double) M12 (double) M13 (double) M21 (double) M22 (Double) M23 (double) M31 (double) M32 (double) M33 (double) Qurl holds an URL (QString) qvariant T He type of the data (QUINT32) the NULL flag (QINT8) The data of the specified typeqvector2d the X coordinate (dou BLE) The Y coordinate (double) qvector3d The X coordinate (double) the Y coordinate (double) the Z coordinate ( Double) qvector4d The X coordinate (double) the Y coordinate (double) the z coordinate (double) the W Coordinat E (double) qvector<t> the number of items (Quint32) the items (T)



These are strong enough to imagine that you are building a qlist/qimage locally, being able to transmit it to each other completely, without writing the relevant construction code to transfer the object in the past can save much effort.

today wrote a demo demo, Feel good.

First, construct your own serializable class.

Class Datapackage{public:    QString label_1;    QString label_2;    Friend qdatastream& operator >> (qdatastream& so,datapackage& de);    Friend qdatastream& operator << (qdatastream& de,datapackage& so);

Then overload the operator

qdatastream& operator>> (qdatastream& so,datapackage& de) {    return so>>de.label_1> >de.label_2;} qdatastream& operator<< (qdatastream& de,datapackage& so) {    return de<<so.label_1< <so.label_2;}

So when the sender sends it

void Mainwindow::send_button () {    if (!this->socketa) {        this->ui->statusbar->showmessage ("Not a Socket ",";        return;    }    DataPackage package;    Package.label_1=this->ui->a1->text ();    Package.label_2=this->ui->a2->text ();    Qbytearray by;    Qdatastream DS (&by,qiodevice::writeonly);    ds<<package;    This->socketa->write (by);}

When the receiving party receives the

void Mainwindow::socketb_recv () {    qbytearray by= this->socketb->readall ();    Qdatastream DS (&by,qiodevice::readonly);    DataPackage package;    ds>>package;    This->ui->b1->settext (package.label_1);    This->ui->b2->settext (package.label_2);}


The above easily passes an object, overloaded operator << equivalent with serialization of this object. >> is equivalent to deserializing the object. This way, if the two parties write the protocol only need to construct a protocol package, and then each side of the serialization of the deserialization operation to communicate, with the QT default type will be more complex communication content, the use of more convenient.










Using the socket to transfer objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.