Console application & QTcpSocket
-
@Bonnie , @jsulm , @KroMignon , this is my send function:
void clsModHelper::sendJSON(QJsonObject& objJSON) { if ( isOpen() != true ) { return; } //Associate this TCP socket with the output data stream QByteArray arybytMsg; QDataStream dsOut(&arybytMsg, QIODevice::WriteOnly); //dsOut.setDevice(this); dsOut.setVersion(clsJSON::mscintQtVersion); //Send message to data stream dsOut << QJsonDocument(objJSON).toJson(QJsonDocument::Compact); //Write message write(arybytMsg); }
And receiving:
void clsModFileIO::onDataIn() { QDataStream dsIn; dsIn.setDevice(this); dsIn.setVersion(clsJSON::mscintQtVersion); QString strRx; dsIn.startTransaction(); dsIn >> strRx; if ( strRx.isEmpty() != true ) { qdbg() << "onDataIn: " << strRx; } if ( dsIn.commitTransaction() == false ) { return; } }
clsModFileIO is derived from clsModHelper. clsJSON::mscintQtVersion is defined as:
const int clsJSON::mscintQtVersion = QDataStream::Qt_5_14;
-
@Bonnie , @jsulm , @KroMignon , this is my send function:
void clsModHelper::sendJSON(QJsonObject& objJSON) { if ( isOpen() != true ) { return; } //Associate this TCP socket with the output data stream QByteArray arybytMsg; QDataStream dsOut(&arybytMsg, QIODevice::WriteOnly); //dsOut.setDevice(this); dsOut.setVersion(clsJSON::mscintQtVersion); //Send message to data stream dsOut << QJsonDocument(objJSON).toJson(QJsonDocument::Compact); //Write message write(arybytMsg); }
And receiving:
void clsModFileIO::onDataIn() { QDataStream dsIn; dsIn.setDevice(this); dsIn.setVersion(clsJSON::mscintQtVersion); QString strRx; dsIn.startTransaction(); dsIn >> strRx; if ( strRx.isEmpty() != true ) { qdbg() << "onDataIn: " << strRx; } if ( dsIn.commitTransaction() == false ) { return; } }
clsModFileIO is derived from clsModHelper. clsJSON::mscintQtVersion is defined as:
const int clsJSON::mscintQtVersion = QDataStream::Qt_5_14;
@SPlatten as @JonB has already written
QJsonDocument::toJson()
returns aQByteArray
not asQString
.You have to send and read the same thing if you want it to work!
void clsModFileIO::onDataIn() { QDataStream dsIn; dsIn.setDevice(this); dsIn.setVersion(clsJSON::mscintQtVersion); QByteArray jsonArray; dsIn.startTransaction(); dsIn >> jsonArray; QJsonDocument doc; if ( jsonArray.isEmpty() != true ) { qdbg() << "onDataIn: " << jsonArray; doc = QJsonDocument::fromJson(jsonArray); } if ( dsIn.commitTransaction() == false ) { return; } }
[EDIT] This only works if you also change the send:
void clsModHelper::sendJSON(QJsonObject& objJSON) { if ( isOpen() != true ) { return; } //Associate this TCP socket with the output data stream QDataStream dsOut; dsOut.setDevice(this); dsOut.setVersion(clsJSON::mscintQtVersion); //Send message to data stream dsOut << QJsonDocument(objJSON).toJson(QJsonDocument::Compact); }
-
@SPlatten as @JonB has already written
QJsonDocument::toJson()
returns aQByteArray
not asQString
.You have to send and read the same thing if you want it to work!
void clsModFileIO::onDataIn() { QDataStream dsIn; dsIn.setDevice(this); dsIn.setVersion(clsJSON::mscintQtVersion); QByteArray jsonArray; dsIn.startTransaction(); dsIn >> jsonArray; QJsonDocument doc; if ( jsonArray.isEmpty() != true ) { qdbg() << "onDataIn: " << jsonArray; doc = QJsonDocument::fromJson(jsonArray); } if ( dsIn.commitTransaction() == false ) { return; } }
[EDIT] This only works if you also change the send:
void clsModHelper::sendJSON(QJsonObject& objJSON) { if ( isOpen() != true ) { return; } //Associate this TCP socket with the output data stream QDataStream dsOut; dsOut.setDevice(this); dsOut.setVersion(clsJSON::mscintQtVersion); //Send message to data stream dsOut << QJsonDocument(objJSON).toJson(QJsonDocument::Compact); }
-
@SPlatten as @JonB has already written
QJsonDocument::toJson()
returns aQByteArray
not asQString
.You have to send and read the same thing if you want it to work!
void clsModFileIO::onDataIn() { QDataStream dsIn; dsIn.setDevice(this); dsIn.setVersion(clsJSON::mscintQtVersion); QByteArray jsonArray; dsIn.startTransaction(); dsIn >> jsonArray; QJsonDocument doc; if ( jsonArray.isEmpty() != true ) { qdbg() << "onDataIn: " << jsonArray; doc = QJsonDocument::fromJson(jsonArray); } if ( dsIn.commitTransaction() == false ) { return; } }
[EDIT] This only works if you also change the send:
void clsModHelper::sendJSON(QJsonObject& objJSON) { if ( isOpen() != true ) { return; } //Associate this TCP socket with the output data stream QDataStream dsOut; dsOut.setDevice(this); dsOut.setVersion(clsJSON::mscintQtVersion); //Send message to data stream dsOut << QJsonDocument(objJSON).toJson(QJsonDocument::Compact); }
@KroMignon , thank you, now its working!