PDA

View Full Version : socket read/write bytes



nowire75
3rd July 2007, 21:42
Hi at all!,

I want to write (an after read) an bytes array to the TCP socket.
I try with this code:


QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_1);

QString variable="CIAO";

out << quint16(0) << variable;

out.device()->seek(0);
out << quint16(block.size() - sizeof(quint16));
tcpSocket.write(block);

this code work, but the problem is that I not read only "CIAO" on my receiver program.
I'm waiting to receive 6 bytes... "04CIAO"
I must change the variable type?

nowire

wysota
4th July 2007, 00:18
Don't use QDataStream, it's a serialization mechanism, not a general purpose binary stream.

nowire75
4th July 2007, 21:40
must I use QTextStream? Can you post an example?

I want to make a block-oriented application.

nowire

wysota
4th July 2007, 23:12
Don't use a stream at all.