PDA

View Full Version : How to write to a TCPSocket with a QTextStream?



Denarius
8th May 2009, 11:08
Hi,
I can't figure out why it doesn't send my data over the socket.
This is my code.

buffer is a class which contains a Qlist with myStructs in it.
This is because two threads are working with the buffer.
One adds the other gets.



void MyClass::WriteData{
myStruct package;

while(!buffer->isEmptyMyStruct()){
msleep(20);
package = buffer->getMyStruct();

QByteArray block;
QTextStream out(&block, QIODevice::ReadWrite);

out << package;
out.flush();

tcpSocket->write(block);
tcpSocket->flush();

emit message(package.message);
emit message(package.id);
}
}

mcosta
8th May 2009, 12:19
To write binary data use QDataStream instead QtextStream.

Denarius
8th May 2009, 13:28
To write binary data use QDataStream instead QtextStream.

I'm not writing the data as binary but as text.
I figured out that it's a lot easier to just write to the socket directly. This solved my problem

wysota
8th May 2009, 13:28
What does write() return?