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 connecting even tho no server?

QTcpSocket connecting even tho no server?

Scheduled Pinned Locked Moved Unsolved General and Desktop
28 Posts 7 Posters 5.2k Views 1 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
    #1

    In my application I am using QTcpSocket to connect to another process. The other process would be on the same system listening to a different port in this case 8124. This process isn't running.

    My main application tries to connect to this process using:

    mpsckClient->connectToHost(strIP, muint16Port);
    

    Where strIP is my local system IP address and muint16Port is 8124. I would expect this to fail because nothing is running listening to that port. However after the call the isOpen function returns true which indicates it is connected, why is this the case?

    This is my onStartupSlot:

    void clsMsgSender::onStartService() {
    qdbg() << mpsckClient->isOpen();
        if ( mpsckClient->isOpen() != true ) {
        //Change connection interval to regular connection time period for reconnects
            mptmrConnect->setInterval(clsMsgSender::mscuint16ConnectTimer);
        //Get local IP address
            QString strIP = clsMsgSender::strGetLocalIP();
            qdbg() << "Connecting to: " << strIP << ":" << muint16Port;
        //Connect to the Application
            mpsckClient->connectToHost(strIP, muint16Port);
        }
        if ( clsMsgSender::mspThread == nullptr ) {
            clsMsgSender::mspThread = new QThread;
            moveToThread(clsMsgSender::mspThread);
            QObject::connect(clsMsgSender::mspThread, &QThread::started
                        ,this, &clsMsgSender::run);
            QObject::connect(this, &clsMsgSender::finished
                        ,clsMsgSender::mspThread, &QThread::quit);
        }
        if ( mqueMsgsOut.size() > 0 && clsMsgSender::mspThread != nullptr ) {
            clsMsgSender::mspThread->start();
        }
    }
    

    I can see that isOpen is false on the first call, then true after the connectToHost.

    Kind Regards,
    Sy

    J.HilkJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      In my application I am using QTcpSocket to connect to another process. The other process would be on the same system listening to a different port in this case 8124. This process isn't running.

      My main application tries to connect to this process using:

      mpsckClient->connectToHost(strIP, muint16Port);
      

      Where strIP is my local system IP address and muint16Port is 8124. I would expect this to fail because nothing is running listening to that port. However after the call the isOpen function returns true which indicates it is connected, why is this the case?

      This is my onStartupSlot:

      void clsMsgSender::onStartService() {
      qdbg() << mpsckClient->isOpen();
          if ( mpsckClient->isOpen() != true ) {
          //Change connection interval to regular connection time period for reconnects
              mptmrConnect->setInterval(clsMsgSender::mscuint16ConnectTimer);
          //Get local IP address
              QString strIP = clsMsgSender::strGetLocalIP();
              qdbg() << "Connecting to: " << strIP << ":" << muint16Port;
          //Connect to the Application
              mpsckClient->connectToHost(strIP, muint16Port);
          }
          if ( clsMsgSender::mspThread == nullptr ) {
              clsMsgSender::mspThread = new QThread;
              moveToThread(clsMsgSender::mspThread);
              QObject::connect(clsMsgSender::mspThread, &QThread::started
                          ,this, &clsMsgSender::run);
              QObject::connect(this, &clsMsgSender::finished
                          ,clsMsgSender::mspThread, &QThread::quit);
          }
          if ( mqueMsgsOut.size() > 0 && clsMsgSender::mspThread != nullptr ) {
              clsMsgSender::mspThread->start();
          }
      }
      

      I can see that isOpen is false on the first call, then true after the connectToHost.

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

      @SPlatten isOpen() is from the very base QIODevice class, I'm not sure when exactly this is set.

      What you should be looking for in your QTcpSocket instance, is state() and wether or not that is QAbstractSocket::ConnectedState


      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.

      SPlattenS 1 Reply Last reply
      3
      • J.HilkJ J.Hilk

        @SPlatten isOpen() is from the very base QIODevice class, I'm not sure when exactly this is set.

        What you should be looking for in your QTcpSocket instance, is state() and wether or not that is QAbstractSocket::ConnectedState

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

        @J-Hilk , thank you, I've changed the code to:

        void clsMsgSender::onStartService() {
        qdbg() << mpsckClient->state();
            if ( mpsckClient->state() != QAbstractSocket::ConnectedState ) {
            //Change connection interval to regular connection time period for reconnects
                mptmrConnect->setInterval(clsMsgSender::mscuint16ConnectTimer);
            //Get local IP address
                QString strIP = clsMsgSender::strGetLocalIP();
                qdbg() << "Connecting to: " << strIP << ":" << muint16Port;
            //Connect to the Application
                mpsckClient->connectToHost(strIP, muint16Port);
            }
            if ( clsMsgSender::mspThread == nullptr ) {
                clsMsgSender::mspThread = new QThread;
                moveToThread(clsMsgSender::mspThread);
                QObject::connect(clsMsgSender::mspThread, &QThread::started
                            ,this, &clsMsgSender::run);
                QObject::connect(this, &clsMsgSender::finished
                            ,clsMsgSender::mspThread, &QThread::quit);
            }
            if ( mqueMsgsOut.size() > 0 && clsMsgSender::mspThread != nullptr ) {
                clsMsgSender::mspThread->start();
            }
        }
        

        I've also added a connection to:

        QObject::connect(mpsckClient, &QTcpSocket::hostFound
                            ,this, &clsMsgSender::onHostFound);
        

        In my onHostFound slot:

        void clsMsgSender::onHostFound() {
           qdbg() << "Host Found!";
        }
        

        From the Application Output I can see lots of Host Found! messages, is this normal when the IP:Port doesn't have anything listening to it?

        Kind Regards,
        Sy

        O KroMignonK 2 Replies Last reply
        0
        • SPlattenS SPlatten

          @J-Hilk , thank you, I've changed the code to:

          void clsMsgSender::onStartService() {
          qdbg() << mpsckClient->state();
              if ( mpsckClient->state() != QAbstractSocket::ConnectedState ) {
              //Change connection interval to regular connection time period for reconnects
                  mptmrConnect->setInterval(clsMsgSender::mscuint16ConnectTimer);
              //Get local IP address
                  QString strIP = clsMsgSender::strGetLocalIP();
                  qdbg() << "Connecting to: " << strIP << ":" << muint16Port;
              //Connect to the Application
                  mpsckClient->connectToHost(strIP, muint16Port);
              }
              if ( clsMsgSender::mspThread == nullptr ) {
                  clsMsgSender::mspThread = new QThread;
                  moveToThread(clsMsgSender::mspThread);
                  QObject::connect(clsMsgSender::mspThread, &QThread::started
                              ,this, &clsMsgSender::run);
                  QObject::connect(this, &clsMsgSender::finished
                              ,clsMsgSender::mspThread, &QThread::quit);
              }
              if ( mqueMsgsOut.size() > 0 && clsMsgSender::mspThread != nullptr ) {
                  clsMsgSender::mspThread->start();
              }
          }
          

          I've also added a connection to:

          QObject::connect(mpsckClient, &QTcpSocket::hostFound
                              ,this, &clsMsgSender::onHostFound);
          

          In my onHostFound slot:

          void clsMsgSender::onHostFound() {
             qdbg() << "Host Found!";
          }
          

          From the Application Output I can see lots of Host Found! messages, is this normal when the IP:Port doesn't have anything listening to it?

          O Offline
          O Offline
          ollarch
          wrote on last edited by
          #4

          @SPlatten said in QTcpSocket connecting even tho no server?:

          From the Application Output I can see lots of Host Found! messages, is this normal when the IP:Port doesn't have anything listening to it?

          But the host if found. The problem will be if the localhost is not found. One thing is that it found the host and the other thing is that there is some process linstening on the port.

          SPlattenS 1 Reply Last reply
          2
          • O ollarch

            @SPlatten said in QTcpSocket connecting even tho no server?:

            From the Application Output I can see lots of Host Found! messages, is this normal when the IP:Port doesn't have anything listening to it?

            But the host if found. The problem will be if the localhost is not found. One thing is that it found the host and the other thing is that there is some process linstening on the port.

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

            @ollarch , the IP exists on the system, but there is no application running listening for connection, nothing is using that port.

            Kind Regards,
            Sy

            J.HilkJ O 2 Replies Last reply
            0
            • SPlattenS SPlatten

              @J-Hilk , thank you, I've changed the code to:

              void clsMsgSender::onStartService() {
              qdbg() << mpsckClient->state();
                  if ( mpsckClient->state() != QAbstractSocket::ConnectedState ) {
                  //Change connection interval to regular connection time period for reconnects
                      mptmrConnect->setInterval(clsMsgSender::mscuint16ConnectTimer);
                  //Get local IP address
                      QString strIP = clsMsgSender::strGetLocalIP();
                      qdbg() << "Connecting to: " << strIP << ":" << muint16Port;
                  //Connect to the Application
                      mpsckClient->connectToHost(strIP, muint16Port);
                  }
                  if ( clsMsgSender::mspThread == nullptr ) {
                      clsMsgSender::mspThread = new QThread;
                      moveToThread(clsMsgSender::mspThread);
                      QObject::connect(clsMsgSender::mspThread, &QThread::started
                                  ,this, &clsMsgSender::run);
                      QObject::connect(this, &clsMsgSender::finished
                                  ,clsMsgSender::mspThread, &QThread::quit);
                  }
                  if ( mqueMsgsOut.size() > 0 && clsMsgSender::mspThread != nullptr ) {
                      clsMsgSender::mspThread->start();
                  }
              }
              

              I've also added a connection to:

              QObject::connect(mpsckClient, &QTcpSocket::hostFound
                                  ,this, &clsMsgSender::onHostFound);
              

              In my onHostFound slot:

              void clsMsgSender::onHostFound() {
                 qdbg() << "Host Found!";
              }
              

              From the Application Output I can see lots of Host Found! messages, is this normal when the IP:Port doesn't have anything listening to it?

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

              @SPlatten said in QTcpSocket connecting even tho no server?:

              From the Application Output I can see lots of Host Found! messages, is this normal when the IP:Port doesn't have anything listening to it?

              Why do you never read documentation before asking what already describe in documentation?
              Have you tried to read connectToHost() documentation (hint: just hit F1 key in Qt Creator):

              Attempts to make a connection to hostName on the given port. The protocol parameter can be used to specify which network protocol to use (eg. IPv4 or IPv6).

              The socket is opened in the given openMode and first enters HostLookupState, then performs a host name lookup of hostName. If the lookup succeeds, hostFound() is emitted and QAbstractSocket enters ConnectingState. It then attempts to connect to the address or addresses returned by the lookup. Finally, if a connection is established, QAbstractSocket enters ConnectedState and emits connected().

              At any point, the socket can emit errorOccurred() to signal that an error occurred.

              hostName may be an IP address in string form (e.g., "43.195.83.32"), or it may be a host name (e.g., "example.com"). QAbstractSocket will do a lookup only if required. port is in native byte order.

              As you could read:

              • the socket is opened, so QTcpSocket::isOpen() will return true
              • hostname lookup is started ==> hostFound() is emitted on success
              • on success, connection is started

              It is so hard to read documentation????

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

                @ollarch , the IP exists on the system, but there is no application running listening for connection, nothing is using that port.

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

                @SPlatten do you call connectToHost multiple times ? IIRC each connectTohost call may emit a HostFound signal


                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.

                SPlattenS 1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @SPlatten do you call connectToHost multiple times ? IIRC each connectTohost call may emit a HostFound signal

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

                  @J-Hilk I do call every 5 seconds.

                  Kind Regards,
                  Sy

                  1 Reply Last reply
                  0
                  • SPlattenS SPlatten

                    @ollarch , the IP exists on the system, but there is no application running listening for connection, nothing is using that port.

                    O Offline
                    O Offline
                    ollarch
                    wrote on last edited by
                    #9

                    @SPlatten said in QTcpSocket connecting even tho no server?:

                    @ollarch , the IP exists on the system, but there is no application running listening for connection, nothing is using that port.

                    So, the host is found.
                    As @KroMignon has told you a lot of times is to read the documentation.
                    It seems that you write some code, test, and if it fails you start writing a new thread in the forum.
                    Yes, I know that you have a lot work to do but this also applies to us.

                    SPlattenS 1 Reply Last reply
                    5
                    • O ollarch

                      @SPlatten said in QTcpSocket connecting even tho no server?:

                      @ollarch , the IP exists on the system, but there is no application running listening for connection, nothing is using that port.

                      So, the host is found.
                      As @KroMignon has told you a lot of times is to read the documentation.
                      It seems that you write some code, test, and if it fails you start writing a new thread in the forum.
                      Yes, I know that you have a lot work to do but this also applies to us.

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

                      @ollarch, I have read the documentation, and nowhere does it say that the hostFound signal is issue when the IP is found regardless of the port!

                      The IP and Port combine make the address, the IP itself is no indication that there is anything to connect to, not if there is nothing using the specified port. And this information is not documented, hence my question.

                      Kind Regards,
                      Sy

                      JonBJ 1 Reply Last reply
                      0
                      • Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @SPlatten said in QTcpSocket connecting even tho no server?:

                        and nowhere does it say that the hostFound signal is issue when the IP is found regardless of the port!

                        Sorry but the error is called 'HostNotFound' - not 'Can not connect to host and port'...

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

                        1 Reply Last reply
                        0
                        • KroMignonK KroMignon

                          @SPlatten said in QTcpSocket connecting even tho no server?:

                          From the Application Output I can see lots of Host Found! messages, is this normal when the IP:Port doesn't have anything listening to it?

                          Why do you never read documentation before asking what already describe in documentation?
                          Have you tried to read connectToHost() documentation (hint: just hit F1 key in Qt Creator):

                          Attempts to make a connection to hostName on the given port. The protocol parameter can be used to specify which network protocol to use (eg. IPv4 or IPv6).

                          The socket is opened in the given openMode and first enters HostLookupState, then performs a host name lookup of hostName. If the lookup succeeds, hostFound() is emitted and QAbstractSocket enters ConnectingState. It then attempts to connect to the address or addresses returned by the lookup. Finally, if a connection is established, QAbstractSocket enters ConnectedState and emits connected().

                          At any point, the socket can emit errorOccurred() to signal that an error occurred.

                          hostName may be an IP address in string form (e.g., "43.195.83.32"), or it may be a host name (e.g., "example.com"). QAbstractSocket will do a lookup only if required. port is in native byte order.

                          As you could read:

                          • the socket is opened, so QTcpSocket::isOpen() will return true
                          • hostname lookup is started ==> hostFound() is emitted on success
                          • on success, connection is started

                          It is so hard to read documentation????

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

                          @KroMignon , I didn't say I never read the documentation, that's your inference and on this occasion I find that the documentation is not detailed enough. An address has two parts, the IP and the port.

                          Kind Regards,
                          Sy

                          KroMignonK 1 Reply Last reply
                          0
                          • SPlattenS SPlatten

                            @KroMignon , I didn't say I never read the documentation, that's your inference and on this occasion I find that the documentation is not detailed enough. An address has two parts, the IP and the port.

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

                            @SPlatten said in QTcpSocket connecting even tho no server?:

                            I find that the documentation is not detailed enough. An address has two parts, the IP and the port.

                            It is written in documentation:
                            The socket is opened in the given openMode and first enters HostLookupState, then performs a host name lookup of hostName. If the lookup succeeds, hostFound() is emitted

                            => host name lookup, this is done before connection starts, so no need of TCP port.

                            This is very explicitly described in documentation.

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

                              @ollarch, I have read the documentation, and nowhere does it say that the hostFound signal is issue when the IP is found regardless of the port!

                              The IP and Port combine make the address, the IP itself is no indication that there is anything to connect to, not if there is nothing using the specified port. And this information is not documented, hence my question.

                              JonBJ Online
                              JonBJ Online
                              JonB
                              wrote on last edited by JonB
                              #14

                              @SPlatten
                              I don't want to get into an argument, but it does say that.

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

                              This signal is emitted after connectToHost() has been called and the host lookup has succeeded.

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

                              The socket is opened in the given openMode and first enters HostLookupState, then performs a host name lookup of hostName. If the lookup succeeds, hostFound() is emitted and QAbstractSocket enters ConnectingState. It then attempts to connect to the address or addresses returned by the lookup. Finally, if a connection is established, QAbstractSocket enters ConnectedState and emits connected().

                              So, as it says, hostFound signal is emitted on finding the host IP address. It does what it says on the packet, i.e. it's just a host lookup, by name or IP address, and tells you the host does exist.

                              Connecting on the port comes after that, and of course may succeed or fail separately.

                              Anyway, there it is, that's how it works.

                              SPlattenS 1 Reply Last reply
                              2
                              • JonBJ JonB

                                @SPlatten
                                I don't want to get into an argument, but it does say that.

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

                                This signal is emitted after connectToHost() has been called and the host lookup has succeeded.

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

                                The socket is opened in the given openMode and first enters HostLookupState, then performs a host name lookup of hostName. If the lookup succeeds, hostFound() is emitted and QAbstractSocket enters ConnectingState. It then attempts to connect to the address or addresses returned by the lookup. Finally, if a connection is established, QAbstractSocket enters ConnectedState and emits connected().

                                So, as it says, hostFound signal is emitted on finding the host IP address. It does what it says on the packet, i.e. it's just a host lookup, by name or IP address, and tells you the host does exist.

                                Connecting on the port comes after that, and of course may succeed or fail separately.

                                Anyway, there it is, that's how it works.

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

                                @JonB , as I said previously the IP is just part of the address, the port completes the address, when you establish the connection the port is optional, but if supplied then it should be used because connecting to address and address:port are very different.

                                Kind Regards,
                                Sy

                                Christian EhrlicherC JonBJ KroMignonK 3 Replies Last reply
                                0
                                • SPlattenS SPlatten

                                  @JonB , as I said previously the IP is just part of the address, the port completes the address, when you establish the connection the port is optional, but if supplied then it should be used because connecting to address and address:port are very different.

                                  Christian EhrlicherC Online
                                  Christian EhrlicherC Online
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @SPlatten said in QTcpSocket connecting even tho no server?:

                                  address:port

                                  This is wrong. It's host:port !

                                  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

                                    @SPlatten said in QTcpSocket connecting even tho no server?:

                                    address:port

                                    This is wrong. It's host:port !

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

                                    @Christian-Ehrlicher , are you being piccy?

                                    Kind Regards,
                                    Sy

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

                                      @SPlatten said in QTcpSocket connecting even tho no server?:

                                      are you being piccy?

                                      No, but your assumptions are wrong because of this wrong definition.

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

                                      1 Reply Last reply
                                      0
                                      • SPlattenS SPlatten

                                        @JonB , as I said previously the IP is just part of the address, the port completes the address, when you establish the connection the port is optional, but if supplied then it should be used because connecting to address and address:port are very different.

                                        JonBJ Online
                                        JonBJ Online
                                        JonB
                                        wrote on last edited by JonB
                                        #19

                                        @SPlatten
                                        Sorry, Simon, you're incorrect. I did try to explain. I do know what IP addresses vs ports are. I was just pointing out, politely, that Qt's hostFound is emitted on host found, not on port is listening/connected. Anyways, it's not worth arguing about, we were just talking about how Qt documents what it is that it does when.

                                        1 Reply Last reply
                                        2
                                        • SPlattenS SPlatten

                                          @JonB , as I said previously the IP is just part of the address, the port completes the address, when you establish the connection the port is optional, but if supplied then it should be used because connecting to address and address:port are very different.

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

                                          @SPlatten said in QTcpSocket connecting even tho no server?:

                                          as I said previously the IP is just part of the address, the port completes the address, when you establish the connection the port is optional, but if supplied then it should be used because connecting to address and address:port are very different.

                                          Your are mixing up definitions:

                                          • Hostname is IP address or a name (like "www.qt.com")
                                          • portnum is the TCP port (number between 0 and 65534).
                                          • combination of hostname and TCP port is an Endpoint
                                          • a TCP connection is defined as a combination of 2 endpoints, the local endpoint and the remote endpoint

                                          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
                                          9

                                          • Login

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