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
Forum Update on Monday, May 27th 2025

QTcpSocket:read null data from socket

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 899 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.
  • L Offline
    L Offline
    LengJianHanShuang
    wrote on 14 Aug 2019, 01:28 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

    L 1 Reply Last reply 14 Aug 2019, 02:22
    0
    • L LengJianHanShuang
      14 Aug 2019, 01:28

      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;
           }
       }
      
      L Offline
      L Offline
      LengJianHanShuang
      wrote on 14 Aug 2019, 02:22 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

      A 1 Reply Last reply 14 Aug 2019, 06:29
      0
      • K Offline
        K Offline
        Kent-Dorfman
        wrote on 14 Aug 2019, 04:55 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.

        L 1 Reply Last reply 14 Aug 2019, 06:35
        2
        • L LengJianHanShuang
          14 Aug 2019, 02:22

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

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 14 Aug 2019, 06:29 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.

          L 1 Reply Last reply 14 Aug 2019, 06:46
          0
          • K Kent-Dorfman
            14 Aug 2019, 04:55

            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.

            L Offline
            L Offline
            LengJianHanShuang
            wrote on 14 Aug 2019, 06:35 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
            • A aha_1980
              14 Aug 2019, 06:29

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

              Regards

              L Offline
              L Offline
              LengJianHanShuang
              wrote on 14 Aug 2019, 06:46 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

              A 1 Reply Last reply 14 Aug 2019, 06:53
              0
              • L LengJianHanShuang
                14 Aug 2019, 06:46

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

                A Offline
                A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on 14 Aug 2019, 06:53 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.

                L 1 Reply Last reply 14 Aug 2019, 07:15
                0
                • A aha_1980
                  14 Aug 2019, 06:53

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

                  If I want to use committransaction

                  Why do you want to use it?

                  L Offline
                  L Offline
                  LengJianHanShuang
                  wrote on 14 Aug 2019, 07:15 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

                  A 1 Reply Last reply 14 Aug 2019, 07:17
                  0
                  • L LengJianHanShuang
                    14 Aug 2019, 07:15

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

                    A Offline
                    A Offline
                    aha_1980
                    Lifetime Qt Champion
                    wrote on 14 Aug 2019, 07:17 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.

                    L 1 Reply Last reply 15 Aug 2019, 01:19
                    4
                    • A aha_1980
                      14 Aug 2019, 07:17

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

                      L Offline
                      L Offline
                      LengJianHanShuang
                      wrote on 15 Aug 2019, 01:19 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

                      3/10

                      14 Aug 2019, 04:55

                      7 unread
                      • Login

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