PDA

View Full Version : Using QDataStream / QTcpSocket - ReadPastEnd failure



oheuser
22nd February 2017, 15:36
Hi,

supposably I'm facing a minor problem by reading from an underlying socket via QDataStream.
Writing through the data stream runs quite well, like seen here:


QByteArray block;
QDataStream out( &block, QIODevice::WriteOnly );
out.setDevice( m_TcpSocket );
out.setVersion( QDataStream::Qt_5_0 );
out << "HELLO CLIENT!\x0d\x0a";

But reading fails in getting a ReadPastEnd - status, when reading takes effect by using QDataStream.


QByteArray block;
QDataStream in( m_TcpSocket );
in.setVersion( QDataStream::Qt_5_0 );
in >> block;

Other way round, using only
QByteArray data = m_TcpSocket->readAll(); reads everything needed.

I was also working through the Fortune example. But here the server/client will always close the connection, which in my case is not reasonable.
Any guesses? Many thanks in advance.

Br,
Oliver

anda_skoa
23rd February 2017, 08:35
You first need to ensure that enough data is available.

A TCP connection is a byte stream, data can arrive in chunks of any size.

Cheers,
_

oheuser
23rd February 2017, 10:06
Hi,

I'm aware of that, but from my point of view the state doesn't indicate what you describe. For me it seems that the stream was read correctly via QDataStream and for some reason not moved to the given variable. If, i.e., a following call to m_TcpSocket->readAll() is invoked there is no more data since it was read before.

Do you know the minimum requirement of data being sent that the stream can read something in?

Cheers,

anda_skoa
24th February 2017, 09:55
Well, the error code indicates that the data that was expected wasn't there.

The byte array is serializied with a length followed by the actual data, so my guess is that after reading the length the available data wasn't long enough to match that requirement.
Likely caused by some data not having been received yet.

Cheers,
_