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. not receiving on QAbstractSocket
QtWS25 Last Chance

not receiving on QAbstractSocket

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 940 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    I use a QAbstractSocket for a file transfer to an embedded target. The protocol is simple: I send a block of data, wait for an ack, then re-send, etc. until the file is fully transferred.

    I'm not getting the acks back from the target. I see them in Wireshark (running on the same system as my Qt app). I even temporarily replaced the QAbstractSocket with Winsock 2, and my app gets the ack that way.

    I honestly don't know what code I can post to be helpful, but here's some of it:

    m_socket = new QTcpSocket;
    ...
    m_socket->connectToHost(m_hostAddr, OTA_PORT_NBR, QIODevice::ReadWrite);
    ...   
    bytesReadFromTarget = m_socket->read(buff, OTA_BUFFSIZE);
    

    bytesReadFromTarget always comes back 0. I've also set a slot for the error() signal, and it isn't triggered on this read. The read simply isn't seeing the ack.

    My writes work just fine.

    I've looked through the page on QAbstractSockets, but I don't see any configuration options that look hopeful. Can someone give me an idea of what I'm doing wrong?

    Thanks...

    J.HilkJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What about QDataStream's read transaction mechanism ?

      When are you calling m_socket->read ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      4
      • mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by mzimmers
        #3

        Hi SGaist - I'll look into QDataStream.

        Not sure what you mean by "when," but I'm reading immediately after a write. But it behaves identically if I insert a delay, and/or step through the code with the debugger, so I don't think that matters.

        EDIT: not sure I'm doing it right, but here's my attempt with QDataStream:

        QDataStream in(m_socket);
        QString qs;
        in.startTransaction();
        in >> qs;
        
        if (in.commitTransaction())
        {
        	cout << "in.commitTransaction() returned true." << endl;
        }
        else
        {
        	cout << "in.commitTransaction() returned false." << endl;
        }
        

        commitTransaction() returns false, indicating an error. It's not clear to me how I get additional information about the error.

        jsulmJ JonBJ 2 Replies Last reply
        0
        • mzimmersM mzimmers

          Hi SGaist - I'll look into QDataStream.

          Not sure what you mean by "when," but I'm reading immediately after a write. But it behaves identically if I insert a delay, and/or step through the code with the debugger, so I don't think that matters.

          EDIT: not sure I'm doing it right, but here's my attempt with QDataStream:

          QDataStream in(m_socket);
          QString qs;
          in.startTransaction();
          in >> qs;
          
          if (in.commitTransaction())
          {
          	cout << "in.commitTransaction() returned true." << endl;
          }
          else
          {
          	cout << "in.commitTransaction() returned false." << endl;
          }
          

          commitTransaction() returns false, indicating an error. It's not clear to me how I get additional information about the error.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @mzimmers You should not read immediately or use a timeout. Use http://doc.qt.io/qt-5/qiodevice.html#readyRead signal and read in the slot you connect to it.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          7
          • mzimmersM mzimmers

            Hi SGaist - I'll look into QDataStream.

            Not sure what you mean by "when," but I'm reading immediately after a write. But it behaves identically if I insert a delay, and/or step through the code with the debugger, so I don't think that matters.

            EDIT: not sure I'm doing it right, but here's my attempt with QDataStream:

            QDataStream in(m_socket);
            QString qs;
            in.startTransaction();
            in >> qs;
            
            if (in.commitTransaction())
            {
            	cout << "in.commitTransaction() returned true." << endl;
            }
            else
            {
            	cout << "in.commitTransaction() returned false." << endl;
            }
            

            commitTransaction() returns false, indicating an error. It's not clear to me how I get additional information about the error.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @mzimmers
            I assumed your
            bytesReadFromTarget = m_socket->read(buff, OTA_BUFFSIZE);
            was indeed in your readyRead slot. Like @jsulm says, you must write your code so that it drops into the event loop and allow signals to arrive to call slots where you then do the reading.

            1 Reply Last reply
            4
            • mzimmersM mzimmers

              Hi all -

              I use a QAbstractSocket for a file transfer to an embedded target. The protocol is simple: I send a block of data, wait for an ack, then re-send, etc. until the file is fully transferred.

              I'm not getting the acks back from the target. I see them in Wireshark (running on the same system as my Qt app). I even temporarily replaced the QAbstractSocket with Winsock 2, and my app gets the ack that way.

              I honestly don't know what code I can post to be helpful, but here's some of it:

              m_socket = new QTcpSocket;
              ...
              m_socket->connectToHost(m_hostAddr, OTA_PORT_NBR, QIODevice::ReadWrite);
              ...   
              bytesReadFromTarget = m_socket->read(buff, OTA_BUFFSIZE);
              

              bytesReadFromTarget always comes back 0. I've also set a slot for the error() signal, and it isn't triggered on this read. The read simply isn't seeing the ack.

              My writes work just fine.

              I've looked through the page on QAbstractSockets, but I don't see any configuration options that look hopeful. Can someone give me an idea of what I'm doing wrong?

              Thanks...

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @mzimmers
              to add to the previous posters.

              You have the choice between 2 ways to do this. Synchronus and Asynchronus.

              The recommended way is using the eventloop and reacting to the SIGNALS the QAbstractSocket emits.

              If you insist on using the Snychronus way or you happend to not have a main evetloop, than you can use the calls waitForBytesWritten , waitForReadyRead etc.
              But a quote for those function from the docu:

              Note: This function may fail randomly on Windows. Consider using the event loop and the bytesWritten() signal if your software will run on Windows.

              We all recommend the Signal/Slot communication, and that for good reasons ;-)


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              JonBJ 1 Reply Last reply
              4
              • J.HilkJ J.Hilk

                @mzimmers
                to add to the previous posters.

                You have the choice between 2 ways to do this. Synchronus and Asynchronus.

                The recommended way is using the eventloop and reacting to the SIGNALS the QAbstractSocket emits.

                If you insist on using the Snychronus way or you happend to not have a main evetloop, than you can use the calls waitForBytesWritten , waitForReadyRead etc.
                But a quote for those function from the docu:

                Note: This function may fail randomly on Windows. Consider using the event loop and the bytesWritten() signal if your software will run on Windows.

                We all recommend the Signal/Slot communication, and that for good reasons ;-)

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @J.Hilk
                Absolutely! And since the OP said he has used Winsock 2, he must be Windoze. I think he should avoid the waitFor... approach, and stick to signals/sockets, as (IIRC) we have had other posts from Win users saying they have indeed had problems with the "wait" ones.

                1 Reply Last reply
                1
                • mzimmersM Offline
                  mzimmersM Offline
                  mzimmers
                  wrote on last edited by
                  #8

                  Thanks, everyone. I'm still trying to get the event loop model through my thick skull.

                  Here's the (successful) change:

                  void Ota::transfer()
                  {
                  ...
                              QObject::connect(m_socket, &QTcpSocket::readyRead, this, &Ota::readAck);
                              sendPacket(); // initial send; further sends will come from signals.
                  ...
                  void Ota::readAck()
                  {
                      // read an ack from the ESP32.
                      qint64 bytesReadFromTarget;
                      char buff[OTA_BUFFSIZE];
                  
                      bytesReadFromTarget = m_socket->read(buff, OTA_BUFFSIZE);
                      if (!strncmp(buff, ACK, strlen(ACK)))
                      {
                          // successful ACK receipt; send another section of the file.
                          sendPacket();
                      }
                      else
                      {
                          cerr << "Didn't get ack for TCP socket write; errno is " << errno << endl << flush;
                      }
                  }
                  

                  Works much better now. It's still a bit of a mystery why I need these acks in the first place, though...I thought TCP should handle that for me, but without them, I overrun the buffer on the target. Anyway...

                  Thanks again for all the help.

                  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