Results 1 to 13 of 13

Thread: read data from QtDatastream

  1. #1
    Join Date
    Feb 2018
    Posts
    24
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default read data from QtDatastream

    Good day,

    I have a server with the method for read data from database and send a client(If you have suggestion is very accepted)

    Qt Code:
    1. void ServerThings::readClient()
    2. {
    3. QTcpSocket *clientSocket = static_cast<QTcpSocket*>(sender());
    4. QDataStream in(clientSocket);
    5. //in.setVersion(QDataStream::Qt_5_10);
    6. for (;;)
    7. {
    8. if (!m_nNextBlockSize)
    9. {
    10. if (clientSocket->bytesAvailable() < sizeof(quint16)) { break; }
    11. in >> m_nNextBlockSize;
    12. }
    13.  
    14. if (clientSocket->bytesAvailable() < m_nNextBlockSize) { break; }
    15. QString str;
    16. in >> str;
    17.  
    18. emit gotNewMesssage(str);
    19.  
    20. m_nNextBlockSize = 0;
    21.  
    22. if (sendToClient(clientSocket, str) == -1)
    23. {
    24. qDebug() << "Some error occured";
    25. }
    26. }
    27. }
    28.  
    29.  
    30. qint64 ServerThings::sendToClient(QTcpSocket *socket, const QString &str)
    31. {
    32.  
    33.  
    34. QByteArray arrBlock;
    35. Q_ASSERT(socket);
    36. QDataStream out(&arrBlock,QIODevice::WriteOnly);
    37. QSqlQuery query;
    38. query.prepare("SELECT * FROM tabripa where Nbusta =(?)");
    39. query.bindValue(0, str);
    40. query.exec();
    41. while (query.next()) {
    42. const QSqlRecord record = query.record();
    43. for(int i=0; i < record.count(); i++)
    44. out << record.value(i).toString();
    45. }
    46. return socket->write(arrBlock);
    47.  
    48.  
    49. }
    To copy to clipboard, switch view to plain text mode 

    I now I should receive the data and transform the various records into (string, int, float etc) but I don't know how to do it, can you help me?

  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: read data from QtDatastream

    Your readClient() method is not very robust.

    If you receive the block size but not enough data to read the block then your method will interpret the next data as the next block size when new data arrives.

    You need to remember which state of the reading/decoding process you are in.

    As for sendToClient(): why do you write a string and not also use QDataStream?

    Cheers,
    _

  3. #3
    Join Date
    Feb 2018
    Posts
    24
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: read data from QtDatastream

    Quote Originally Posted by anda_skoa View Post

    As for sendToClient(): why do you write a string and not also use QDataStream?

    Cheers,
    _
    can you explain yourself better? I Send QbyteArray.

  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: read data from QtDatastream

    Right, sorry, got confused by the toString() calls.

    What you are looking for is to use the actual column data type instead of converting each field to string.
    Otherwise you have to parse the strings at the client side.

    Cheers,
    _

  5. #5
    Join Date
    Feb 2018
    Posts
    24
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: read data from QtDatastream

    yes I would like to do this but how can I send the real data Qdatastream if I am not mistaken it only accepts QbyteArray, could someone give me a small example? Also of only three columns of the database eg (Name "String") and (Nbusta "Int") see attached photo where a line of the ACCESS database is shown
    Cattura.jpg
    Attached Images Attached Images
    Last edited by Nio74; 13th March 2019 at 08:09.

  6. #6
    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: read data from QtDatastream

    Quote Originally Posted by Nio74 View Post
    yes I would like to do this but how can I send the real data Qdatastream if I am not mistaken it only accepts QbyteArray
    The "shift" operators (<< and >>) of QDataStream have several overloads.

    Quote Originally Posted by Nio74 View Post
    could someone give me a small example?
    Qt Code:
    1. QBuffer buffer;
    2.  
    3. {
    4. buffer.open(QIODevice::WriteOnly);
    5. QDataStream stream(&buffer);
    6.  
    7. double d = 3.1415;
    8. qint32 i = 42;
    9. QString s = "Hello World";
    10.  
    11. stream << d << i << s;
    12.  
    13. buffer.close();
    14. }
    15.  
    16. {
    17. buffer.open(QIODevice::ReadOnly);
    18. QDataStream stream(&buffer);
    19.  
    20. double d = 0;
    21. qint32 i = 0;
    22.  
    23. stream >> d >> i >> s;
    24.  
    25. buffer.close();
    26.  
    27. qDebug() << "d=" << d << ", i=" << i << ", s=" << s;
    28. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  7. #7
    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: read data from QtDatastream

    QDataStream have operators << and >> for for all basic data types.

  8. #8
    Join Date
    Feb 2018
    Posts
    24
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: read data from QtDatastream

    Thanks for your patience. Then it would be better if I extract all the server data I insert myself into variables and send them directly to clients without going through QbyteArray? Afterwards in the Client can I receive them separately?

  9. #9
    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: read data from QtDatastream

    The important part is to convert the values from the query into the actual data type before writing into the data stream.

    You likely also want a "header" like in the other direction, e.g. a number that indicates the size of the message to wait for before decoding.

    Cheers,
    _

  10. #10
    Join Date
    Feb 2018
    Posts
    24
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: read data from QtDatastream

    You likely also want a "header" like in the other direction, e.g. a number that indicates the size of the message to wait for before decoding.

    Cheers,
    _
    I will try to redo the server, can you give an example of the client method? So I'm sure I'm on the right track,Tank
    Last edited by Nio74; 13th March 2019 at 11:26.

  11. #11
    Join Date
    Feb 2018
    Posts
    24
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: read data from QtDatastream

    {
    buffer.open(QIODevice::ReadOnly);
    QDataStream stream(&buffer);

    double d = 0;
    qint32 i = 0;
    QString s;

    stream >> d >> i >> s;

    buffer.close();

    qDebug() << "d=" << d << ", i=" << i << ", s=" << s;
    }
    Sorry I did not see


    Added after 16 minutes:


    should I always use the QByteArray for send data at client?

    Cattura.JPG
    Last edited by Nio74; 13th March 2019 at 14:29.

  12. #12
    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: read data from QtDatastream

    First of all don't use construction like this :
    Qt Code:
    1. query.exec("SELECT * FROM...");
    2. query.record().value(0)...
    3. query.record().value(1)...
    4. ...
    To copy to clipboard, switch view to plain text mode 
    Such SELECT does not guarantee the order of columns. Use
    Qt Code:
    1. query.exec("SELECT code,pCost,pPublic FROM...");
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. query.record().value("cost")...
    To copy to clipboard, switch view to plain text mode 
    Second socket is only a pipe. You can not guarantee that one socket.write () operation corresponds to one socket.read () operation. You must provide logic that will pick up the packets of bytes correctly, glue them and interpret them.

  13. #13
    Join Date
    Feb 2018
    Posts
    24
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: read data from QtDatastream

    I did so and It's Work )))

    side Server:

    Qt Code:
    1. qint64 ServerThings::sendToClient(QTcpSocket *socket, const QString &str)
    2. {
    3.  
    4.  
    5. QByteArray byteArray;
    6. // QBuffer buffer(&byteArray);
    7.  
    8. //buffer.open(QIODevice::WriteOnly);
    9. QDataStream stream(&byteArray,QIODevice::WriteOnly);
    10. QSqlQuery query;
    11. query.prepare("SELECT Nbusta,costo,vendita FROM tabripa where Nbusta =(?)");
    12. query.bindValue(0, str);
    13. query.exec();
    14. while (query.next()){
    15. if(query.isValid()){
    16. code = query.record().value("Nbusta").toInt();
    17. pCost = query.record().value("costo").toDouble();
    18. pPublic = query.record().value("vendita").toDouble();
    19.  
    20. stream << code <<pCost << pPublic ;
    21.  
    22. //buffer.close();
    23. }
    24.  
    25. }
    26.  
    27. return socket->write(byteArray);
    28.  
    29.  
    30. }
    To copy to clipboard, switch view to plain text mode 

    side Client:

    Qt Code:
    1. void ClientServices::readyRead()
    2. {
    3. QDataStream in(tcpSocket);//QtcpSocket
    4.  
    5. int code = 0;
    6. double pCost = 0;
    7. double pPublic = 0;
    8.  
    9. in >> code >> pCost >> pPublic;
    10.  
    11. qDebug() << code << pCost << pPublic;
    12.  
    13.  
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 
    I did not use the Qbuffer because on the client I am not able to integrate it, but if I use it on the server side and on the client no the data is received the same. How does it look? We accept suggestions!

Similar Threads

  1. Can't read map data
    By bikonja in forum Newbie
    Replies: 8
    Last Post: 16th July 2015, 16:46
  2. Serial read misses to read data from the serial port
    By mania in forum Qt for Embedded and Mobile
    Replies: 11
    Last Post: 18th August 2014, 09:49
  3. read data from website
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 4
    Last Post: 27th June 2013, 12:32
  4. Read data from txt file
    By wiatrak11 in forum Newbie
    Replies: 4
    Last Post: 10th March 2013, 17:50
  5. can`t read a binary data!
    By blm in forum Qt Programming
    Replies: 8
    Last Post: 18th September 2008, 17:56

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.