Results 1 to 5 of 5

Thread: Datastream not reading from socket as expected

  1. #1
    Join Date
    Jun 2016
    Posts
    3
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Datastream not reading from socket as expected

    I am trying to read values from a socket. when stepping through, everything works as expected until the line "in>>data;". After this line, data is a null pointer. The basic logic is set the socket, wait for enough bytes to read the size of the packet, get the packet size, then read the rest of the data once it's been sent. Socket is a QTcpSocket * and mServer is a QTcpServer *. Checkbytes is just for debug, and it returns the expected value. I have no idea why the datastream isn't populating data??

    Qt Code:
    1. socket = mServer->nextPendingConnection();
    2.  
    3. while (socket->bytesAvailable() < (int)sizeof(quint16))
    4. {
    5. if (!socket->waitForReadyRead(Timeout))
    6. {
    7. emit error(socket->error(), socket->errorString());
    8. return;
    9. }
    10. };
    11.  
    12. quint16 blockSize;
    13.  
    14. QDataStream in(socket);
    15. in.setVersion(QDataStream::Qt_5_6);
    16. in.setByteOrder(QDataStream::LittleEndian);
    17.  
    18. char buf[(int)sizeof(quint16)];
    19. socket->peek(buf, sizeof(buf));
    20. blockSize = (*(buf +1) <<8) | *(buf);
    21.  
    22. while (socket->bytesAvailable() < blockSize )
    23. {
    24. if (!socket->waitForReadyRead(Timeout))
    25. {
    26. emit error(socket->error(), socket->errorString());
    27. return;
    28. }
    29. };
    30.  
    31. int checkbytes = socket->bytesAvailable(); //this is the expected number
    32.  
    33. char* data;
    34. in >> data;
    35.  
    36. //datastream >> allocates using new - caller must destroy
    37. delete[] data;
    To copy to clipboard, switch view to plain text mode 
    Last edited by scoutycat; 9th June 2016 at 20:53.

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Datastream not reading from socket as expected

    You never allocate any memory to the data pointer, its value is indeterminate. You need to allocate memory to initialize the pointer.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Datastream not reading from socket as expected

    Quote Originally Posted by jefftee View Post
    You never allocate any memory to the data pointer, its value is indeterminate. You need to allocate memory to initialize the pointer.
    No, from QDataStream doc : Space for the string is allocated using new -- the caller must destroy it with delete[].

  4. #4
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Datastream not reading from socket as expected

    Oops, sorry for the incorrect advice! Never used QDataStream myself and didn't read the doc before replying!
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Datastream not reading from socket as expected

    QDataStream::operator>>(char *&) is expecting a 32 bit unsigned number of characters followed by that number of characters including the terminating nul byte. Here is a faked incoming packet and read:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QByteArray>
    3. #include <QBuffer>
    4. #include <QDataStream>
    5. #include <QDebug>
    6.  
    7. int main(int argc, char **argv)
    8. {
    9. QCoreApplication app(argc, argv);
    10.  
    11. // Fake data packet
    12. QByteArray packet;
    13. QDataStream out(&packet, QIODevice::WriteOnly);
    14. out.setVersion(QDataStream::Qt_5_5);
    15. out.setByteOrder(QDataStream::LittleEndian);
    16. out << "Payload";
    17. qDebug() << "Bytes available" << packet.size() << packet.toHex();
    18.  
    19. // Fake socket
    20. QBuffer *socket = new QBuffer(&packet, qApp);
    21. socket->open(QIODevice::ReadOnly);
    22.  
    23. // your code
    24. QDataStream in(socket);
    25. in.setVersion(QDataStream::Qt_5_5);
    26. in.setByteOrder(QDataStream::LittleEndian);
    27.  
    28. // need 4 bytes before this can work ---v
    29.  
    30. // look ahead for 32-bit string length, the overall block size is 4 bytes longer than the string
    31. char buf[(int)sizeof(quint32)];
    32. socket->peek(buf, sizeof(buf));
    33. int blockSize = (*(buf+3) <<24) | (*(buf+2) <<16) | (*(buf+1) <<8) | *(buf);
    34. blockSize += 4;
    35. qDebug() << "Block size" << blockSize;
    36.  
    37. // we need to accumulate blockSize bytes before the following can work
    38.  
    39. char *data = 0;
    40. in >> data;
    41. qDebug() << (void *)data;
    42. if (data)
    43. qDebug() << "String:" << data;
    44. delete[] data;
    45.  
    46. return 0;
    47. }
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to ChrisW67 for this useful post:

    scoutycat (22nd June 2016)

Similar Threads

  1. Errors when reading a QImage sent through a socket
    By volatile_ah in forum Qt Programming
    Replies: 5
    Last Post: 1st September 2009, 13:01
  2. Socket Reading causes CPU to max out
    By tntcoda in forum Qt Programming
    Replies: 3
    Last Post: 25th May 2009, 00:07
  3. Reading data from socket
    By ComaWhite in forum Qt Programming
    Replies: 1
    Last Post: 4th December 2008, 18:22
  4. Socket Reading Advice
    By tntcoda in forum Qt Programming
    Replies: 3
    Last Post: 4th July 2008, 11:26
  5. Reading from TCP Socket crashes program
    By OnionRingOfDoom in forum Qt Programming
    Replies: 26
    Last Post: 27th January 2006, 19:32

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.