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, execution halts why and what does it mean?
Forum Updated to NodeBB v4.3 + New Features

QTcpSocket, execution halts why and what does it mean?

Scheduled Pinned Locked Moved Unsolved General and Desktop
23 Posts 5 Posters 2.2k 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.
  • KroMignonK KroMignon

    @SPlatten said in QTcpSocket, execution halts why and what does it mean?:

    If this isn't the correct way to do it, then why are there so many posts out there illustrating it?

    Hmm, I have never found a working example using QTcpSocket which is looking the event loop.
    Can you share some links?

    SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #5

    @KroMignon , do you mean isn't ?

    Links:

    https://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected
    https://www.bogotobogo.com/Qt/Qt5_QTcpSocket.php

    There are many more.

    Kind Regards,
    Sy

    jsulmJ Pl45m4P 2 Replies Last reply
    0
    • SPlattenS SPlatten

      @KroMignon , do you mean isn't ?

      Links:

      https://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected
      https://www.bogotobogo.com/Qt/Qt5_QTcpSocket.php

      There are many more.

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

      @SPlatten I don't see any event loop blocking loops in the links you posted. Your "while( mblnRun == true )" blocks the event loop. If you want to use Qt properly don't block the event loop - one of the most basic rules when using event driven frameworks.

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

      SPlattenS 1 Reply Last reply
      1
      • jsulmJ jsulm

        @SPlatten I don't see any event loop blocking loops in the links you posted. Your "while( mblnRun == true )" blocks the event loop. If you want to use Qt properly don't block the event loop - one of the most basic rules when using event driven frameworks.

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #7

        @jsulm , thank you, can you please help me with an example?

        Kind Regards,
        Sy

        KroMignonK 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @jsulm , thank you, can you please help me with an example?

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #8

          @SPlatten said in QTcpSocket, execution halts why and what does it mean?:

          can you please help me with an example?

          Yes, but an example for doing what exactly?

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          SPlattenS 1 Reply Last reply
          0
          • KroMignonK KroMignon

            @SPlatten said in QTcpSocket, execution halts why and what does it mean?:

            can you please help me with an example?

            Yes, but an example for doing what exactly?

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by SPlatten
            #9

            @KroMignon , the examples I've found on QThread do not include how to NOT block the event loop, so an example on what to do in the thread so it doesn't block the event loop.

            I've just added:

                    QCoreApplication::processEvents();
            

            To the thread loops, is this enough?

            Kind Regards,
            Sy

            KroMignonK Pl45m4P 2 Replies Last reply
            0
            • SPlattenS SPlatten

              @KroMignon , do you mean isn't ?

              Links:

              https://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected
              https://www.bogotobogo.com/Qt/Qt5_QTcpSocket.php

              There are many more.

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #10

              @SPlatten

              In general you can say that

              • for(;;){}
              • while(1)
              • while(true) <- this is what you are doing basically

              are pure evil, when used together with Qt GUI Framework. There might be some exceptions (esp. plain C++ code) but it's not safe to use it, when having signals&slots + events in use.

              In your linked example this

              if (socket->waitForConnected(1000))
              

              for example, wont block the event loop forever, if this is what you were thinking.


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              SPlattenS 1 Reply Last reply
              0
              • Pl45m4P Pl45m4

                @SPlatten

                In general you can say that

                • for(;;){}
                • while(1)
                • while(true) <- this is what you are doing basically

                are pure evil, when used together with Qt GUI Framework. There might be some exceptions (esp. plain C++ code) but it's not safe to use it, when having signals&slots + events in use.

                In your linked example this

                if (socket->waitForConnected(1000))
                

                for example, wont block the event loop forever, if this is what you were thinking.

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #11

                @Pl45m4, I wasn't trying to block the event loop.

                Kind Regards,
                Sy

                1 Reply Last reply
                0
                • SPlattenS SPlatten

                  @KroMignon , the examples I've found on QThread do not include how to NOT block the event loop, so an example on what to do in the thread so it doesn't block the event loop.

                  I've just added:

                          QCoreApplication::processEvents();
                  

                  To the thread loops, is this enough?

                  KroMignonK Offline
                  KroMignonK Offline
                  KroMignon
                  wrote on last edited by KroMignon
                  #12

                  @SPlatten said in QTcpSocket, execution halts why and what does it mean?:

                  To the thread loops, is this enough?

                  I am not sure, is clsMsgSender a QThread or a QObject?

                  @SPlatten I will try to share with you how I use QTcpSocket.

                  First, there is no need to create a thread for that, the easiest implementation is (for me):

                  // supposing mSocket is a member of the current class
                  mSocket = new QTcpSocket();
                  
                  // remove TCP delay
                  mSocket->setSocketOption(QAbstractSocket::LowDelayOption, 1);
                  
                  QObject::connect(mSocket, &QAbstractSocket::connected,
                                   [this]()
                                   {
                                      qDebug() << "Connected with" << mSocket->peerPort();
                                      // Do stuff here                  
                                   });
                  QObject::connect(mSocket, &QAbstractSocket::disconnected,
                                   [this]()
                                   {
                                      qDebug() << "Disconnected from" << mSocket->peerPort();
                                      mSocket->delateLater();
                                      mSocket = nullptr;
                                   });
                  QObject::connect(mSocket, &QIODevice::bytesWritten,
                                   [this](qint64 bytes)
                                   {
                                      qDebug() << bytes << "send to " << mSocket->peerPort();
                                      // do stuff (add next data?)
                                   });
                                   
                  mSocket->connectToHost(remoteIp, tcpPort);
                  QObject::connect(mSocket, &QIODevice::readyRead, [this](){
                      auto buffer = mSocket->readAll();
                      qDebug() << "receiving" << buffer.size() << "bytes from" << mSocket->peerPort();
                      // do stuff with data
                  });
                  

                  It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                  1 Reply Last reply
                  1
                  • SPlattenS SPlatten

                    @KroMignon , the examples I've found on QThread do not include how to NOT block the event loop, so an example on what to do in the thread so it doesn't block the event loop.

                    I've just added:

                            QCoreApplication::processEvents();
                    

                    To the thread loops, is this enough?

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by Pl45m4
                    #13

                    @SPlatten said in QTcpSocket, execution halts why and what does it mean?:

                    the examples I've found on QThread do not include how to NOT block the event loop

                    Here, this wont block:

                    • https://www.bogotobogo.com/Qt/Qt5_QTcpSocket.php (your 2nd linked example)

                    This guy (https://stackoverflow.com/questions/37167927/proper-way-to-run-managable-background-thread-with-qthread) has made a similar mistake.
                    You want to connect once and then let the event loop and your signals do the rest.


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    1 Reply Last reply
                    1
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

                      Funny - this discussion was already done more than 3 months ago already: https://forum.qt.io/topic/120700/receiving-data-from-qtcpsocket/30

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      SPlattenS 1 Reply Last reply
                      2
                      • Christian EhrlicherC Christian Ehrlicher

                        Funny - this discussion was already done more than 3 months ago already: https://forum.qt.io/topic/120700/receiving-data-from-qtcpsocket/30

                        SPlattenS Offline
                        SPlattenS Offline
                        SPlatten
                        wrote on last edited by
                        #15

                        @Christian-Ehrlicher , is it any surprise I'm confused, in that thread you pointed me to the Fortune client and server examples, which I have been following then there is conflicting advice that comes back form this forum when I implement a solution that I'm having problems with.

                        Add to this the what appears to be flaky Qt Creator with updates that seem to send me backwards. I feel I'm loosing the plot.

                        Kind Regards,
                        Sy

                        1 Reply Last reply
                        0
                        • Christian EhrlicherC Offline
                          Christian EhrlicherC Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on last edited by
                          #16

                          The old thread told you to use signals and slots and not the waitFor functions and now we're at the exactly same place.

                          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                          Visit the Qt Academy at https://academy.qt.io/catalog

                          SPlattenS 2 Replies Last reply
                          0
                          • Christian EhrlicherC Christian Ehrlicher

                            The old thread told you to use signals and slots and not the waitFor functions and now we're at the exactly same place.

                            SPlattenS Offline
                            SPlattenS Offline
                            SPlatten
                            wrote on last edited by SPlatten
                            #17

                            @Christian-Ehrlicher , well obviously I have mental memory issues.

                            Kind Regards,
                            Sy

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Christian Ehrlicher

                              The old thread told you to use signals and slots and not the waitFor functions and now we're at the exactly same place.

                              SPlattenS Offline
                              SPlattenS Offline
                              SPlatten
                              wrote on last edited by
                              #18

                              @Christian-Ehrlicher , the examples that ship with Qt 5.15.2 include:

                              blockingfortuneclient
                              threadedfortuneserver

                              On looking at both, these are the sources I used to base my work on. My server is slightly different in that the clients are intended to stay connected after establishing a connection.

                              I will compare the client source with my own.

                              Kind Regards,
                              Sy

                              1 Reply Last reply
                              0
                              • Christian EhrlicherC Offline
                                Christian EhrlicherC Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on last edited by
                                #19

                                From my pov you don't need threads at all but this is not what you want to hear for whatever reason - you rather work on simple stuff like a QTcpSocket for months.

                                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                Visit the Qt Academy at https://academy.qt.io/catalog

                                SPlattenS 1 Reply Last reply
                                0
                                • Christian EhrlicherC Christian Ehrlicher

                                  From my pov you don't need threads at all but this is not what you want to hear for whatever reason - you rather work on simple stuff like a QTcpSocket for months.

                                  SPlattenS Offline
                                  SPlattenS Offline
                                  SPlatten
                                  wrote on last edited by
                                  #20

                                  @Christian-Ehrlicher , one day I will be ready to share what I'm working on, which isn't now.

                                  Kind Regards,
                                  Sy

                                  KroMignonK 1 Reply Last reply
                                  0
                                  • SPlattenS SPlatten

                                    @Christian-Ehrlicher , one day I will be ready to share what I'm working on, which isn't now.

                                    KroMignonK Offline
                                    KroMignonK Offline
                                    KroMignon
                                    wrote on last edited by
                                    #21

                                    @SPlatten said in QTcpSocket, execution halts why and what does it mean?:

                                    one day I will be ready to share what I'm working on, which isn't now.

                                    This sentence sounds strange to me.

                                    I think you misunderstanding some comments/replies you received here.
                                    My personal goal is to help other developers to solve problem they have will creating software with Qt, like I received help from other to solve/understand errors in my software.

                                    On the other side, this also helps me to get a better overview of Qt Framework and continue to learn.

                                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                                    SPlattenS Pl45m4P 2 Replies Last reply
                                    1
                                    • KroMignonK KroMignon

                                      @SPlatten said in QTcpSocket, execution halts why and what does it mean?:

                                      one day I will be ready to share what I'm working on, which isn't now.

                                      This sentence sounds strange to me.

                                      I think you misunderstanding some comments/replies you received here.
                                      My personal goal is to help other developers to solve problem they have will creating software with Qt, like I received help from other to solve/understand errors in my software.

                                      On the other side, this also helps me to get a better overview of Qt Framework and continue to learn.

                                      SPlattenS Offline
                                      SPlattenS Offline
                                      SPlatten
                                      wrote on last edited by
                                      #22

                                      @KroMignon , sorry, some of the comments, not yours I find a little offensive.

                                      Kind Regards,
                                      Sy

                                      1 Reply Last reply
                                      0
                                      • KroMignonK KroMignon

                                        @SPlatten said in QTcpSocket, execution halts why and what does it mean?:

                                        one day I will be ready to share what I'm working on, which isn't now.

                                        This sentence sounds strange to me.

                                        I think you misunderstanding some comments/replies you received here.
                                        My personal goal is to help other developers to solve problem they have will creating software with Qt, like I received help from other to solve/understand errors in my software.

                                        On the other side, this also helps me to get a better overview of Qt Framework and continue to learn.

                                        Pl45m4P Offline
                                        Pl45m4P Offline
                                        Pl45m4
                                        wrote on last edited by
                                        #23

                                        @KroMignon said in QTcpSocket, execution halts why and what does it mean?:

                                        My personal goal is to help other developers to solve problem they have will creating software with Qt, like I received help from other to solve/understand errors in my software.
                                        On the other side, this also helps me to get a better overview of Qt Framework and continue to learn.

                                        This is exactly why I'm here :D


                                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                                        ~E. W. Dijkstra

                                        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