Results 1 to 4 of 4

Thread: Using QDataStream / QTcpSocket - ReadPastEnd failure

  1. #1
    Join Date
    Feb 2017
    Location
    Duesseldorf, Germany
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Using QDataStream / QTcpSocket - ReadPastEnd failure

    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:

    Qt Code:
    1. QByteArray block;
    2. QDataStream out( &block, QIODevice::WriteOnly );
    3. out.setDevice( m_TcpSocket );
    4. out.setVersion( QDataStream::Qt_5_0 );
    5. out << "HELLO CLIENT!\x0d\x0a";
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. QByteArray block;
    2. QDataStream in( m_TcpSocket );
    3. in.setVersion( QDataStream::Qt_5_0 );
    4. in >> block;
    To copy to clipboard, switch view to plain text mode 

    Other way round, using only
    Qt Code:
    1. QByteArray data = m_TcpSocket->readAll();
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QDataStream / QTcpSocket - ReadPastEnd failure

    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,
    _

  3. #3
    Join Date
    Feb 2017
    Location
    Duesseldorf, Germany
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11

    Default Re: Using QDataStream / QTcpSocket - ReadPastEnd failure

    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,

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QDataStream / QTcpSocket - ReadPastEnd failure

    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,
    _

Similar Threads

  1. QTcpSocket.readLine causing segmentation failure
    By hasnain in forum Qt Programming
    Replies: 1
    Last Post: 4th September 2010, 16:00
  2. QTcpSocket QDataStream QByteArray
    By Grimlock in forum Newbie
    Replies: 1
    Last Post: 14th December 2009, 22:47
  3. QDataStream and QTcpSocket
    By babu198649 in forum Newbie
    Replies: 2
    Last Post: 24th January 2009, 12:29
  4. QTcpSocket and QDataStream
    By December in forum Qt Programming
    Replies: 1
    Last Post: 22nd March 2008, 14:13
  5. Problem with QTcpSocket and QDataStream
    By Valheru in forum Qt Programming
    Replies: 4
    Last Post: 16th September 2006, 13:08

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.