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.
  • Nio74N Nio74

    @J.Hilk

    I can't find anything on the qml documentation. Can you give us an example? thank you

    J.HilkJ Online
    J.HilkJ Online
    J.Hilk
    Moderators
    wrote on last edited by
    #4

    @Nio74
    that's because its part of the basic JavaScript libs
    parseInt() and parseFloat() in your case


    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.

    1 Reply Last reply
    1
    • Nio74N Nio74

      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: Switch view
      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,txtCodice.text,txtPCosto.text,txtPPub.text);
       
       
                      }
                  }
      

      this is the metod that send at server:

      
      
      
      Qt Code: Switch view
      void Backend::sendUpdateRiparazione(qint16 type,qint16 codice, double pCosto, double pPubblico)
      {
          //client->connect2host();
          QByteArray arrBlock;
          QDataStream out(&arrBlock, QIODevice::WriteOnly);
          //out.setVersion(QDataStream::Qt_5_10);
          //out <<qint16(0) << type << codice << pCosto << pPubblico ;
          out << qint16(0);
          out << (qint16)type;
          out << (qint16)codice;
          out << (double)pCosto;
          out << (double)pPubblico;
       
         // out << msg ;
       
          out.device()->seek(0);
          out << quint16(arrBlock.size() - sizeof(quint16));
       
          client->tcpSocket->write(arrBlock);
       
       
       
      }
      

      this is Server:

      
      Qt Code: Switch view
      void ClientResearch::readClient()
      {
          QTcpSocket *clientSocket = static_cast<QTcpSocket*>(sender());
          QDataStream in(clientSocket);
       
       
       
          for (;;)
              {
                  if (!m_nNextBlockSize)
                  {
                      if (clientSocket->bytesAvailable() < sizeof(quint16)) { break; }
                      in >> m_nNextBlockSize;
                  }
                  if (clientSocket->bytesAvailable() < m_nNextBlockSize) { break; }
                  QString str;
                  qint16 type;
       
                  emit gotNewMesssage(str);
       
                  m_nNextBlockSize = 0;
       
       
              in >> type ;
       
       
              switch (type) {
       
              case 1:if(type == 1){
                      in >> str;
                      sendToClient(clientSocket, str);
                      qDebug()<< "chiamata uno ";
                      break;
       
                  }
       
               case 2:if(type == 2){
                      in >> str;
                      sendMag(clientSocket, str);
                      break;
       
                  }
              case 3:if(type ==3){
                      qDebug()<< "chiamata tre ";
                      qint64 c_codice;
                      double p_prezzoCosto;
                      double p_prezzoPubblico;
       
                     in >> c_codice >> p_prezzoCosto >> p_prezzoPubblico;
       
                     qDebug() <<c_codice << p_prezzoCosto << p_prezzoPubblico <<"evviva";
                     updateRiparazione(c_codice,p_prezzoCosto,p_prezzoPubblico);
       
       
                 }
                  break;
       
              }
       
       
      }
      }
      

      the data recived are these,the only variable correct is "type" codice should be 82253, p_prezzoCosto 3873,p_prezzoPubblico 9681
      0_1553581338163_Cattura.JPG

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

      Just a very trivial size mismatch between sender and receiver. The sender sends 16 bits, the receiver expects 64

      out << (qint16)codice;

      qint64 c_codice;
      in >> c_codice

      "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
      1
      • Nio74N Offline
        Nio74N Offline
        Nio74
        wrote on last edited by
        #6

        I have tryed so:

        Qml Client:

         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));
        
        
                        }
                    }
        

        c++ Client:

        void Backend::sendUpdateRiparazione(qint64 type,qint64 codice, double pCosto, double pPubblico)
        {
            //client->connect2host();
            QByteArray arrBlock;
            QDataStream out(&arrBlock, QIODevice::WriteOnly);
            //out.setVersion(QDataStream::Qt_5_10);
            out << qint64(0) << type << codice << pCosto << pPubblico ;
        
            out.device()->seek(0);
            out << quint64(arrBlock.size() - sizeof(quint64));
        
            client->tcpSocket->write(arrBlock);
        
        
        
        }
        

        Server :

        void ClientResearch::readClient()
        {
            QTcpSocket *clientSocket = static_cast<QTcpSocket*>(sender());
            QDataStream in(clientSocket);
        
        
            for (;;)
                {
                    if (!m_nNextBlockSize)
                    {
                        if (clientSocket->bytesAvailable() < sizeof(quint64)) { break; }
                        in >> m_nNextBlockSize;
                    }
                    if (clientSocket->bytesAvailable() < m_nNextBlockSize) { break; }
                    QString str;
                    qint64 type;
                    
                //    emit gotNewMesssage(str);
        
                    m_nNextBlockSize = 0;
        
        
                in >> type ;
        
        
                switch (type) {
        
                case 1:if(type == 1){
                        in >> str;
                        sendToClient(clientSocket, str);
                        qDebug()<< "chiamata uno ";
                        break;
        
                    }
        
                 case 2:if(type == 2){
                        in >> str;
                        sendMag(clientSocket, str);
                        break;
        
                    }
                 case 3:if(type ==3){
                        qDebug()<< "chiamata tre ";
                        qint64 c_codice;
                        double p_prezzoCosto;
                        double p_prezzoPubblico;
                        in >> c_codice >> p_prezzoCosto >> p_prezzoPubblico;
        
                   }
                    break;
        
                }
        
        
        }
        }
        

        but not qork correctly:
        0_1553597251485_Cattura3.JPG

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

          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

          "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
          2
          • 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 Online
                J.HilkJ Online
                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