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 to no server!

QTcpSocket connecting even to no server!

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 301 Views
  • 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.
  • D Offline
    D Offline
    Darta
    wrote on last edited by Darta
    #1

    hello everyone,

    I have an issue with my usage of a QTcpSocket that will connect to no server no matter the adress as long as it is a valide one and regardless of the port :

    function used to call the two functions that will actualy connect :

    QSettings vmIni("qrc:/VirtualHome.ini");
    
        vmIni.setValue("ServerIP", "127.0.0.1");
        vmIni.setValue("ServerPort", 58513);
        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();
    

    and my two functions that actually connects to a supposed server:

    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 = "Bind error to adress: " + adress;
                qDebug() << "Bind error to adress: " + 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("writing a useless sentence.");
        return true;
    }
    

    so when I put a correct address with any ports I get the output:

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

    at the end we can see that :

    bool    Client::isConnected(void)
    {
        return m_socket->state() == QAbstractSocket::ConnectedState ? true : false;
    }
    

    it return true every single times

    Christian EhrlicherC 1 Reply Last reply
    0
    • D Darta referenced this topic on
    • D Darta

      hello everyone,

      I have an issue with my usage of a QTcpSocket that will connect to no server no matter the adress as long as it is a valide one and regardless of the port :

      function used to call the two functions that will actualy connect :

      QSettings vmIni("qrc:/VirtualHome.ini");
      
          vmIni.setValue("ServerIP", "127.0.0.1");
          vmIni.setValue("ServerPort", 58513);
          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();
      

      and my two functions that actually connects to a supposed server:

      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 = "Bind error to adress: " + adress;
                  qDebug() << "Bind error to adress: " + 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("writing a useless sentence.");
          return true;
      }
      

      so when I put a correct address with any ports I get the output:

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

      at the end we can see that :

      bool    Client::isConnected(void)
      {
          return m_socket->state() == QAbstractSocket::ConnectedState ? true : false;
      }
      

      it return true every single times

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

      @Darta said in QTcpSocket connecting even to no server!:

      it return true every single times

      And I don't see why not - first you open a port with bind() and then you connect to this opened port with connectTo() which is completely useless and strange but that's not a reason that it will not work.

      /edit: You will and must not call bind() on a client socket.

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

        @Darta said in QTcpSocket connecting even to no server!:

        it return true every single times

        And I don't see why not - first you open a port with bind() and then you connect to this opened port with connectTo() which is completely useless and strange but that's not a reason that it will not work.

        /edit: You will and must not call bind() on a client socket.

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

        @Christian-Ehrlicher thank's very much ^^ It works fine now

        1 Reply Last reply
        0
        • D Darta has marked this topic as solved on

        • Login

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