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. QTcpSocket:read null data from socket
QtWS25 Last Chance

QTcpSocket:read null data from socket

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 884 Views
  • 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.
  • LengJianHanShuangL Offline
    LengJianHanShuangL Offline
    LengJianHanShuang
    wrote on last edited by
    #1

    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

    LengJianHanShuangL 1 Reply Last reply
    0
    • LengJianHanShuangL LengJianHanShuang

      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;
           }
       }
      
      LengJianHanShuangL Offline
      LengJianHanShuangL Offline
      LengJianHanShuang
      wrote on last edited by
      #2

      @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_1980A 1 Reply Last reply
      0
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #3

        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.

        LengJianHanShuangL 1 Reply Last reply
        2
        • LengJianHanShuangL LengJianHanShuang

          @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?

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Regards

          Qt has to stay free or it will die.

          LengJianHanShuangL 1 Reply Last reply
          0
          • Kent-DorfmanK Kent-Dorfman

            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.

            LengJianHanShuangL Offline
            LengJianHanShuangL Offline
            LengJianHanShuang
            wrote on last edited by
            #5

            @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
            0
            • aha_1980A aha_1980

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

              Regards

              LengJianHanShuangL Offline
              LengJianHanShuangL Offline
              LengJianHanShuang
              wrote on last edited by
              #6

              @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_1980A 1 Reply Last reply
              0
              • LengJianHanShuangL LengJianHanShuang

                @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?

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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.

                LengJianHanShuangL 1 Reply Last reply
                0
                • aha_1980A aha_1980

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

                  If I want to use committransaction

                  Why do you want to use it?

                  LengJianHanShuangL Offline
                  LengJianHanShuangL Offline
                  LengJianHanShuang
                  wrote on last edited by
                  #8

                  @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_1980A 1 Reply Last reply
                  0
                  • LengJianHanShuangL LengJianHanShuang

                    @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...

                    aha_1980A Offline
                    aha_1980A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @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.

                    LengJianHanShuangL 1 Reply Last reply
                    4
                    • aha_1980A aha_1980

                      @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.

                      LengJianHanShuangL Offline
                      LengJianHanShuangL Offline
                      LengJianHanShuang
                      wrote on last edited by
                      #10

                      @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
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved