Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTcpSocket connecting even tho no server?



  • 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.


  • Moderators

    @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



  • @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?



  • @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.



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



  • @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????


  • Moderators

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



  • @J-Hilk I do call every 5 seconds.



  • @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.



  • @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.


  • Lifetime Qt Champion

    @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'...



  • @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.



  • @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.



  • @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.



  • @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.


  • Lifetime Qt Champion

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

    address:port

    This is wrong. It's host:port !



  • @Christian-Ehrlicher , are you being piccy?


  • Lifetime Qt Champion

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

    are you being piccy?

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



  • @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.



  • @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

Log in to reply