View Full Version : Problem with QTcpSocket and QDataStream

15th September 2006, 09:49
The problem is that my run function is returning only the first few characters (up until the first whitespace actually) of the data that has arrived on the socket. So it returns


instead of

200 newsreader2.eweka.nl NNRP Service Ready - news.eweka.nl (posting ok)

Anyone know why? It works fine if I uncomment the readLine() function, but I'm trying to get it to work using a datastream.

void Connection::run()
QTcpSocket socket;
const int Timeout = timeout * 1000;
emit emitData( "run() commencing" );
quit = false;
socket.connectToHost( hostName, port );
if( !socket.waitForConnected( Timeout ) ){
emit emitData( "returning ( socket.waitForConnected )" );
emit emitData( "entering loop" );
if( !socket.waitForReadyRead( Timeout ) ){
emit emitData( "returning ( socket.waitForReadyRead )" );
QString data;
QTextStream dataStream( &socket );
// while( socket.canReadLine() ){
// emit emitData( socket.readLine() );
// emit emitData( QString::number( socket.bytesAvailable() ) );
// if( socket.waitForReadyRead( Timeout ) ){
// emit emitData( "returning ( socket.bytesAvailable )" );
// return;
// }
// }
dataStream >> data;
emit emitData( data );

15th September 2006, 10:22
QDataStream is very specific, it requires data to be formatted the way it likes it. I suggest you use QTextStream (but it'll stop at every whitespace) or use readLine() directly. If you really need/want QDataStream, use QDataStream::readRawData().

15th September 2006, 10:51
Thanks, just before you posted that I found out about QTextStream.readLine() :p

15th September 2006, 16:45
Thanks, just before you posted that I found out about QTextStream.readLine() :p

Note that using QTextStream is a bit slower than operating on the device directly. So if QTextStream::readLine() is the only method of that class you use, you could consider calling readLine on the socket instead.

16th September 2006, 14:08
Hmm, the reason why I am trying to stream the data is that because in the past with an earlier version of my program, I was getting strange errors when processing large amounts of data on the socket. I would list a newsgroup, and recieve the list of titles of the (in the form of the post number) from the newsserver. However, this only worked up until a certain number of posts. When listing large newsgroups, such as alt.binaries.boneless, after a while I would start getting an error. I can't remember what exactly, something along the lines of maxLen() > 2 ... I was processing the readLine() of the socket, adding the information directly into a QListWidget.