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. Console application & QTcpSocket
Forum Updated to NodeBB v4.3 + New Features

Console application & QTcpSocket

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 5 Posters 3.4k 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.
  • SPlattenS SPlatten

    @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;
    
    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by
    #21

    @SPlatten
    But you are sending a QJsonDocument::toJson() which returns a QByteArray and receiving a QString? So....

    1 Reply Last reply
    1
    • SPlattenS SPlatten

      @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;
      
      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #22

      @SPlatten as @JonB has already written QJsonDocument::toJson() returns a QByteArray not as QString.

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

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      SPlattenS 2 Replies Last reply
      3
      • KroMignonK KroMignon

        @SPlatten as @JonB has already written QJsonDocument::toJson() returns a QByteArray not as QString.

        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);
        }
        
        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by SPlatten
        #23
        This post is deleted!
        1 Reply Last reply
        0
        • KroMignonK KroMignon

          @SPlatten as @JonB has already written QJsonDocument::toJson() returns a QByteArray not as QString.

          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);
          }
          
          SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #24

          @KroMignon , thank you, now its working!

          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