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



  • 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


  • Moderators

    @Nio74
    on qml side, explicitly convert your text to a number before passing it to your cpp slot!



  • @J.Hilk

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


  • Moderators

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


  • Qt Champions 2018

    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



  • 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


  • Qt Champions 2018

    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



  • @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 🧐


  • Qt Champions 2018

    @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?


  • Moderators

    @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.


  • Qt Champions 2018

    @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;



  • so it works but the data is incorrect

    0_1553681441874_Cattura4.JPG


  • Qt Champions 2018

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



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


  • I have Uplad to Git if anyone wants to look
    GIT



  • 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


  • Qt Champions 2018

    @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.



  • Tanks to all you are very kind I can put solved


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.