PDA

View Full Version : Sending simple object via udp...with a twist



the scribe
6th June 2007, 03:00
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)!

high_flyer
6th June 2007, 10:16
cant you just use a quint8 for 8 bits or quint16 for all bits?


//Define the structure
typedef struct Output {
unsigned char a : <val>; //8 (4+1+1+1+1) bits
unsigned char b : <val>; //1 bit
};

you might look QBitArray up, see how it behaves when piped through sockets