PDA

View Full Version : QDataStream and readBytes



pdoria
18th January 2008, 18:43
Hi,

In the wake of this thread (http://www.qtcentre.org/forum/f-qt-programming-2/t-qbytearray-revisited-11275-post60287.html#post60287) thread I'm trying to read binary data from a socket, hence the code:


unsigned int totalBytes = 0;
totalBytes = tcpSocket.bytesAvailable();
if (totalBytes < 4) return; // bogus tx...

uint headerSize=sizeof(uint);
char * header = new char [headerSize];

uint headerBytes=0;
QDataStream inData(&tcpSocket);

inData.readBytes(header, headerSize);
headerBytes=atoi(header);


Unless I really don't know what I'm doing... ;) (most probably) why does inData.readBytes(header, headerSize); always cause a segfault?

Any pointers welcome.
Thanks in advance,
Pedro Doria Meunier.

jacek
18th January 2008, 18:48
Use QDataStream::readRawData(). readBytes() is a complement of writeBytes() and it assumes that the data is in certain format (it also allocates the buffer itself).

Won't it be simplier to write:
uint headerBytes=0;
QDataStream inData(&tcpSocket);
inData >> headerBytes;?

pdoria
18th January 2008, 18:57
Txs Jacek,

I already tried that only to discover that these devices are stupid enough to send the header and data all at once, so in >> is not an option. I have to read the 1st bytes of what comes to determine the length of the following data... :(

Kind regards,
Pedro Doria Meunier.

jacek
18th January 2008, 19:05
I already tried that only to discover that these devices are stupid enough to send the header and data all at once, so in >> is not an option.
Why?


quint16 header;
inData >> header;
will read exactly two bytes from the stream into header, leaving the rest intact. Then you can check the header and read more data.

pdoria
18th January 2008, 19:49
Jacek,

You're absolutely right. This is working now. I don't remember what I did last time... :o
Man... what a ride!!! :D
I sincerely want to thank you for your valuable support... thank you!

Kind regards,
Pedro Doria Meunier.

pdoria
18th January 2008, 20:16
Ok. last step:

this doesn't work...



quint16 dataLength=0;

QDataStream inData(&tcpSocket);

while (zerocount < 4 && dataLength==0)
{
inData >> dataLength;
if (dataLength==0) zerocount++;
}

// exceed number of tries... bogus txs. return.
if (zerocount==3) return;

char * data = new char[dataLength];
inData >> data;


the data is always 0x0 ... :confused:
So ... please tell me... after in >> dataLength the pointer in the stream is advanced or not?
the operator>> ( char *& s ) is able to convert the binary data into char?

Thanks in advance.

pdoria
19th January 2008, 13:11
bump.

Must one do this?



char * data = new char[dataLength];
qint8 c;
int n;
for (n=0; n<dataLength; n++) {
inData >> c;
data[n]=c;
}


It feels a bit archaic...

jacek
19th January 2008, 15:31
Must one do this?
No, you can use readRawData() instead.

wysota
20th January 2008, 11:22
Be aware that QDataStream is not a general purpose binary stream but a serialization method. Don't use it if you don't need it.