Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to send different data:Int ,double, string, to server
Forum Updated to NodeBB v4.3 + New Features

how to send different data:Int ,double, string, to server

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • VRoninV VRonin

    Try this:

    void Backend::sendUpdateRiparazione(qint64 type,qint64 codice, double pCosto, double pPubblico)
    {
        QDataStream out(client->tcpSocket);
        out << qint64(0) << type << codice << pCosto << pPubblico;
    }
    
    void ClientResearch::readClient()
    {
        Q_ASSERT(dynamic_cast<QTcpSocket*>(sender()));
        QTcpSocket *clientSocket = static_cast<QTcpSocket*>(sender());
        QDataStream in(clientSocket);
        for (;;) {
            in.startTransaction();
            qint64 type;
            in >> type;
            switch (type) {
            case 1:{
                QString str;
                in >> str;
                if (!in.commitTransaction())
                    return;
                sendToClient(clientSocket, str);
                break;
            }
            case 2:{
                QString str;
                in >> str;
                if (!in.commitTransaction())
                    return;
                sendMag(clientSocket, str);
                break;
            }
            case 3:{
                qint64 c_codice;
                double p_prezzoCosto;
                double p_prezzoPubblico;
                in >> c_codice >> p_prezzoCosto >> p_prezzoPubblico;
                if (!in.commitTransaction())
                    return;
                doSomethingWithData(c_codice ,p_prezzoCosto ,p_prezzoPubblico);
                break;
            }
            }
        }
    }
    

    For an explanation on how transactions work see the description of ChatClient::onReadyRead of this example

    Nio74N Offline
    Nio74N Offline
    Nio74
    wrote on last edited by
    #8

    @VRonin said in how to send different data:Int ,double, string, to server:

    Try this:

    void Backend::sendUpdateRiparazione(qint64 type,qint64 codice, double pCosto, double pPubblico)
    {
        QDataStream out(client->tcpSocket);
        out << qint64(0) << type << codice << pCosto << pPubblico;
    }
    
    void ClientResearch::readClient()
    {
        Q_ASSERT(dynamic_cast<QTcpSocket*>(sender()));
        QTcpSocket *clientSocket = static_cast<QTcpSocket*>(sender());
        QDataStream in(clientSocket);
        for (;;) {
            in.startTransaction();
            qint64 type;
            in >> type;
            switch (type) {
            case 1:{
                QString str;
                in >> str;
                if (!in.commitTransaction())
                    return;
                sendToClient(clientSocket, str);
                break;
            }
            case 2:{
                QString str;
                in >> str;
                if (!in.commitTransaction())
                    return;
                sendMag(clientSocket, str);
                break;
            }
            case 3:{
                qint64 c_codice;
                double p_prezzoCosto;
                double p_prezzoPubblico;
                in >> c_codice >> p_prezzoCosto >> p_prezzoPubblico;
                if (!in.commitTransaction())
                    return;
                doSomethingWithData(c_codice ,p_prezzoCosto ,p_prezzoPubblico);
                break;
            }
            }
        }
    }
    

    For an explanation on how transactions work see the description of ChatClient::onReadyRead of [this example](https://wiki.qt.io/WIP-How_to_create_a_simple_chat_applicatio

    Don't work I have to finds some example 🧐

    VRoninV 1 Reply Last reply
    0
    • Nio74N Nio74

      @VRonin said in how to send different data:Int ,double, string, to server:

      Try this:

      void Backend::sendUpdateRiparazione(qint64 type,qint64 codice, double pCosto, double pPubblico)
      {
          QDataStream out(client->tcpSocket);
          out << qint64(0) << type << codice << pCosto << pPubblico;
      }
      
      void ClientResearch::readClient()
      {
          Q_ASSERT(dynamic_cast<QTcpSocket*>(sender()));
          QTcpSocket *clientSocket = static_cast<QTcpSocket*>(sender());
          QDataStream in(clientSocket);
          for (;;) {
              in.startTransaction();
              qint64 type;
              in >> type;
              switch (type) {
              case 1:{
                  QString str;
                  in >> str;
                  if (!in.commitTransaction())
                      return;
                  sendToClient(clientSocket, str);
                  break;
              }
              case 2:{
                  QString str;
                  in >> str;
                  if (!in.commitTransaction())
                      return;
                  sendMag(clientSocket, str);
                  break;
              }
              case 3:{
                  qint64 c_codice;
                  double p_prezzoCosto;
                  double p_prezzoPubblico;
                  in >> c_codice >> p_prezzoCosto >> p_prezzoPubblico;
                  if (!in.commitTransaction())
                      return;
                  doSomethingWithData(c_codice ,p_prezzoCosto ,p_prezzoPubblico);
                  break;
              }
              }
          }
      }
      

      For an explanation on how transactions work see the description of ChatClient::onReadyRead of [this example](https://wiki.qt.io/WIP-How_to_create_a_simple_chat_applicatio

      Don't work I have to finds some example 🧐

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #9

      @Nio74 said in how to send different data:Int ,double, string, to server:

      Don't work

      I'm pretty confident the code above should work.
      What doesn't work?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      J.HilkJ 1 Reply Last reply
      0
      • VRoninV VRonin

        @Nio74 said in how to send different data:Int ,double, string, to server:

        Don't work

        I'm pretty confident the code above should work.
        What doesn't work?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #10

        @VRonin said in how to send different data:Int ,double, string, to server:

        I'm pretty confident the code above should work.

        I'm pretty sure your example in readClient is missing the sizeread of the data chunk before the type read

        That would cause the function not to work, but is something the OP should be able to fix himself.


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        VRoninV 1 Reply Last reply
        3
        • J.HilkJ J.Hilk

          @VRonin said in how to send different data:Int ,double, string, to server:

          I'm pretty confident the code above should work.

          I'm pretty sure your example in readClient is missing the sizeread of the data chunk before the type read

          That would cause the function not to work, but is something the OP should be able to fix himself.

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #11

          @J.Hilk said in how to send different data:Int ,double, string, to server:

          'm pretty sure your example in readClient is missing the sizeread of the data chunk before the type read

          Excellent spot! I'm an idiot.

          Replace out << qint64(0) << type << codice << pCosto << pPubblico;
          with out << type << codice << pCosto << pPubblico;

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          Nio74N 1 Reply Last reply
          3
          • VRoninV VRonin

            @J.Hilk said in how to send different data:Int ,double, string, to server:

            'm pretty sure your example in readClient is missing the sizeread of the data chunk before the type read

            Excellent spot! I'm an idiot.

            Replace out << qint64(0) << type << codice << pCosto << pPubblico;
            with out << type << codice << pCosto << pPubblico;

            Nio74N Offline
            Nio74N Offline
            Nio74
            wrote on last edited by Nio74
            #12

            so it works but the data is incorrect

            0_1553681441874_Cattura4.JPG

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #13

              can you show us the acutal code you are using, what are the inputs and the outputs?

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • Nio74N Offline
                Nio74N Offline
                Nio74
                wrote on last edited by
                #14

                Qml:

                CustomButton {
                                id: btnSalva
                                x: 0
                                color: qsTr("#ffffff")
                                text: "SALVA"
                                anchors.top: parent.top
                                anchors.topMargin: 50
                                anchors.right: parent.right
                                anchors.rightMargin: 50
                                onClicked: {
                
                                   backend.connectClicked()
                                   backend.sendUpdateRiparazione(3,parseInt(txtCodice.text),parseFloat(txtPCosto.text),parseFloat(txtPPub.text));
                
                
                                }
                            }
                

                Client c++

                void Backend::sendUpdateRiparazione(qint64 type,qint64 codice, double pCosto, double pPubblico)
                {
                    QDataStream out(client->tcpSocket);
                    out  << type << codice << pCosto << pPubblico;
                }
                

                server:

                void ClientResearch::readClient()
                {
                    Q_ASSERT(dynamic_cast<QTcpSocket*>(sender()));
                    QTcpSocket *clientSocket = static_cast<QTcpSocket*>(sender());
                    QDataStream in(clientSocket);
                    for (;;) {
                        in.startTransaction();
                        qint64 type;
                        in >> type;
                        switch (type) {
                        case 1:{
                            QString str;
                            in >> str;
                            if (!in.commitTransaction())
                                return;
                            sendToClient(clientSocket, str);
                            break;
                        }
                        case 2:{
                            QString str;
                            in >> str;
                            if (!in.commitTransaction())
                                return;
                            sendMag(clientSocket, str);
                            break;
                        }
                        case 3:{
                            qint64 c_codice;
                            double p_prezzoCosto;
                            double p_prezzoPubblico;
                            in >> c_codice >> p_prezzoCosto >> p_prezzoPubblico;
                            if (!in.commitTransaction())
                                return;
                           // doSomethingWithData(c_codice ,p_prezzoCosto ,p_prezzoPubblico);
                            break;
                        }
                        }
                    }
                }
                
                1 Reply Last reply
                0
                • Nio74N Offline
                  Nio74N Offline
                  Nio74
                  wrote on last edited by Nio74
                  #15

                  I have Uplad to Git if anyone wants to look
                  GIT

                  1 Reply Last reply
                  0
                  • Nio74N Offline
                    Nio74N Offline
                    Nio74
                    wrote on last edited by
                    #16

                    So it works but I think there's always something wrong:

                    Server

                    void ClientResearch::readClient()
                    {
                        Q_ASSERT(dynamic_cast<QTcpSocket*>(sender()));
                        QTcpSocket *clientSocket = static_cast<QTcpSocket*>(sender());
                        QDataStream in(clientSocket);
                        while (clientSocket->bytesAvailable())
                    
                       {
                    
                            in.startTransaction();
                            qint64 type;
                            in >> type;
                            switch (type) {
                            case 1:{
                                qDebug()<< "caso 1";
                                qint32 code;
                                in >> code;
                                if (!in.commitTransaction())
                                    return;
                                sendToClient(clientSocket, code);
                                break;
                            }
                            case 2:{
                                QString str;
                                in >> str;
                                if (!in.commitTransaction())
                                    return;
                                sendMag(clientSocket, str);
                                break;
                            }
                            case 3:{
                                qint32 c_codice;
                                double p_prezzoCosto;
                                double p_prezzoPubblico;
                                in >> c_codice >> p_prezzoCosto >> p_prezzoPubblico;
                                if (!in.commitTransaction())
                                    return;
                               updateRiparazione(c_codice ,p_prezzoCosto ,p_prezzoPubblico);
                                break;
                            }
                            }
                        }
                    }
                    

                    Client:

                    void Backend::sendUpdateRiparazione(qint64 type,qint32 codice, double pCosto, double pPubblico)
                    {
                    QDataStream out(client->tcpSocket);
                    out << type << codice << pCosto << pPubblico;
                    }

                    if you see here the data is not ok:

                    0_1553841680904_Type1.JPG

                    While here only Type is correct to put the code is still wrong:

                    0_1553841736455_TYpeOkCodeno.JPG

                    While here it is all correct:

                    0_1553841777781_typecoedeOk.JPG

                    jsulmJ 1 Reply Last reply
                    0
                    • Nio74N Nio74

                      So it works but I think there's always something wrong:

                      Server

                      void ClientResearch::readClient()
                      {
                          Q_ASSERT(dynamic_cast<QTcpSocket*>(sender()));
                          QTcpSocket *clientSocket = static_cast<QTcpSocket*>(sender());
                          QDataStream in(clientSocket);
                          while (clientSocket->bytesAvailable())
                      
                         {
                      
                              in.startTransaction();
                              qint64 type;
                              in >> type;
                              switch (type) {
                              case 1:{
                                  qDebug()<< "caso 1";
                                  qint32 code;
                                  in >> code;
                                  if (!in.commitTransaction())
                                      return;
                                  sendToClient(clientSocket, code);
                                  break;
                              }
                              case 2:{
                                  QString str;
                                  in >> str;
                                  if (!in.commitTransaction())
                                      return;
                                  sendMag(clientSocket, str);
                                  break;
                              }
                              case 3:{
                                  qint32 c_codice;
                                  double p_prezzoCosto;
                                  double p_prezzoPubblico;
                                  in >> c_codice >> p_prezzoCosto >> p_prezzoPubblico;
                                  if (!in.commitTransaction())
                                      return;
                                 updateRiparazione(c_codice ,p_prezzoCosto ,p_prezzoPubblico);
                                  break;
                              }
                              }
                          }
                      }
                      

                      Client:

                      void Backend::sendUpdateRiparazione(qint64 type,qint32 codice, double pCosto, double pPubblico)
                      {
                      QDataStream out(client->tcpSocket);
                      out << type << codice << pCosto << pPubblico;
                      }

                      if you see here the data is not ok:

                      0_1553841680904_Type1.JPG

                      While here only Type is correct to put the code is still wrong:

                      0_1553841736455_TYpeOkCodeno.JPG

                      While here it is all correct:

                      0_1553841777781_typecoedeOk.JPG

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #17

                      @Nio74 You need to step over line 47 so it is executed and data is read into code variable. In your screen shot the debugger is waiting at line 47, that means it was not yet executed.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • Nio74N Offline
                        Nio74N Offline
                        Nio74
                        wrote on last edited by
                        #18

                        Tanks to all you are very kind I can put solved

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved