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. QTcpServer, listen, is the bind internal?
Qt 6.11 is out! See what's new in the release blog

QTcpServer, listen, is the bind internal?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 674 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 SPlatten
    #1

    I have two applications one is using QTcpServer, call it App A and other is a client and uses QTcpSocket, call it App B.

    A listens to Any IP on port 8123. There is a signal called newConnection which I connect to a slot:

    void clsSocketServer::sayHello() {
        clsSocketClient* pClient = new clsSocketClient(nextPendingConnection());
    
        if ( pClient == nullptr ) {
            return;
        }
        QByteArray arybytMsg;
        QDataStream dsOut(&arybytMsg, QIODevice::WriteOnly);
        dsOut.setVersion(clsJSON::mscintQtVersion);
        QJsonObject objMsg;
        objMsg.insert(clsJSON::mscszModule, clsMainWnd::mscstrTitle);
        dsOut << QJsonDocument(objMsg).toJson(QJsonDocument::Compact);
        connect(pClient, &QAbstractSocket::disconnected
               ,pClient, &QObject::deleteLater);
        pClient->sendJSON(objMsg);
    }
    

    Application B looks for a local IP address and connects to it on the same port:

    void clsModHelper::onConnectToXMLMPAM() {
        if ( isOpen() == true ) {
        //Already connected do nothing!
            return;
        }
        //Get the I/P address
        QList<QHostAddress> lstAddresses = QNetworkInterface::allAddresses();
        QString strIP;
        for( int i=0; i<lstAddresses.size(); i++ ) {
            if ( lstAddresses[i] != QHostAddress::LocalHost
              && lstAddresses[i].toIPv4Address() ) {
                strIP = lstAddresses[i].toString();
                break;
            }
        }
        if ( strIP.isEmpty() == true ) {
            strIP = QHostAddress(QHostAddress::LocalHost).toString();
        }
        //Connect to the Application
        qdbg() << "Connecting to: " << strIP << ":" << muint16XMLMPAMport;
        connectToHost(strIP, muint16XMLMPAMport);
    }
    

    When a connection from B to A is established the signal 'connected' is emitted and I can see a message is logged by the slot in application B 'onConnected':

    void clsModFileIO::onConnected() {
        qdbg() << "onConnected";
    }
    

    I'm confused as application A shows no sign of the new connection, but if I terminate application A, application B gets the 'disconnected' signal, I can see that because the slot in application B:

    void clsModFileIO::onDisconnected() {
        //Close the exiting connection
        close();
        //Re-open the connection
        connectToXMLMPAM();
    }
    

    I would have expected to see in Application A that the slot that is connected to the 'newConnection' signal gets called, but it does not. I'm confused about what is going on and how I can progress this?

    Application A signal connections:

    connect(this, &QTcpServer::newConnection, this, &clsSocketServer::sayHello);
    

    Application B signal connections:

        QObject::connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit
                        ,this, &clsModHelper::onExitModule);
        QObject::connect(this, SIGNAL(connected()), this, SLOT(onConnected()));
        QObject::connect(this, SIGNAL(connected()), this, SLOT(onSendModuleStartupMsg()));
        QObject::connect(this, SIGNAL(connected()), this, SLOT(onStartHeartbeat()));
        QObject::connect(this, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
        QObject::connect(this, &QAbstractSocket::errorOccurred, this, &clsModHelper::onErrorOccurred);
        QObject::connect(this, &QIODevice::readyRead, this, &clsModHelper::onDataIn);
        QObject::connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));        
        QObject::connect(this, &clsModHelper::terminateModule, this, &clsModHelper::onExitModule);
    

    Application A , log:

    D00000000000000000001:XMLMPAM is listening on: 192.168.1.158:8123
    D00000000000000000002:L00000141Fsimon.js[void clsScriptHelper::log]:
    D00000000000000000003:************** setup ****************
    D00000000000000000004:L00000116Fsimon.js[void clsScriptHelper::log]:
    D00000000000000000005:************** connect ****************
    D00000000000000000006:L00000062Fsimon.js[void clsScriptHelper::log]:
    D00000000000000000007:L00000047Fsimon.js[QProcess *clsScriptHelper::pLaunch]:
    D00000000000000000008:Checking for PID for: /Users/simonplatten/XMLMPAM/config/modules/mdFileIO
    D00000000000000000009:Process: /Users/simonplatten/XMLMPAM/config/modules/mdFileIO started, PID: 80462
    D00000000000000000010:L00000100Fsimon.js[void clsScriptHelper::log]:
    D00000000000000000011:************** setupFIO ****************
    

    Application B, log:

    D00000000000000000001:mdFileIO Version: 1.00
    D00000000000000000002:Connecting to: 192.168.1.158:8123
    D00000000000000000003:onConnected
    D00000000000000000004:sendJSON: 54
    D00000000000000000005:clsModHelper::onBytesWritten:54
    D00000000000000000006:sendJSON: 54
    D00000000000000000007:clsModHelper::onBytesWritten:54
    D00000000000000000008:sendJSON: 54
    D00000000000000000009:clsModHelper::onBytesWritten:54
    D00000000000000000010:sendJSON: 54
    D00000000000000000011:clsModHelper::onBytesWritten:54
    D00000000000000000012:sendJSON: 54
    D00000000000000000013:clsModHelper::onBytesWritten:54
    D00000000000000000014:sendJSON: 54
    D00000000000000000015:clsModHelper::onBytesWritten:54
    

    Kind Regards,
    Sy

    JonBJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      Sorry, the newConnection signal connection is in the constructor:

      clsSocketServer::clsSocketServer(QObject* pParent) : QTcpServer(pParent) {
          QString strListenFailure = QString("Cannot listen to port: ") + QString::number(clsSocketServer::mscuint16port);
          listen(QHostAddress::Any, clsSocketServer::mscuint16port);
          QList<QHostAddress> lstAddresses = QNetworkInterface::allAddresses();
          QString strIP;
          for( int i=0; i<lstAddresses.size(); i++ ) {
              if ( lstAddresses[i] != QHostAddress::LocalHost
                && lstAddresses[i].toIPv4Address() ) {
                  strIP = lstAddresses[i].toString();
                  break;
              }
          }
          if ( strIP.isEmpty() == true ) {
              strIP = QHostAddress(QHostAddress::LocalHost).toString();
          }
          qdbg() << tr("XMLMPAM is listening on: %1:%2\n")
                      .arg(strIP).arg(clsSocketServer::mscuint16port);
          QObject::connect(this, &QTcpServer::newConnection, this, &clsSocketServer::sayHello);
          Q_ASSERT_X(isListening(), "clsSocketServer::clsSocketServer", strListenFailure.toLatin1().data());
      }
      

      No it doesn't look like it is being called, however its clear that a connection is made because when I terminate Application A, application B gets disconnect signals.

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

      @SPlatten

      void clsSocketServer::sayHello() {
          clsSocketClient* pClient = new clsSocketClient(nextPendingConnection());
      
          if ( pClient == nullptr ) {
              return;
          }
      
      

      Can you please put in a qDebug() as the very first line of this slot. I don't know how the rest of your code behaves, you imply here it might fail....

      SPlattenS 1 Reply Last reply
      0
      • O Offline
        O Offline
        ollarch
        wrote on last edited by
        #2
        This post is deleted!
        1 Reply Last reply
        0
        • SPlattenS SPlatten

          I have two applications one is using QTcpServer, call it App A and other is a client and uses QTcpSocket, call it App B.

          A listens to Any IP on port 8123. There is a signal called newConnection which I connect to a slot:

          void clsSocketServer::sayHello() {
              clsSocketClient* pClient = new clsSocketClient(nextPendingConnection());
          
              if ( pClient == nullptr ) {
                  return;
              }
              QByteArray arybytMsg;
              QDataStream dsOut(&arybytMsg, QIODevice::WriteOnly);
              dsOut.setVersion(clsJSON::mscintQtVersion);
              QJsonObject objMsg;
              objMsg.insert(clsJSON::mscszModule, clsMainWnd::mscstrTitle);
              dsOut << QJsonDocument(objMsg).toJson(QJsonDocument::Compact);
              connect(pClient, &QAbstractSocket::disconnected
                     ,pClient, &QObject::deleteLater);
              pClient->sendJSON(objMsg);
          }
          

          Application B looks for a local IP address and connects to it on the same port:

          void clsModHelper::onConnectToXMLMPAM() {
              if ( isOpen() == true ) {
              //Already connected do nothing!
                  return;
              }
              //Get the I/P address
              QList<QHostAddress> lstAddresses = QNetworkInterface::allAddresses();
              QString strIP;
              for( int i=0; i<lstAddresses.size(); i++ ) {
                  if ( lstAddresses[i] != QHostAddress::LocalHost
                    && lstAddresses[i].toIPv4Address() ) {
                      strIP = lstAddresses[i].toString();
                      break;
                  }
              }
              if ( strIP.isEmpty() == true ) {
                  strIP = QHostAddress(QHostAddress::LocalHost).toString();
              }
              //Connect to the Application
              qdbg() << "Connecting to: " << strIP << ":" << muint16XMLMPAMport;
              connectToHost(strIP, muint16XMLMPAMport);
          }
          

          When a connection from B to A is established the signal 'connected' is emitted and I can see a message is logged by the slot in application B 'onConnected':

          void clsModFileIO::onConnected() {
              qdbg() << "onConnected";
          }
          

          I'm confused as application A shows no sign of the new connection, but if I terminate application A, application B gets the 'disconnected' signal, I can see that because the slot in application B:

          void clsModFileIO::onDisconnected() {
              //Close the exiting connection
              close();
              //Re-open the connection
              connectToXMLMPAM();
          }
          

          I would have expected to see in Application A that the slot that is connected to the 'newConnection' signal gets called, but it does not. I'm confused about what is going on and how I can progress this?

          Application A signal connections:

          connect(this, &QTcpServer::newConnection, this, &clsSocketServer::sayHello);
          

          Application B signal connections:

              QObject::connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit
                              ,this, &clsModHelper::onExitModule);
              QObject::connect(this, SIGNAL(connected()), this, SLOT(onConnected()));
              QObject::connect(this, SIGNAL(connected()), this, SLOT(onSendModuleStartupMsg()));
              QObject::connect(this, SIGNAL(connected()), this, SLOT(onStartHeartbeat()));
              QObject::connect(this, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
              QObject::connect(this, &QAbstractSocket::errorOccurred, this, &clsModHelper::onErrorOccurred);
              QObject::connect(this, &QIODevice::readyRead, this, &clsModHelper::onDataIn);
              QObject::connect(this, SIGNAL(bytesWritten(qint64)), this, SLOT(onBytesWritten(qint64)));        
              QObject::connect(this, &clsModHelper::terminateModule, this, &clsModHelper::onExitModule);
          

          Application A , log:

          D00000000000000000001:XMLMPAM is listening on: 192.168.1.158:8123
          D00000000000000000002:L00000141Fsimon.js[void clsScriptHelper::log]:
          D00000000000000000003:************** setup ****************
          D00000000000000000004:L00000116Fsimon.js[void clsScriptHelper::log]:
          D00000000000000000005:************** connect ****************
          D00000000000000000006:L00000062Fsimon.js[void clsScriptHelper::log]:
          D00000000000000000007:L00000047Fsimon.js[QProcess *clsScriptHelper::pLaunch]:
          D00000000000000000008:Checking for PID for: /Users/simonplatten/XMLMPAM/config/modules/mdFileIO
          D00000000000000000009:Process: /Users/simonplatten/XMLMPAM/config/modules/mdFileIO started, PID: 80462
          D00000000000000000010:L00000100Fsimon.js[void clsScriptHelper::log]:
          D00000000000000000011:************** setupFIO ****************
          

          Application B, log:

          D00000000000000000001:mdFileIO Version: 1.00
          D00000000000000000002:Connecting to: 192.168.1.158:8123
          D00000000000000000003:onConnected
          D00000000000000000004:sendJSON: 54
          D00000000000000000005:clsModHelper::onBytesWritten:54
          D00000000000000000006:sendJSON: 54
          D00000000000000000007:clsModHelper::onBytesWritten:54
          D00000000000000000008:sendJSON: 54
          D00000000000000000009:clsModHelper::onBytesWritten:54
          D00000000000000000010:sendJSON: 54
          D00000000000000000011:clsModHelper::onBytesWritten:54
          D00000000000000000012:sendJSON: 54
          D00000000000000000013:clsModHelper::onBytesWritten:54
          D00000000000000000014:sendJSON: 54
          D00000000000000000015:clsModHelper::onBytesWritten:54
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #3

          @SPlatten said in QTcpServer, listen, is the bind internal?:

          There is a signal called newConnection which I connect to a slot

          I'm confused as application A shows no sign of the new connection,

          I would have expected to see in Application A that the slot that is connected to the 'newConnection' signal gets called, but it does not.

          Among all the connects you show, I see nothing about newConnection, nor sayHello().

          Sigh. Did you edit it or did I just miss it?!

          connect(this, &QTcpServer::newConnection, this, &clsSocketServer::sayHello);
          

          Are you saying the slot does not get called?

          1 Reply Last reply
          0
          • SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by SPlatten
            #4

            Sorry, the newConnection signal connection is in the constructor:

            clsSocketServer::clsSocketServer(QObject* pParent) : QTcpServer(pParent) {
                QString strListenFailure = QString("Cannot listen to port: ") + QString::number(clsSocketServer::mscuint16port);
                listen(QHostAddress::Any, clsSocketServer::mscuint16port);
                QList<QHostAddress> lstAddresses = QNetworkInterface::allAddresses();
                QString strIP;
                for( int i=0; i<lstAddresses.size(); i++ ) {
                    if ( lstAddresses[i] != QHostAddress::LocalHost
                      && lstAddresses[i].toIPv4Address() ) {
                        strIP = lstAddresses[i].toString();
                        break;
                    }
                }
                if ( strIP.isEmpty() == true ) {
                    strIP = QHostAddress(QHostAddress::LocalHost).toString();
                }
                qdbg() << tr("XMLMPAM is listening on: %1:%2\n")
                            .arg(strIP).arg(clsSocketServer::mscuint16port);
                QObject::connect(this, &QTcpServer::newConnection, this, &clsSocketServer::sayHello);
                Q_ASSERT_X(isListening(), "clsSocketServer::clsSocketServer", strListenFailure.toLatin1().data());
            }
            

            No it doesn't look like it is being called, however its clear that a connection is made because when I terminate Application A, application B gets disconnect signals.

            Kind Regards,
            Sy

            JonBJ 1 Reply Last reply
            0
            • SPlattenS SPlatten

              Sorry, the newConnection signal connection is in the constructor:

              clsSocketServer::clsSocketServer(QObject* pParent) : QTcpServer(pParent) {
                  QString strListenFailure = QString("Cannot listen to port: ") + QString::number(clsSocketServer::mscuint16port);
                  listen(QHostAddress::Any, clsSocketServer::mscuint16port);
                  QList<QHostAddress> lstAddresses = QNetworkInterface::allAddresses();
                  QString strIP;
                  for( int i=0; i<lstAddresses.size(); i++ ) {
                      if ( lstAddresses[i] != QHostAddress::LocalHost
                        && lstAddresses[i].toIPv4Address() ) {
                          strIP = lstAddresses[i].toString();
                          break;
                      }
                  }
                  if ( strIP.isEmpty() == true ) {
                      strIP = QHostAddress(QHostAddress::LocalHost).toString();
                  }
                  qdbg() << tr("XMLMPAM is listening on: %1:%2\n")
                              .arg(strIP).arg(clsSocketServer::mscuint16port);
                  QObject::connect(this, &QTcpServer::newConnection, this, &clsSocketServer::sayHello);
                  Q_ASSERT_X(isListening(), "clsSocketServer::clsSocketServer", strListenFailure.toLatin1().data());
              }
              

              No it doesn't look like it is being called, however its clear that a connection is made because when I terminate Application A, application B gets disconnect signals.

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

              @SPlatten

              void clsSocketServer::sayHello() {
                  clsSocketClient* pClient = new clsSocketClient(nextPendingConnection());
              
                  if ( pClient == nullptr ) {
                      return;
                  }
              
              

              Can you please put in a qDebug() as the very first line of this slot. I don't know how the rest of your code behaves, you imply here it might fail....

              SPlattenS 1 Reply Last reply
              0
              • JonBJ JonB

                @SPlatten

                void clsSocketServer::sayHello() {
                    clsSocketClient* pClient = new clsSocketClient(nextPendingConnection());
                
                    if ( pClient == nullptr ) {
                        return;
                    }
                
                

                Can you please put in a qDebug() as the very first line of this slot. I don't know how the rest of your code behaves, you imply here it might fail....

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

                @JonB , Sorry, the 'newConnection' signal is being raised and the slot 'sayHello' does get called, I've just tested it again....I am not receiving the data that application B says it has transmitted and the 'bytesWritten' signal backs this up.

                Kind Regards,
                Sy

                JonBJ 1 Reply Last reply
                0
                • SPlattenS SPlatten

                  @JonB , Sorry, the 'newConnection' signal is being raised and the slot 'sayHello' does get called, I've just tested it again....I am not receiving the data that application B says it has transmitted and the 'bytesWritten' signal backs this up.

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

                  @SPlatten
                  That's a totally different question!

                  You would want to supply a completely different set of code to address this.

                  1 Reply Last reply
                  0
                  • SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #8

                    @JonB , will do.

                    Kind Regards,
                    Sy

                    1 Reply Last reply
                    0

                    • Login

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