how do i send and receive a classType over aTcp network in Qt.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ssmny
    New Member
    • Jul 2010
    • 7

    how do i send and receive a classType over aTcp network in Qt.

    Qt problem
    Hello all,
    i would like to know how i can prepair(or encode) a class type for sending over a TCP connection. and when sent, how do i decode that info at the other end. That should be done in Qt (c++).
    Thanks
    I am sorry, i dont have any example, but if you have any that explains this situation, i will be very greatifull.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Serialise (encode) or marshal the class data into a byte array. Send the array then at the other end deserialise (decode) or unmarshal the received byte array back into a class.

    Comment

    • ssmny
      New Member
      • Jul 2010
      • 7

      #3
      thanks banfa,
      could u please give me an example of how i would do that. am not familia with Qt. I have have even failed getting a start.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Serialisation is not as complex as it sounds, you just split the data up into bytes so for a 4 byte long variable it would look something like

        Code:
        long startValue, endvalue;
        unsigned char buffer[4];
        
        //Serialise to big endian data buffer
        startValue = 54321;
        
        buffer[0] = (unsigned char)((startValue >> 24) & 0xFF);
        buffer[1] = (unsigned char)((startValue >> 16) & 0xFF);
        buffer[2] = (unsigned char)((startValue >> 8) & 0xFF);
        buffer[3] = (unsigned char)(startValue & 0xFF);
        
        //Deserialise from big endian data buffer
        endvalue  = ((long)buffer[0]) << 24;
        endvalue |= ((long)buffer[1]) << 16;
        endvalue |= ((long)buffer[2]) << 8;
        endvalue |= (long)buffer[3];
        Just do that for all the POD (plain old data) variables in the class and similarly in a recursive fashion for any composite types the class holds.

        Comment

        Working...