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. connectToHost and waitForConnected
Forum Updated to NodeBB v4.3 + New Features

connectToHost and waitForConnected

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 2.1k Views 2 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by SPlatten
    #1

    Confused, I'm calling connectToHost:

    connectToHost(clsModHelper::mscpszHost, muint16XMLMPAMport);
    
    if ( waitForConnected(clsModHelepr::mscintConnectionTimeout) != true ) {
        exit terminateModule();
    }
    

    mscpszHost contains "localhost"
    muint16XMLMPAMport contains 8123
    mscintConnectionTimeout contains 20000;

    The timeout is in milliseconds, so 20 seconds. I'm absolutely sure nothing else is using 8123 on my system, but when using the debugger I can see that the waitForConnected is timing out instantly and not after 20 seconds at all, why ?

    [Edit] Modified code to:

    QElapsedTimer tmr;
    tmr.start();
    connectToHost(clsModHelper::mscpszHost, muint16XMLMPAMport);
    
    if ( waitForConnected(clsModHelper::mscintConnectionTimeout) != true ) {
       cout << tmr.elapsed() << endl;
       emit terminateModule();
    }
    ``
    In the Application Output, 4 is displayed.

    Kind Regards,
    Sy

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

      Did you actually check which error you get? I mean - the OS will not wait 20 seconds until it tells you that e.g. the connection was refused.

      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
      3
      • SPlattenS SPlatten

        Confused, I'm calling connectToHost:

        connectToHost(clsModHelper::mscpszHost, muint16XMLMPAMport);
        
        if ( waitForConnected(clsModHelepr::mscintConnectionTimeout) != true ) {
            exit terminateModule();
        }
        

        mscpszHost contains "localhost"
        muint16XMLMPAMport contains 8123
        mscintConnectionTimeout contains 20000;

        The timeout is in milliseconds, so 20 seconds. I'm absolutely sure nothing else is using 8123 on my system, but when using the debugger I can see that the waitForConnected is timing out instantly and not after 20 seconds at all, why ?

        [Edit] Modified code to:

        QElapsedTimer tmr;
        tmr.start();
        connectToHost(clsModHelper::mscpszHost, muint16XMLMPAMport);
        
        if ( waitForConnected(clsModHelper::mscintConnectionTimeout) != true ) {
           cout << tmr.elapsed() << endl;
           emit terminateModule();
        }
        ``
        In the Application Output, 4 is displayed.
        Pl45m4P Offline
        Pl45m4P Offline
        Pl45m4
        wrote on last edited by Pl45m4
        #2

        @SPlatten

        Are you on MS Windows?

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

        (from: https://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected)

        What timeout do you get, when you use the recommended alternative, the connected() - signal?

        https://doc.qt.io/qt-5/qabstractsocket.html#connected

        The connected() signal documentation also states, that it may stop instantly, when connecting to localhost.


        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
        1
        • Pl45m4P Pl45m4

          @SPlatten

          Are you on MS Windows?

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

          (from: https://doc.qt.io/qt-5/qabstractsocket.html#waitForConnected)

          What timeout do you get, when you use the recommended alternative, the connected() - signal?

          https://doc.qt.io/qt-5/qabstractsocket.html#connected

          The connected() signal documentation also states, that it may stop instantly, when connecting to localhost.

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

          @Pl45m4 No, iMAC:

          iMac (Retina 5K, 27-inch, Late 2015)
          Processor 4 GHz Quad-Core Intel Core i7
          Memory 16 GB 1867 MHz DDR3
          Graphics AMD Radeon R9 M395X 4 GB
          

          Qt:

          Qt Creator 4.13.2
          Based on Qt.5.15.1 (Clang 11.0 (Apple), 64 bit)
          Built on Oct 1 2020 01:16:45
          From revision 2ee1af2032
          

          My application is using Qt Kit:

          Desktop Qt 5.15.0 clang 64bit
          

          Before the code I do have connections to signals:

          connect(this, SIGNAL(connected()), this, SLOT(onConnected()));
          connect(this, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
          connect(this, SIGNAL(readyRead()), this, SLOT(onDataIn()));
          connect(this, &QAbstractSocket::errorOccurred, this, &clsModHelper::onErrorOccurred);
          

          My class is derived from QTcpSocket.

          Kind Regards,
          Sy

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

            Did you actually check which error you get? I mean - the OS will not wait 20 seconds until it tells you that e.g. the connection was refused.

            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
            3
            • SPlattenS SPlatten

              @Pl45m4 No, iMAC:

              iMac (Retina 5K, 27-inch, Late 2015)
              Processor 4 GHz Quad-Core Intel Core i7
              Memory 16 GB 1867 MHz DDR3
              Graphics AMD Radeon R9 M395X 4 GB
              

              Qt:

              Qt Creator 4.13.2
              Based on Qt.5.15.1 (Clang 11.0 (Apple), 64 bit)
              Built on Oct 1 2020 01:16:45
              From revision 2ee1af2032
              

              My application is using Qt Kit:

              Desktop Qt 5.15.0 clang 64bit
              

              Before the code I do have connections to signals:

              connect(this, SIGNAL(connected()), this, SLOT(onConnected()));
              connect(this, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
              connect(this, SIGNAL(readyRead()), this, SLOT(onDataIn()));
              connect(this, &QAbstractSocket::errorOccurred, this, &clsModHelper::onErrorOccurred);
              

              My class is derived from QTcpSocket.

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

              @SPlatten

              @Pl45m4 said in connectToHost and waitForConnected:

              The connected() signal documentation also states, that it may stop instantly, when connecting to localhost.

              Have you tested it with some remote port?


              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
              • Christian EhrlicherC Christian Ehrlicher

                Did you actually check which error you get? I mean - the OS will not wait 20 seconds until it tells you that e.g. the connection was refused.

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

                @Christian-Ehrlicher , thank you, the onErrorOccurred does get error 0. 0 = Connection Refused. Which I guess is correct if no server is up using that port.

                Kind Regards,
                Sy

                1 Reply Last reply
                0
                • Pl45m4P Pl45m4

                  @SPlatten

                  @Pl45m4 said in connectToHost and waitForConnected:

                  The connected() signal documentation also states, that it may stop instantly, when connecting to localhost.

                  Have you tested it with some remote port?

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

                  @Pl45m4 , what port could I try, 8123 isn't used on my system, I checked with:

                  netstat -a | grep 8123
                  

                  Nothing was returned.

                  Kind Regards,
                  Sy

                  Pl45m4P 1 Reply Last reply
                  0
                  • SPlattenS SPlatten

                    @Pl45m4 , what port could I try, 8123 isn't used on my system, I checked with:

                    netstat -a | grep 8123
                    

                    Nothing was returned.

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

                    @SPlatten

                    It's not about the port, but about the computer. As far as I understand the docs, it will accept / terminate the connection instantly when you connect to your local machine, which you do. So you probably never gonna get close to 20 secs.

                    @SPlatten said in connectToHost and waitForConnected:

                    0 = Connection Refused. Which I guess is correct if no server is up using that port.

                    Open ports shouldn't refuse connection requests. Any firewall that could block it?

                    Found out, that 8000-8999 are used by different Apple products (iTunes, irdmi service)
                    https://support.apple.com/en-us/HT202944


                    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
                    • SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by
                      #9

                      An IP connection is a combination of the IP address and the port, when making a connection you need to have the correct IP and port and since there was no application listening to both it failed. I should have realised this. I was just being stupid.

                      Kind Regards,
                      Sy

                      JonBJ 1 Reply Last reply
                      0
                      • SPlattenS SPlatten

                        An IP connection is a combination of the IP address and the port, when making a connection you need to have the correct IP and port and since there was no application listening to both it failed. I should have realised this. I was just being stupid.

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

                        @SPlatten
                        FTR: A TCP/IP connection is a unique combination of four numbers: server IP + port + client IP + port. the same combination cannot be reused. Usually the client specifies the server port to connect on but not which port to use from client side. That gets filled in during the connection (e.g. connectToHost()) with a "random", unused port number at client side. That's how the same client can make multiple, separate connections to the same server service port. You can see this by looking at the port numbers used at client side during, say, multiple FTP connections.

                        1 Reply Last reply
                        2

                        • Login

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