PDA

View Full Version : Send Int and double QDatastream trought QTCpSocket



Nio74
24th March 2019, 16:50
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:


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,txt PCosto.text,txtPPub.text);


}
}

this is the metod that send at server:


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:


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_prezzoP ubblico);


}
break;

}


}
}

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

anda_skoa
24th March 2019, 18:10
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,
_

Nio74
25th March 2019, 06:13
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:mad::

13057

client:


void Backend::sendUpdateRiparazione(qint64 type,qint64 codice, qint64 pCosto, qint64 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 ";
// qint16 type;
qint64 c_codice;
qint64 p_prezzoCosto;
qint64 p_prezzoPubblico;

in >> c_codice >> p_prezzoCosto >> p_prezzoPubblico;

// qDebug() <<c_codice << p_prezzoCosto << p_prezzoPubblico <<"evviva";
// updateRiparazione(c_codice,p_prezzoCosto,p_prezzoP ubblico);


}
break;

}


}
}



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.

anda_skoa
25th March 2019, 07:15
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,
_

Nio74
29th March 2019, 11:08
I have solved So:

Client:


void Backend::sendUpdateRiparazione(qint64 type,qint32 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);
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;
}
}
}
}