Results 1 to 5 of 5

Thread: Problem with reading in data from a QTCPSocket

  1. #1
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem with reading in data from a QTCPSocket

    Hello,
    I'm getting data from a server in the format:
    char* identifier
    double timestamp
    char* message
    double n1
    double n2
    double n3

    When I try to read it, i get 0 bytes.

    This is my piece of code:
    Qt Code:
    1. void MyClass::ReadData{
    2. while(m_TcpSocket->bytesAvailable() > 0){
    3. blockSize = m_TcpSocket->bytesAvailable();
    4. QDataStream in(m_TcpSocket);
    5. in.setVersion(QDataStream::Qt_4_0);
    6.  
    7. QVariant value;
    8. QString package;
    9. char* id;
    10. double time;
    11. char* message;
    12. double n1,n2,n3;
    13.  
    14. in >> id;
    15. in >> time;
    16. in >> message;
    17. in >> n1 >> n2 >> n3;
    18.  
    19. package = id;
    20. value = time;
    21. package.append(value.toString());
    22.  
    23. emit messageReady(package);
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

    messageReady just appends the string into a textfield/browserfield.

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with reading in data from a QTCPSocket

    In general you must check how many bytes are available before you start reading. You do it but you don't check if the blockSize value is enough to contain data you read.

    Have you connected the readin from the socket to the QIODevice::readyRead() signal?
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with reading in data from a QTCPSocket

    Quote Originally Posted by mcosta View Post
    In general you must check how many bytes are available before you start reading. You do it but you don't check if the blockSize value is enough to contain data you read.

    Have you connected the readin from the socket to the QIODevice::readyRead() signal?
    Yes the ReadData is only executed when a readyRead is received

  4. #4
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with reading in data from a QTCPSocket

    are you getting data from your another application? I mean can you change your package structure a bit? Because good thing would be to send at the beginning of your package one int which value would be equal to the size of the whole package. Then if bytesAvailable() is less you are waiting fo anothe part of data to come and if its equal or greater it means you had received whole package and can read it correctly. What's more, I'm a bit suspicius that reading char * in your way can be not so good but i'm not sure...
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  5. #5
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with reading in data from a QTCPSocket

    I found the problem.
    I was trying to read it with the wron stream.
    I should be reading it with QTextStream.

Similar Threads

  1. Replies: 1
    Last Post: 3rd December 2008, 15:51
  2. wrong syntax or QTcpSocket problem?
    By vito49 in forum Newbie
    Replies: 3
    Last Post: 8th October 2008, 08:12
  3. reading utf8 as data stream.
    By kami in forum Newbie
    Replies: 1
    Last Post: 28th September 2008, 21:48
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. QTcpSocket disconnection problem
    By erdi in forum Qt Programming
    Replies: 4
    Last Post: 19th February 2006, 21:50

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.