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. QAbstractSocket not sending
Qt 6.11 is out! See what's new in the release blog

QAbstractSocket not sending

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 6 Posters 2.5k Views 3 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #4

    What is bufOut ?
    What does it contain ?

    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
    0
    • mzimmersM Offline
      mzimmersM Offline
      mzimmers
      wrote on last edited by
      #5

      bufOut is a char array. It holds records from an Intel Extended Hex file. All bytes are either the colon (":") or an ASCII hex character ('0', '1', etc). There are newlines ('\n') in there as well.

      kshegunovK 1 Reply Last reply
      0
      • mzimmersM mzimmers

        bufOut is a char array. It holds records from an Intel Extended Hex file. All bytes are either the colon (":") or an ASCII hex character ('0', '1', etc). There are newlines ('\n') in there as well.

        kshegunovK Offline
        kshegunovK Offline
        kshegunov
        Moderators
        wrote on last edited by
        #6

        Have you connected to the error signals of the socket? What kind of socket is it, UDP or TCP?

        Read and abide by the Qt Code of Conduct

        mzimmersM 1 Reply Last reply
        0
        • kshegunovK kshegunov

          Have you connected to the error signals of the socket? What kind of socket is it, UDP or TCP?

          mzimmersM Offline
          mzimmersM Offline
          mzimmers
          wrote on last edited by
          #7

          @kshegunov yes I have...nothing happens there. It's a TCP socket.

          kshegunovK 1 Reply Last reply
          0
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Moderators Qt Champions 2024 Qt Champions 2022 Qt Champions 2017
            wrote on last edited by
            #8

            I suspect socket is not connected. To ensure that it is really connected look for 6 signal defined the socket. One of them should give hint. If it is really connected atleast it should receive the connected signal.

            1. How do you say data is not sent ?
            2. On the other side of the socket, are able to get the newConnection request ?
            3. Have you connected to readyRead at the other side ? Just check if some signal is not connected
            4. Are you doing any of these operation before even starting the event loop ?

            Dheerendra
            @Community Service
            Certified Qt Specialist
            https://www.pthinks.com

            1 Reply Last reply
            0
            • mzimmersM mzimmers

              Hi all -

              Working on a utility that will read a file, and transfer the contents to a socket. The writes aren't working -- they're not showing up in Wireshark, and my slot that catches the bytesWritten() signal isn't executed.

              I'm running from a QWidget (don't know whether that matters); here's some of the code:

                  QVariant one(1);
              
                  m_sock->setSocketOption(QAbstractSocket::LowDelayOption, one);
                  m_sock->connectToHost(ui->lineEditAddr->text(),
                                     (quint16) ui->lineEditPort->text().toInt(),
                                     QIODevice::ReadWrite,
                                     QAbstractSocket::IPv4Protocol);
              ...
                  socketBytesWritten = sock->write((const char *) bufOut, bufLen);
              

              I've tried this with varying bufLens between 15 and ~2000. The write() call returns the same value, but nothing is getting sent.

              What might I be doing wrong here? Thanks.

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

              @mzimmers please check your code.

              You connect to m_sock, but want to write to sock. Could it be that simple?

              Qt has to stay free or it will die.

              1 Reply Last reply
              2
              • mzimmersM mzimmers

                @kshegunov yes I have...nothing happens there. It's a TCP socket.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #10

                What is the socket state just before you write the data? The TCP socket can be open for writing (i.e. isValid() returns true) even if it's in the Connecting state.

                Read and abide by the Qt Code of Conduct

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

                  Hey guys - sorry it took me so long to reply; I took a break from this over the weekend.

                  Aha: sock is an argument passed to a non-member function. It's the same socket as m_sock. (EDIT: all functions are now member functions, and it uses m_sock as does the successful write; still not working here.)

                  ksheg: it's "QAbstractSocket::ConnectedState." I actually test for it twice before sending.

                  dheerendra: the connection is legitimate:0_1548689394691_ws.PNG

                  And again, another part of this program does successfully send on that socket. I can see it in Wireshark. It's just this send that doesn't show up on Wireshark.

                  EDIT: some additional information: the write() that I can see in Wireshark is called from a slot. The actual writing doesn't occur until the slot is exited, and control returns somewhere deep in Qt-land. This reinforces my suspicion that I'm doing something wrong in terms of blocking the event loop or something. I'm really lost as to what to do, though.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #12

                    Did you check for any busy loop in your code ?

                    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
                    0
                    • mzimmersM Offline
                      mzimmersM Offline
                      mzimmers
                      wrote on last edited by mzimmers
                      #13

                      You mean a compute-intensive loop? Nothing like that. When I'm in autosend mode, there's a timer that can be quite brief, but that's not being used now.

                      It's something about my widget -- the write() doesn't complete until control passes out of it. I did a bunch of step-outs in the debugger, and it wasn't until I got to the very end of the stack that the writing actually occurred.

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

                        Found a solution -- calling m_sock->flush() after my write sends the stuff before control returns to the event loop. Not sure I really understand why this is needed, but it works. Thanks to all who looked.

                        J.HilkJ 1 Reply Last reply
                        3
                        • mzimmersM mzimmers

                          Found a solution -- calling m_sock->flush() after my write sends the stuff before control returns to the event loop. Not sure I really understand why this is needed, but it works. Thanks to all who looked.

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

                          @mzimmers

                          actually the docs explains why ;-)

                          http://doc.qt.io/qt-5/qabstractsocket.html#flush

                          Call this function if you need QAbstractSocket to start sending buffered data immediately. The number of bytes successfully written depends on the operating system. In most cases, you do not need to call this function, because QAbstractSocket will start sending data automatically once control goes back to the event loop


                          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.

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

                            @mzimmers

                            actually the docs explains why ;-)

                            http://doc.qt.io/qt-5/qabstractsocket.html#flush

                            Call this function if you need QAbstractSocket to start sending buffered data immediately. The number of bytes successfully written depends on the operating system. In most cases, you do not need to call this function, because QAbstractSocket will start sending data automatically once control goes back to the event loop

                            mzimmersM Offline
                            mzimmersM Offline
                            mzimmers
                            wrote on last edited by
                            #16

                            @J.Hilk yeah, I did see that; I guess I just didn't fully understand its implications. I probably mis-designed this part of the app anyway - the socket I/O is lengthy enough to be in a separate thread.

                            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