Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved QTcpSocket:read null data from socket

    General and Desktop
    3
    10
    284
    Loading More Posts
    • 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.
    • LengJianHanShuang
      LengJianHanShuang last edited by

      I'm using vm linux to develop qtcpsocket program, I can connect to the server device, and the server device continue send data, the wireshark can receive all the data correctly, and my qt program can receive the readyRead signal, but I can't read any thing from my slot function, the data I got always null. Some one please help me

      following is my readyRead slot function's code

      QByteArray cCanInfoSerials;
      QDataStream socketStream(m_pcSocket);
      socketStream.setVersion(QDataStream::Qt_5_13);
      
      // read to decide if the current board to be closed
      // and set display's backlight level
      // Data from Mainboard will be triggerred by DMSInfo module
      
      // start an infinite loop
      for (;;) {
          // we start a transaction so we can revert to the previous state in case we try to read more data than is available on the socket
          socketStream.startTransaction();
          // we try to read the Can Data
          socketStream >> cCanInfoSerials;
          printf("Data Serail:");
          for(int i=0;i<cCanInfoSerials.size();i++) {
              printf(" %02x ", cCanInfoSerials.at(i));
          }
          printf("\n");
          if (socketStream.commitTransaction()) {
              // we successfully read some data
              // we now need to make sure it's in fact a valid Can Data
              uint DataSize = cCanInfoSerials.size();
              qDebug() << "CanDataSize:"<<DataSize;
               while(DataSize > sizeof(EthernetCanData)) {
                   // read one DMSInfo and update DMSInfo Module
                   EthernetCanData sCanInfoData;
                   sCanInfoData.FF = reinterpret_cast<EthernetCanData*>(cCanInfoSerials.data())->FF;
                   sCanInfoData.RTR = reinterpret_cast<EthernetCanData*>(cCanInfoSerials.data())->RTR;
                   sCanInfoData.CanDataLength = reinterpret_cast<EthernetCanData*>(cCanInfoSerials.data())->CanDataLength;
      
                   memcpy(sCanInfoData.DetailData, &reinterpret_cast<EthernetCanData*>(cCanInfoSerials.data())->DetailData,sizeof(sCanInfoData.DetailData));
      
                   if(nullptr != m_pcCanDataManager) {
                      m_pcCanDataManager->CanDataReceived(sCanInfoData);
                   }
                   // update size
                   DataSize -= sizeof(EthernetCanData);
               }
               // loop and try to read more DMSInfo if they are available
           }
           else {
               // the read failed, the socket goes automatically back to the state it was in before the transaction started
               // we just exit the loop and wait for more data to become available
               qDebug() << "fail to commitTransaction";
               break;
           }
       }
      

      Follow your dreams

      LengJianHanShuang 1 Reply Last reply Reply Quote 0
      • LengJianHanShuang
        LengJianHanShuang @LengJianHanShuang last edited by

        @lengjianhanshuang
        I have do other test, and I found Qtcpsocket::size() returns 0, but Qtcpsocket::read() can actually read expected data, so in QtcpSocket, how should I know the real data size of socket?

        Follow your dreams

        aha_1980 1 Reply Last reply Reply Quote 0
        • Kent-Dorfman
          Kent-Dorfman last edited by

          why is your read slot an infinite loop? readyRead is a signal that tells when data is available on a channel. The slot should read "available" data and return until readyRead signal is fired again.

          LengJianHanShuang 1 Reply Last reply Reply Quote 2
          • aha_1980
            aha_1980 Lifetime Qt Champion @LengJianHanShuang last edited by

            @lengjianhanshuang and to add to @Kent-Dorfman: you are looking for bytesAvailable.

            Regards

            Qt has to stay free or it will die.

            LengJianHanShuang 1 Reply Last reply Reply Quote 0
            • LengJianHanShuang
              LengJianHanShuang @Kent-Dorfman last edited by

              @kent-dorfman
              Thanks for you replay, because every time I only need a small packet of the data to process, I don't know how many packets in the data buffer, and wether during my reading the buffer continue receiving new data, so I read them in the loop, until there is not enough data for me to read

              Follow your dreams

              1 Reply Last reply Reply Quote 0
              • LengJianHanShuang
                LengJianHanShuang @aha_1980 last edited by

                @aha_1980
                thanks for your information.
                If I want to use committransaction, How should I use it, in my code, the committransaction always return false..., do you have any idea how it always leads to false?

                Follow your dreams

                aha_1980 1 Reply Last reply Reply Quote 0
                • aha_1980
                  aha_1980 Lifetime Qt Champion @LengJianHanShuang last edited by

                  @lengjianhanshuang said in QTcpSocket:read null data from socket:

                  If I want to use committransaction

                  Why do you want to use it?

                  Qt has to stay free or it will die.

                  LengJianHanShuang 1 Reply Last reply Reply Quote 0
                  • LengJianHanShuang
                    LengJianHanShuang @aha_1980 last edited by

                    @aha_1980
                    from the doc, it seems the remaind data(not enough data bytes for processing) from previous package can be automatically contigous to data from next package, develop will do less process, so I 'd like to have a try...

                    Follow your dreams

                    aha_1980 1 Reply Last reply Reply Quote 0
                    • aha_1980
                      aha_1980 Lifetime Qt Champion @LengJianHanShuang last edited by

                      @lengjianhanshuang Indeed it can help you with that.

                      But you are reading QByteArray out of the data stream, how should the stream know where a QByteArray begins or ends?

                      That would only work if you actually send the data with QDataStream on the other side.

                      Qt has to stay free or it will die.

                      LengJianHanShuang 1 Reply Last reply Reply Quote 4
                      • LengJianHanShuang
                        LengJianHanShuang @aha_1980 last edited by

                        @aha_1980
                        you are right, actually I don't know how to use QByteArray and QDataStream together correctly, the QDataStream has starttransaction and committransaction, my understanding is that, when QDataStream is used to manage the socket data, first use the start** to record the start point of Data stream in QDatastream, then try to "read some data" from QDataStream, at this point, we can use committransaction to decide whether it succeeded in "reading some data"(may be the size of "some data" is different from byteavailable), if it suceed, the "some data" in QDataStream will be clear, if it is not, the "some data" can be still in QDataStream using rolltransaction.
                        Is my understanding right?

                        Follow your dreams

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post