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.3k 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 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 Offline
    JonBJ Offline
    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 Offline
        Christian EhrlicherC Offline
        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 Offline
            Christian EhrlicherC Offline
            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 Offline
              JonBJ Offline
              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
                • D Offline
                  D Offline
                  Darta
                  wrote on last edited by Darta
                  #21

                  I have the same issue no listening server at the adress port but it's connecting anyway:

                  bool Client::bindTo(QString adress, quint16 port)
                  {
                      QHostAddress adressToCo;
                      if (adressToCo.setAddress(adress) == true)
                      {
                          m_socket = new QTcpSocket;
                          if (!m_socket->bind(adressToCo, port)) {
                              m_errorMsg = "Cannot connect to " + adress;
                              qDebug() << "Cannot connect to " + adress;
                          }
                      }
                      else {
                          m_errorMsg = "Wrong IP address.";
                          qDebug() << "Wrong IP address: " + adress;
                          return false;
                      }
                      qDebug() << "Bind corectly to " + adress;
                      return true;
                  }
                  
                  bool Client::connectTo(QString adress, quint16 port)
                  {
                      QHostAddress adressToCo;
                      adressToCo.setAddress(adress);
                      QObject::connect(m_socket, &QTcpSocket::readyRead, [=] {
                          QString msg = m_socket->readAll();
                          emit msgRcvd(msg);
                      });
                      m_socket->connectToHost(adressToCo, port);
                      if (!m_socket->waitForConnected(5000)) {
                          m_errorMsg = m_socket->errorString();
                          qDebug() << "Error socket:" << m_socket->errorString();
                          return false;
                      }
                      else if(m_socket->state() != QAbstractSocket::ConnectedState) {
                          m_errorMsg = m_socket->errorString();
                          qDebug() << "Error socket:" << m_socket->errorString();
                          return false;
                      }
                      qDebug() << "Connect corectly to " + adress;
                      m_socket->write("I'm writing a message that will be lost somewhere over the rainbow");
                      return true;
                  }
                  

                  I gave the same adress port to both functions :
                  Adress: "192.168.1.42" Port: 58513

                  Regardless the port number I put in,
                  I also could set the adress to "127.0.0.1" it is always the same issue:
                  the client is connected.

                  Output of software:

                  Adress: "192.168.1.42" Port: 58513
                  "Bind corectly to 192.168.1.42"
                  "Connect corectly to 192.168.1.42"
                  Client connected: true
                  

                  to get the final line i'm using :

                      m_socket->state() == QAbstractSocket::ConnectedState ? true : false;
                  

                  So everytimes the QTcpSocket connect itself to my adress regardless of my port...
                  When I launch my server, the server doesn't have any connection either

                  JonBJ 1 Reply Last reply
                  0
                  • D Darta

                    I have the same issue no listening server at the adress port but it's connecting anyway:

                    bool Client::bindTo(QString adress, quint16 port)
                    {
                        QHostAddress adressToCo;
                        if (adressToCo.setAddress(adress) == true)
                        {
                            m_socket = new QTcpSocket;
                            if (!m_socket->bind(adressToCo, port)) {
                                m_errorMsg = "Cannot connect to " + adress;
                                qDebug() << "Cannot connect to " + adress;
                            }
                        }
                        else {
                            m_errorMsg = "Wrong IP address.";
                            qDebug() << "Wrong IP address: " + adress;
                            return false;
                        }
                        qDebug() << "Bind corectly to " + adress;
                        return true;
                    }
                    
                    bool Client::connectTo(QString adress, quint16 port)
                    {
                        QHostAddress adressToCo;
                        adressToCo.setAddress(adress);
                        QObject::connect(m_socket, &QTcpSocket::readyRead, [=] {
                            QString msg = m_socket->readAll();
                            emit msgRcvd(msg);
                        });
                        m_socket->connectToHost(adressToCo, port);
                        if (!m_socket->waitForConnected(5000)) {
                            m_errorMsg = m_socket->errorString();
                            qDebug() << "Error socket:" << m_socket->errorString();
                            return false;
                        }
                        else if(m_socket->state() != QAbstractSocket::ConnectedState) {
                            m_errorMsg = m_socket->errorString();
                            qDebug() << "Error socket:" << m_socket->errorString();
                            return false;
                        }
                        qDebug() << "Connect corectly to " + adress;
                        m_socket->write("I'm writing a message that will be lost somewhere over the rainbow");
                        return true;
                    }
                    

                    I gave the same adress port to both functions :
                    Adress: "192.168.1.42" Port: 58513

                    Regardless the port number I put in,
                    I also could set the adress to "127.0.0.1" it is always the same issue:
                    the client is connected.

                    Output of software:

                    Adress: "192.168.1.42" Port: 58513
                    "Bind corectly to 192.168.1.42"
                    "Connect corectly to 192.168.1.42"
                    Client connected: true
                    

                    to get the final line i'm using :

                        m_socket->state() == QAbstractSocket::ConnectedState ? true : false;
                    

                    So everytimes the QTcpSocket connect itself to my adress regardless of my port...
                    When I launch my server, the server doesn't have any connection either

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

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

                    I have the same issue no listening server at the adress port but it's connecting anyway:

                    "Bind corectly to 192.168.1.42"

                    "Connect corectly to 192.168.1.42"

                    I don't understand. Your bindTo() does the listening, the connectTo() connects to that, they are both using the same port number, your program is acting as both client and server. That's how it looks to me, so what's the problem?

                    D 1 Reply Last reply
                    0
                    • JonBJ JonB

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

                      I have the same issue no listening server at the adress port but it's connecting anyway:

                      "Bind corectly to 192.168.1.42"

                      "Connect corectly to 192.168.1.42"

                      I don't understand. Your bindTo() does the listening, the connectTo() connects to that, they are both using the same port number, your program is acting as both client and server. That's how it looks to me, so what's the problem?

                      D Offline
                      D Offline
                      Darta
                      wrote on last edited by Darta
                      #23

                      @JonB I divid it in two functions because my server only use one, but my client is using both like that :

                          QSettings vmIni("qrc:/VirtualHome.ini");
                      
                          QString ip = vmIni.value("ServerIP").toString();
                          quint16 port = vmIni.value("ServerPort").toUInt();
                          qDebug() << "Adress:" << ip << "Port:" << port;
                          if (m_client.bindTo(ip, port)) {
                              if (!m_client.connectTo(ip, port))
                                  qDebug() << "Fail to connect to server.";
                          }
                          else
                              qDebug() << "Fail to bind to server";
                          qDebug() << "Client connected:" << m_client.isConnected();
                      
                      JonBJ 1 Reply Last reply
                      0
                      • D Darta

                        @JonB I divid it in two functions because my server only use one, but my client is using both like that :

                            QSettings vmIni("qrc:/VirtualHome.ini");
                        
                            QString ip = vmIni.value("ServerIP").toString();
                            quint16 port = vmIni.value("ServerPort").toUInt();
                            qDebug() << "Adress:" << ip << "Port:" << port;
                            if (m_client.bindTo(ip, port)) {
                                if (!m_client.connectTo(ip, port))
                                    qDebug() << "Fail to connect to server.";
                            }
                            else
                                qDebug() << "Fail to bind to server";
                            qDebug() << "Client connected:" << m_client.isConnected();
                        
                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #24

                        @Darta
                        I suggest you take this to your own thread which you start. It won't be to do with this old one.

                        I devid it in two function because my server only use one of those but my client where i'm using both directly at the same moment like that :

                        I'm afraid I don't understand this sentence and doubt others will.

                        QTcpSocket is not going to be successfully connecting to an IP/port if there is nothing bound to and listening on that.

                        D 2 Replies Last reply
                        0
                        • JonBJ JonB

                          @Darta
                          I suggest you take this to your own thread which you start. It won't be to do with this old one.

                          I devid it in two function because my server only use one of those but my client where i'm using both directly at the same moment like that :

                          I'm afraid I don't understand this sentence and doubt others will.

                          QTcpSocket is not going to be successfully connecting to an IP/port if there is nothing bound to and listening on that.

                          D Offline
                          D Offline
                          Darta
                          wrote on last edited by
                          #25

                          @JonB my server is irrelevant since i'm not launching it

                          1 Reply Last reply
                          0
                          • JonBJ JonB

                            @Darta
                            I suggest you take this to your own thread which you start. It won't be to do with this old one.

                            I devid it in two function because my server only use one of those but my client where i'm using both directly at the same moment like that :

                            I'm afraid I don't understand this sentence and doubt others will.

                            QTcpSocket is not going to be successfully connecting to an IP/port if there is nothing bound to and listening on that.

                            D Offline
                            D Offline
                            Darta
                            wrote on last edited by
                            #26

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

                            QTcpSocket is not going to be successfully connecting to an IP/port if there is nothing bound to and listening on that.

                            why can't you just trust what I've experienced ?
                            I'm telling you that I have no server and using both functions like I shown previously I get the ouptut connected every times it's not to debate I'm asking if I've done something wrong that would make it that way nothing more but nothing less

                            Christian EhrlicherC 1 Reply Last reply
                            0
                            • D Darta

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

                              QTcpSocket is not going to be successfully connecting to an IP/port if there is nothing bound to and listening on that.

                              why can't you just trust what I've experienced ?
                              I'm telling you that I have no server and using both functions like I shown previously I get the ouptut connected every times it's not to debate I'm asking if I've done something wrong that would make it that way nothing more but nothing less

                              Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #27

                              Please provide a minimal, compilable example, not some code fragments you're insisting that nothing else is running and we can't check.

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

                              D 1 Reply Last reply
                              0
                              • Christian EhrlicherC Christian Ehrlicher

                                Please provide a minimal, compilable example, not some code fragments you're insisting that nothing else is running and we can't check.

                                D Offline
                                D Offline
                                Darta
                                wrote on last edited by Darta
                                #28

                                @Christian-Ehrlicher as previously suggested I created my own topic here

                                1 Reply Last reply
                                1

                                • Login

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