Solved how to send different data:Int ,double, string, to server
-
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:
-
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_applicatioDon't work I have to finds some example 🧐
-
@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? -
@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 thesize
read of the data chunk before thetype
readThat would cause the function not to work, but is something the OP should be able to fix himself.
-
@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;
without << type << codice << pCosto << pPubblico;
-
so it works but the data is incorrect
-
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:
While here only Type is correct to put the code is still wrong:
While here it is all correct:
-
@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