Results 1 to 5 of 5

Thread: Send Int and double QDatastream trought QTCpSocket

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

    Default Send Int and double QDatastream trought QTCpSocket

    Good day, I have send data Int,string ,and double, but i cun't recieve them correctly.

    thys is mt button Qml where it have start the event:

    Qt Code:
    1. CustomButton {
    2. id: btnSalva
    3. x: 0
    4. color: qsTr("#ffffff")
    5. text: "SALVA"
    6. anchors.top: parent.top
    7. anchors.topMargin: 50
    8. anchors.right: parent.right
    9. anchors.rightMargin: 50
    10. onClicked: {
    11.  
    12. backend.connectClicked()
    13. backend.sendUpdateRiparazione(3,txtCodice.text,txtPCosto.text,txtPPub.text);
    14.  
    15.  
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    this is the metod that send at server:

    Qt Code:
    1. void Backend::sendUpdateRiparazione(qint16 type,qint16 codice, double pCosto, double pPubblico)
    2. {
    3. //client->connect2host();
    4. QByteArray arrBlock;
    5. QDataStream out(&arrBlock, QIODevice::WriteOnly);
    6. //out.setVersion(QDataStream::Qt_5_10);
    7. //out <<qint16(0) << type << codice << pCosto << pPubblico ;
    8. out << qint16(0);
    9. out << (qint16)type;
    10. out << (qint16)codice;
    11. out << (double)pCosto;
    12. out << (double)pPubblico;
    13.  
    14. // out << msg ;
    15.  
    16. out.device()->seek(0);
    17. out << quint16(arrBlock.size() - sizeof(quint16));
    18.  
    19. client->tcpSocket->write(arrBlock);
    20.  
    21.  
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

    this is Server:

    Qt Code:
    1. void ClientResearch::readClient()
    2. {
    3. QTcpSocket *clientSocket = static_cast<QTcpSocket*>(sender());
    4. QDataStream in(clientSocket);
    5.  
    6.  
    7.  
    8. for (;;)
    9. {
    10. if (!m_nNextBlockSize)
    11. {
    12. if (clientSocket->bytesAvailable() < sizeof(quint16)) { break; }
    13. in >> m_nNextBlockSize;
    14. }
    15. if (clientSocket->bytesAvailable() < m_nNextBlockSize) { break; }
    16. QString str;
    17. qint16 type;
    18.  
    19. emit gotNewMesssage(str);
    20.  
    21. m_nNextBlockSize = 0;
    22.  
    23.  
    24. in >> type ;
    25.  
    26.  
    27. switch (type) {
    28.  
    29. case 1:if(type == 1){
    30. in >> str;
    31. sendToClient(clientSocket, str);
    32. qDebug()<< "chiamata uno ";
    33. break;
    34.  
    35. }
    36.  
    37. case 2:if(type == 2){
    38. in >> str;
    39. sendMag(clientSocket, str);
    40. break;
    41.  
    42. }
    43. case 3:if(type ==3){
    44. qDebug()<< "chiamata tre ";
    45. qint64 c_codice;
    46. double p_prezzoCosto;
    47. double p_prezzoPubblico;
    48.  
    49. in >> c_codice >> p_prezzoCosto >> p_prezzoPubblico;
    50.  
    51. qDebug() <<c_codice << p_prezzoCosto << p_prezzoPubblico <<"evviva";
    52. updateRiparazione(c_codice,p_prezzoCosto,p_prezzoPubblico);
    53.  
    54.  
    55. }
    56. break;
    57.  
    58. }
    59.  
    60.  
    61. }
    62. }
    To copy to clipboard, switch view to plain text mode 

    the data recived are these,the only variable correct is "type" codice should be 82253, p_prezzoCosto 3873,p_prezzoPubblico 9681
    Cattura.JPG
    Last edited by Nio74; 24th March 2019 at 17:55.

  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: Send Int and double QDatastream trought QTCpSocket

    qint16 means "signed 16-bit integer".
    qint64 means "signed 64-bit integer".


    So you write 16 bit (2 bytes) and then read 64 bits (8 bytes).

    So c_codice contains the 2 bytes from codice and 6 bytes from pCosto

    General recommendation: this is way easier to debug if you don't have two processes and network between them.
    Write a unit test that checks serialization and deserialization actually match each other.

    Cheers,
    _

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

    Default Re: Send Int and double QDatastream trought QTCpSocket

    Quote Originally Posted by anda_skoa View Post
    qint16 means "signed 16-bit integer".
    qint64 means "signed 64-bit integer".


    So you write 16 bit (2 bytes) and then read 64 bits (8 bytes).

    So c_codice contains the 2 bytes from codice and 6 bytes from pCosto

    Cheers,
    _
    I have tryed at work all qint64 but only"type"work fine:

    Cattura2.JPG

    client:

    Qt Code:
    1. void Backend::sendUpdateRiparazione(qint64 type,qint64 codice, qint64 pCosto, qint64 pPubblico)
    2. {
    3. //client->connect2host();
    4. QByteArray arrBlock;
    5. QDataStream out(&arrBlock, QIODevice::WriteOnly);
    6. //out.setVersion(QDataStream::Qt_5_10);
    7. out << qint64(0) << type << codice << pCosto << pPubblico ;
    8.  
    9. out.device()->seek(0);
    10. out << quint64(arrBlock.size() - sizeof(quint64));
    11.  
    12. client->tcpSocket->write(arrBlock);
    13.  
    14.  
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

    server:
    Qt Code:
    1. void ClientResearch::readClient()
    2. {
    3. QTcpSocket *clientSocket = static_cast<QTcpSocket*>(sender());
    4. QDataStream in(clientSocket);
    5.  
    6.  
    7. for (;;)
    8. {
    9. if (!m_nNextBlockSize)
    10. {
    11. if (clientSocket->bytesAvailable() < sizeof(quint64)) { break; }
    12. in >> m_nNextBlockSize;
    13. }
    14. if (clientSocket->bytesAvailable() < m_nNextBlockSize) { break; }
    15. QString str;
    16. qint64 type;
    17.  
    18. emit gotNewMesssage(str);
    19.  
    20. m_nNextBlockSize = 0;
    21.  
    22.  
    23. in >> type ;
    24.  
    25.  
    26. switch (type) {
    27.  
    28. case 1:if(type == 1){
    29. in >> str;
    30. sendToClient(clientSocket, str);
    31. qDebug()<< "chiamata uno ";
    32. break;
    33.  
    34. }
    35.  
    36. case 2:if(type == 2){
    37. in >> str;
    38. sendMag(clientSocket, str);
    39. break;
    40.  
    41. }
    42. case 3:if(type ==3){
    43. qDebug()<< "chiamata tre ";
    44. // qint16 type;
    45. qint64 c_codice;
    46. qint64 p_prezzoCosto;
    47. qint64 p_prezzoPubblico;
    48.  
    49. in >> c_codice >> p_prezzoCosto >> p_prezzoPubblico;
    50.  
    51. // qDebug() <<c_codice << p_prezzoCosto << p_prezzoPubblico <<"evviva";
    52. // updateRiparazione(c_codice,p_prezzoCosto,p_prezzoPubblico);
    53.  
    54.  
    55. }
    56. break;
    57.  
    58. }
    59.  
    60.  
    61. }
    62. }
    To copy to clipboard, switch view to plain text mode 


    Unit Testin,I don't know how to do it. I should learn. I know where to find all the material. Do you have any ideas? Thanks for helping out with patience.

    General recommendation: this is way easier to debug if you don't have two processes and network between them.
    Write a unit test that checks serialization and deserialization actually match each other.
    Last edited by Nio74; 25th March 2019 at 07:26.

  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: Send Int and double QDatastream trought QTCpSocket

    Quote Originally Posted by Nio74 View Post
    Unit Testin,I don't know how to do it. I should learn. I know where to find all the material. Do you have any ideas? Thanks for helping out with patience.
    https://doc.qt.io/qt-5/qttestlib-tutorial1-example.html

    Cheers,
    _

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

    Default Re: Send Int and double QDatastream trought QTCpSocket

    I have solved So:

    Client:

    Qt Code:
    1. void Backend::sendUpdateRiparazione(qint64 type,qint32 codice, double pCosto, double pPubblico)
    2. {
    3. QDataStream out(client->tcpSocket);
    4. out << type << codice << pCosto << pPubblico;
    5. }
    To copy to clipboard, switch view to plain text mode 

    Server:

    Qt Code:
    1. void ClientResearch::readClient()
    2. {
    3. Q_ASSERT(dynamic_cast<QTcpSocket*>(sender()));
    4. QTcpSocket *clientSocket = static_cast<QTcpSocket*>(sender());
    5. QDataStream in(clientSocket);
    6. while (clientSocket->bytesAvailable())
    7.  
    8. {
    9.  
    10. in.startTransaction();
    11. qint64 type;
    12. in >> type;
    13. switch (type) {
    14. case 1:{
    15. qDebug()<< "caso 1";
    16. qint32 code;
    17. in >> code;
    18. if (!in.commitTransaction())
    19. return;
    20. sendToClient(clientSocket, code);
    21. break;
    22. }
    23. case 2:{
    24. QString str;
    25. in >> str;
    26. if (!in.commitTransaction())
    27. return;
    28. sendMag(clientSocket, str);
    29. break;
    30. }
    31. case 3:{
    32. qint32 c_codice;
    33. double p_prezzoCosto;
    34. double p_prezzoPubblico;
    35. in >> c_codice >> p_prezzoCosto >> p_prezzoPubblico;
    36. if (!in.commitTransaction())
    37. return;
    38. updateRiparazione(c_codice ,p_prezzoCosto ,p_prezzoPubblico);
    39. break;
    40. }
    41. }
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTcpSocket QDataStream QByteArray
    By Grimlock in forum Newbie
    Replies: 1
    Last Post: 14th December 2009, 23:47
  2. Send a Image throw a QDataStream
    By gt.beta2 in forum Newbie
    Replies: 5
    Last Post: 21st March 2009, 00:07
  3. QDataStream and QTcpSocket
    By babu198649 in forum Newbie
    Replies: 2
    Last Post: 24th January 2009, 13:29
  4. QTcpSocket and QDataStream
    By December in forum Qt Programming
    Replies: 1
    Last Post: 22nd March 2008, 15:13
  5. Problem with QTcpSocket and QDataStream
    By Valheru in forum Qt Programming
    Replies: 4
    Last Post: 16th September 2006, 14:08

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.