Results 1 to 2 of 2

Thread: Sending simple object via udp...with a twist

  1. #1
    Join Date
    Jun 2007
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Sending simple object via udp...with a twist

    OKAY.

    Before I dig into my issue,

    1. I'm new to bit-fiddling in Qt, let alone bit-fiddling in C++
    2. I'm new to the forum
    3. I'm not sure if this falls under more of a Qt issue or a C++ issue (you tell me!)

    Onward with my issue. I'm attempting to send a user-defined object using QUdp to a destination legacy system. Unfortunately, the legacy system is extremely picky about the format of the data contained. The system validates the incoming packet by its structure and contents contained, down to the bit. The system on the receiving end is expecting user data in the following format:

    Member1: limited to 4 bits, where its binary value "means something"
    Member2: limited to 1 bit, where its binary value "means something"
    Member3: limited to 1 bit, where its binary value "means something"
    Member4: limited to 1 bit, where its binary value "means something"
    Member5: limited to 1 bit, where its binary value "means something"

    Now for the twist. These members have to packed into this single byte; no padding, no alignment. So far, I've had no issues in creating the object or sending it via udp to its' destination, but the packet comes in spread out over 5 bytes (verse the one byte that I need).

    A chunk of my code is as follows:

    ----------------------------

    //Define the structure
    typedef struct Output {
    unsigned char a : 4; //4 bits
    unsigned char b : 1; //1 bit
    unsigned char c : 1; //1 bit
    unsigned char d : 1; //1 bit
    unsigned char e : 1; //1 bit
    };

    //Create new instance of Output as Testing
    Output Testing;

    //Set member fields
    Testing.a = 2;
    Testing.b = 0;
    Testing.c = 1;
    Testing.d = 1;
    Testing.e = 0;

    //Declare stream and udp-necessary variables
    QUdpSocket *socketOutput = new QUdpSocket(this);
    QHostAddress DestinationAddress;
    QByteArray OutputDatagram;
    QDataStream out(&OutputDatagram, QIODevice::ReadWrite);

    //Set params
    out.setByteOrder(QDataStream::LittleEndian);
    DestinationAddress.setAddress("127.0.0.1"); //Loopback for testing purposes

    //Set parameters and send the message
    out.setVersion(QDataStream::Qt_4_1);
    out << Testing.a;
    out << Testing.b;
    out << Testing.c;
    out << Testing.d;
    out << Testing.e;

    //Write the datagram
    socketOutput->writeDatagram(OutputDatagram, OutputDatagram.size(), DestinationAddress, 1790);

    ----------------------------------

    Now given this, member a needs to fit inside of the first 8 bits (given the value I'm assigning to it in the code above, the binary equivalent should be 0010). The remaining members should fit into the remaining 8 bits of the byte (0110??). However, the packet on the receiving end reads something more like:

    For Member1: 0000 0010 //1 byte
    For Member2: 0000 0000 //1 byte
    For Member3: 0000 0001 //1 byte
    For Member4: 0000 0001 //1 byte
    For Member 5: 0000 0000 //1 byte

    vs.

    For Member1, Member2, Member3, Member4, and Member5: 0110 0010 //1 byte

    I'm convinced that my issue(s) either lie in the type I'm currently using for my structure members (unsigned char -- I've tried most of the other data types that QDataStream will accept) or in the way that QDataStream is handling them when written. But alas, after trying this and that and everything in between, I'm still stumped here.

    Any thoughts?; and thank you in advance (let me know if there's ANTYHING I can do to clarify what I'm going for and what is actually happening)!

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Sending simple object via udp...with a twist

    cant you just use a quint8 for 8 bits or quint16 for all bits?
    Qt Code:
    1. //Define the structure
    2. typedef struct Output {
    3. unsigned char a : <val>; //8 (4+1+1+1+1) bits
    4. unsigned char b : <val>; //1 bit
    5. };
    To copy to clipboard, switch view to plain text mode 
    you might look QBitArray up, see how it behaves when piped through sockets
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. qt network performance
    By criss in forum Qt Programming
    Replies: 16
    Last Post: 24th July 2006, 09:23

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.