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 / QTcpServer, connection closed
Forum Updated to NodeBB v4.3 + New Features

QTcpSocket / QTcpServer, connection closed

Scheduled Pinned Locked Moved Solved General and Desktop
42 Posts 6 Posters 6.8k Views 3 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

    The class derived from QTcpServer:

    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);
        connect(this, &QTcpServer::newConnection, this, &clsSocketServer::sayHello);
        Q_ASSERT_X(isListening(), "clsSocketServer::clsSocketServer", strListenFailure.toLatin1().data());
    }
    /**
     * @brief clsSocketServer::~clsSocketServer - class destructor
     */
    clsSocketServer::~clsSocketServer() {
        close();
    }
    /**
     * @brief clsSocketServer::discardClient
     */
    void clsSocketServer::discardClient() {
        QTcpSocket* pClient = (QTcpSocket*)sender();
        pClient->deleteLater();
    }
    /**
     * @brief clsSocketService::incomingConnection
     * @param sfd : Client Socket Descriptor
     */
    void clsSocketServer::incomingConnection(qintptr sfd) {
        clsSocketClient* pClient = new clsSocketClient(sfd, this);
        pClient->setSocketDescriptor(sfd);
    }
    /**
     * @brief clsSocketServer::sayHello
     */
    void clsSocketServer::sayHello() {
        QTcpSocket* pClient = this->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);
        sendJSON(*pClient, objMsg);
    }
    /**
     * @brief clsSocketServer::sendJSON
     * @param objClient : Reference to client socket
     * @param objJSON : Reference to JSON object to transmit
     */
    void clsSocketServer::sendJSON(QTcpSocket& objClient, QJsonObject& objJSON) {
        if ( objClient.isOpen() != true ) {
            return;
        }
        //Associate this TCP socket with the output data stream
        QByteArray arybytMsg;
        QDataStream dsOut(&arybytMsg, QIODevice::WriteOnly);
        //dsOut.setDevice(this);
        dsOut.setVersion(clsJSON::mscintQtVersion);
        //Send message to data stream
        dsOut << QJsonDocument(objJSON).toJson(QJsonDocument::Compact);
        //Write message
        qint64 int64Written = objClient.write(arybytMsg);
    
        qdbg() << "sendJSON: " << int64Written;
    }
    

    This isn't finished, however once its created which there is only a single instance, it is not destroyed. A client is created by the overloaded function "incomingConnection".

    Presently there is no additional management of connections going on, nothing is cleaning up, closing or disconnecting clients.

    qdbg is just a macro:

    #define qdbg()      qDebug().noquote().nospace()
    
    KroMignonK Offline
    KroMignonK Offline
    KroMignon
    wrote on last edited by
    #9

    @SPlatten Please do not just copy/past code found on internet, try also to read documentation to understand what you are doing.

    You have reimplemented QTcpServer::incomingConnection(), why not. But if you what to also use signal QTcpServer::newConnection(), you have to add the socket into the pending list with QTcpServer::addPendingConnection().

    • https://doc.qt.io/qt-5/qtcpserver.html#incomingConnection
    • https://doc.qt.io/qt-5/qtcpserver.html#addPendingConnection

    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
    2
    • KroMignonK KroMignon

      @SPlatten Please do not just copy/past code found on internet, try also to read documentation to understand what you are doing.

      You have reimplemented QTcpServer::incomingConnection(), why not. But if you what to also use signal QTcpServer::newConnection(), you have to add the socket into the pending list with QTcpServer::addPendingConnection().

      • https://doc.qt.io/qt-5/qtcpserver.html#incomingConnection
      • https://doc.qt.io/qt-5/qtcpserver.html#addPendingConnection
      SPlattenS Offline
      SPlattenS Offline
      SPlatten
      wrote on last edited by
      #10

      @KroMignon , What code found on the internet? Thats all my source.

      Kind Regards,
      Sy

      KroMignonK 1 Reply Last reply
      0
      • SPlattenS SPlatten

        @KroMignon , What code found on the internet? Thats all my source.

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

        @SPlatten said in QTcpSocket / QTcpServer, connection closed:

        Thats all my source.

        Maybe, but it is not clean coded.
        Why did you not call addPendingConnection() in your incomingConnection() implementation as it is recommended 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)

        SPlattenS 2 Replies Last reply
        0
        • KroMignonK KroMignon

          @SPlatten said in QTcpSocket / QTcpServer, connection closed:

          Thats all my source.

          Maybe, but it is not clean coded.
          Why did you not call addPendingConnection() in your incomingConnection() implementation as it is recommended in documentation?

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

          @KroMignon , sorry I clicked the wrong thing, the problem is not solved.

          Kind Regards,
          Sy

          1 Reply Last reply
          0
          • KroMignonK KroMignon

            @SPlatten said in QTcpSocket / QTcpServer, connection closed:

            Thats all my source.

            Maybe, but it is not clean coded.
            Why did you not call addPendingConnection() in your incomingConnection() implementation as it is recommended in documentation?

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

            @KroMignon , I've stopped the main application from creating the process, so I can debug it in two instances of Qt Creator and see whats going on.

            So for the sake of this past A = the original parent application and B is the other application. A uses QTcpServer and B uses QTcpCSocket.

            When A starts up:

            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);
                connect(this, &QTcpServer::newConnection, this, &clsSocketServer::sayHello);
                Q_ASSERT_X(isListening(), "clsSocketServer::clsSocketServer", strListenFailure.toLatin1().data());
            }
            

            The slot connected to newConnection:

            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);
            }
            

            When B starts up:

            clsModHelper::clsModHelper(QObject* pParent, int intArgc, char* parystrArgv[]
                                      ,const char* cpszTitle, double dblVersion)
                        : QTcpSocket(pParent)            
                        , mdblVersion(dblVersion)
                        , mfpDbgLog(nullptr)
                        , mint64AppPID(QCoreApplication::applicationPid())
                        , mstrTitle(cpszTitle)
                        , muint16ModulePort(0), muint16LauncherPID(0)
                        , muint16XMLMPAMport(0) {
                if ( intArgc < CLA_LAUNCHER_PID ) {
                    std::cout << "Insufficient arguments, aborting!" << std::endl;
                    exit(EXIT_FAILURE);
                }
                if ( mspThis == nullptr ) {
                    mspThis = this;
                }
                muint16XMLMPAMport = (quint16)atoi(parystrArgv[CLA_XMLMPAM_PORT]);
                muint16ModulePort = (quint16)atoi(parystrArgv[CLA_MODULE_PORT]);
                muint16LauncherPID = (quint16)atoi(parystrArgv[CLA_LAUNCHER_PID]);
                setSocketOption(QAbstractSocket::LowDelayOption, 1);
                //Connect up the signals
                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);
                qdbg() << state();
                //Install custom message handler
                clsDebugService::serviceDebugQueue(cpszTitle, true, true);
                //Build up title for output to the console
                qdbg() << QString::asprintf("%s Version: %.2lf", cpszTitle, mdblVersion);
                connectToXMLMPAM();
            }
            

            The function connectToXMLMPAM():

            /**
             * @brief clsModHelper::connectToXMLMPAM
             */
            void clsModHelper::connectToXMLMPAM() {
                //Start of one-shot timer to connect to XMLMPAM
                QTimer::singleShot(clsModHelper::mscintModuleCycleFrequency, this, SLOT(onConnectToXMLMPAM()));
            }
            

            If there is anything else I haven't posted, please let me know, I'm not seeing anything received by A, yet B keeps logging that its sent data and there are no errors.

            Kind Regards,
            Sy

            KroMignonK 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @KroMignon , I've stopped the main application from creating the process, so I can debug it in two instances of Qt Creator and see whats going on.

              So for the sake of this past A = the original parent application and B is the other application. A uses QTcpServer and B uses QTcpCSocket.

              When A starts up:

              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);
                  connect(this, &QTcpServer::newConnection, this, &clsSocketServer::sayHello);
                  Q_ASSERT_X(isListening(), "clsSocketServer::clsSocketServer", strListenFailure.toLatin1().data());
              }
              

              The slot connected to newConnection:

              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);
              }
              

              When B starts up:

              clsModHelper::clsModHelper(QObject* pParent, int intArgc, char* parystrArgv[]
                                        ,const char* cpszTitle, double dblVersion)
                          : QTcpSocket(pParent)            
                          , mdblVersion(dblVersion)
                          , mfpDbgLog(nullptr)
                          , mint64AppPID(QCoreApplication::applicationPid())
                          , mstrTitle(cpszTitle)
                          , muint16ModulePort(0), muint16LauncherPID(0)
                          , muint16XMLMPAMport(0) {
                  if ( intArgc < CLA_LAUNCHER_PID ) {
                      std::cout << "Insufficient arguments, aborting!" << std::endl;
                      exit(EXIT_FAILURE);
                  }
                  if ( mspThis == nullptr ) {
                      mspThis = this;
                  }
                  muint16XMLMPAMport = (quint16)atoi(parystrArgv[CLA_XMLMPAM_PORT]);
                  muint16ModulePort = (quint16)atoi(parystrArgv[CLA_MODULE_PORT]);
                  muint16LauncherPID = (quint16)atoi(parystrArgv[CLA_LAUNCHER_PID]);
                  setSocketOption(QAbstractSocket::LowDelayOption, 1);
                  //Connect up the signals
                  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);
                  qdbg() << state();
                  //Install custom message handler
                  clsDebugService::serviceDebugQueue(cpszTitle, true, true);
                  //Build up title for output to the console
                  qdbg() << QString::asprintf("%s Version: %.2lf", cpszTitle, mdblVersion);
                  connectToXMLMPAM();
              }
              

              The function connectToXMLMPAM():

              /**
               * @brief clsModHelper::connectToXMLMPAM
               */
              void clsModHelper::connectToXMLMPAM() {
                  //Start of one-shot timer to connect to XMLMPAM
                  QTimer::singleShot(clsModHelper::mscintModuleCycleFrequency, this, SLOT(onConnectToXMLMPAM()));
              }
              

              If there is anything else I haven't posted, please let me know, I'm not seeing anything received by A, yet B keeps logging that its sent data and there are no errors.

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

              @SPlatten did you remove clsSocketServer::incomingConnection()?

              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
              0
              • KroMignonK KroMignon

                @SPlatten did you remove clsSocketServer::incomingConnection()?

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

                @KroMignon yes

                Kind Regards,
                Sy

                KroMignonK 1 Reply Last reply
                0
                • SPlattenS SPlatten

                  @KroMignon yes

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

                  @SPlatten said in QTcpSocket / QTcpServer, connection closed:

                  yes

                  I am not sure you are understanding what you are doing, and that's bad.

                  The protected virtual function QTcpServer::incomingConnection() is called by QTcpServer on each new connection request.
                  The default implementation of this function will create a QTcpSocket instance with the connection informations and use the QTcpServer as parent and finally add the new instance to the connection queue with addPendingConnection().

                  With this default implementation, all clients will run in same thread as QTcpServer, because QObject with a parent cannot be move to another thread, only the parent QObject.
                  The call addPendingConnection() will also trigger a signal QTcpServer::newConnection().
                  And finally you can retrieve to instance you have add to queue with QTcpServer::nextPendingConnection().

                  This is why QTcpServer::incomingConnection() exists and how it has to be used.

                  So again, please take time to read documentation of the classes you are using.
                  The documentation is not so bad!

                  To summarize: in your first code, you are subclassing incomingConnection() but not add the new connection to pending queue with addPendingConnection() and finally waiting for signal QTcpServer::newConnection() which will never be triggered.

                  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
                  2
                  • SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #17

                    @KroMignon , What I'm struggling to get to grips with is getting the QTcpServer and QTcpSocket to work. I'm using the examples to prod and learn. I have written lots of socket based applications in the past, not Qt.

                    I am an experience programmer, the samples FortuneServer and FortuneClient do not use incomingConnection directly either, they are very lean examples. The examples to not call addPendingConnection either.

                    Initialisation of QTcpServer in FortuneServer:

                        tcpServer = new QTcpServer(this);
                        if (!tcpServer->listen()) {
                            QMessageBox::critical(this, tr("Fortune Server"),
                                                  tr("Unable to start the server: %1.")
                                                  .arg(tcpServer->errorString()));
                            close();
                            return;
                        }
                    //! [0]
                        QString ipAddress;
                        QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
                        // use the first non-localhost IPv4 address
                        for (int i = 0; i < ipAddressesList.size(); ++i) {
                            if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
                                ipAddressesList.at(i).toIPv4Address()) {
                                ipAddress = ipAddressesList.at(i).toString();
                                break;
                            }
                        }
                        // if we did not find one, use IPv4 localhost
                        if (ipAddress.isEmpty())
                            ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
                        statusLabel->setText(tr("The server is running on\n\nIP: %1\nport: %2\n\n"
                                                "Run the Fortune Client example now.")
                                             .arg(ipAddress).arg(tcpServer->serverPort()));
                    

                    The slot that is connected to the newConnection signal:

                    void Server::sendFortune()
                    {
                        QByteArray block;
                        QDataStream out(&block, QIODevice::WriteOnly);
                        out.setVersion(QDataStream::Qt_5_10);
                        out << fortunes[QRandomGenerator::global()->bounded(fortunes.size())];
                    
                        QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
                        qDebug() << "Server::sendFortune clientConnection->isOpen(): " << clientConnection->isOpen();
                        qDebug() << "Server::sendFortune clientConnection->error(): " << clientConnection->error();
                    
                        connect(clientConnection, &QAbstractSocket::disconnected,
                                clientConnection, &QObject::deleteLater);
                        clientConnection->write(block);
                        clientConnection->disconnectFromHost();
                    }
                    

                    The FortuneClient doesn't use addPendingConnection either.

                    In my application newConnection is getting called, I've said this before. Its sending data successfully that I'm now looking at as this isn't happening.

                    Kind Regards,
                    Sy

                    KroMignonK JKSHJ 2 Replies Last reply
                    0
                    • SPlattenS SPlatten

                      @KroMignon , What I'm struggling to get to grips with is getting the QTcpServer and QTcpSocket to work. I'm using the examples to prod and learn. I have written lots of socket based applications in the past, not Qt.

                      I am an experience programmer, the samples FortuneServer and FortuneClient do not use incomingConnection directly either, they are very lean examples. The examples to not call addPendingConnection either.

                      Initialisation of QTcpServer in FortuneServer:

                          tcpServer = new QTcpServer(this);
                          if (!tcpServer->listen()) {
                              QMessageBox::critical(this, tr("Fortune Server"),
                                                    tr("Unable to start the server: %1.")
                                                    .arg(tcpServer->errorString()));
                              close();
                              return;
                          }
                      //! [0]
                          QString ipAddress;
                          QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
                          // use the first non-localhost IPv4 address
                          for (int i = 0; i < ipAddressesList.size(); ++i) {
                              if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
                                  ipAddressesList.at(i).toIPv4Address()) {
                                  ipAddress = ipAddressesList.at(i).toString();
                                  break;
                              }
                          }
                          // if we did not find one, use IPv4 localhost
                          if (ipAddress.isEmpty())
                              ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
                          statusLabel->setText(tr("The server is running on\n\nIP: %1\nport: %2\n\n"
                                                  "Run the Fortune Client example now.")
                                               .arg(ipAddress).arg(tcpServer->serverPort()));
                      

                      The slot that is connected to the newConnection signal:

                      void Server::sendFortune()
                      {
                          QByteArray block;
                          QDataStream out(&block, QIODevice::WriteOnly);
                          out.setVersion(QDataStream::Qt_5_10);
                          out << fortunes[QRandomGenerator::global()->bounded(fortunes.size())];
                      
                          QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
                          qDebug() << "Server::sendFortune clientConnection->isOpen(): " << clientConnection->isOpen();
                          qDebug() << "Server::sendFortune clientConnection->error(): " << clientConnection->error();
                      
                          connect(clientConnection, &QAbstractSocket::disconnected,
                                  clientConnection, &QObject::deleteLater);
                          clientConnection->write(block);
                          clientConnection->disconnectFromHost();
                      }
                      

                      The FortuneClient doesn't use addPendingConnection either.

                      In my application newConnection is getting called, I've said this before. Its sending data successfully that I'm now looking at as this isn't happening.

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

                      @SPlatten said in QTcpSocket / QTcpServer, connection closed:

                      What I'm struggling to get to grips with is getting the QTcpServer and QTcpSocket to work. I'm using the examples to prod and learn. I have written lots of socket based applications in the past, not Qt.
                      I am an experience programmer, the samples FortuneServer and FortuneClient do not use incomingConnection directly either, they are very lean examples. The examples to not call addPendingConnection either.

                      I don't want to hurt you, explain by writing in a foreign language is not so easy, maybe my words are not everytime appropriated.
                      I also had many problems to start with Qt, even if I was developing embedded software more than 20 years before in C/C++/Assembler.
                      There is a philosophy to understand and accept.
                      I am trying to follow this simple rule "If you are fight against the framework, than you are doing something wrong!".

                      I know there are many examples available, but most of them are outdated, but as Qt is open source, you can always take a look behind the scene and look what the code really does:

                      void QTcpServer::incomingConnection(qintptr socketDescriptor)
                      {
                      #if defined (QTCPSERVER_DEBUG)
                          qDebug("QTcpServer::incomingConnection(%i)", socketDescriptor);
                      #endif
                      
                          QTcpSocket *socket = new QTcpSocket(this);
                          socket->setSocketDescriptor(socketDescriptor);
                          addPendingConnection(socket);
                      }
                      

                      My big problem is that I don't really understand what do you want to achieve with your TCP server. I think you have a "bad start". That is normal, s***t happens ;)

                      I would love to help you, but I am a little lost, because I don't really understand your code and I can't understand the final result you want to have.
                      Create a TCP server with Qt is very simple, it takes less than 100 line codes.

                      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
                      0
                      • SPlattenS Offline
                        SPlattenS Offline
                        SPlatten
                        wrote on last edited by SPlatten
                        #19

                        @KroMignon , I'm starting to think the problem is not source code related but another factor. In the example I am looking at FortuneServer there is a fundamental difference. It gets the port to use automatically, where as in my code I am using a specific port 8123.

                        I've got the source to both FortuneServer and my own side by side and FortuneServer is much simpler in that it only implements one signal from QTcpServer "newConnection".

                        When a new client connection in the FortuneServer occurs it calls in the sendFortune slot:

                         QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
                        

                        The difference is that the clientConnection status of isOpen is true, where as in my own slot, it's false.

                        In my slot that is called on a new Connection:

                        void clsSocketServer::sayHello() {
                            clsSocketClient* pClient = new clsSocketClient(nextPendingConnection());
                        
                            if ( pClient != nullptr ) {
                            //Construct the message to send
                                QJsonObject objMsg;
                                objMsg.insert(clsJSON::mscszModule, clsMainWnd::mscstrTitle);
                                pClient->sendJSON(objMsg);
                            }
                        }
                        

                        In the function sendJSON:

                        void clsSocketClient::sendJSON(QJsonObject& objJSON) {
                            //Associate this TCP socket with the output data stream
                            QByteArray arybytMsg;
                            QDataStream dsOut(&arybytMsg, QIODevice::WriteOnly);
                            dsOut.setVersion(clsJSON::mscintQtVersion);
                            //Send message to data stream
                            dsOut << QJsonDocument(objJSON).toJson(QJsonDocument::Compact);
                            //Write message
                            qint64 int64Written = write(arybytMsg);
                            //Make sure the data is written now
                            flush();
                            qdbg() << "sendJSON: " << int64Written;
                        }
                        

                        The call to flush was just to ensure that the written content is not held in a buffer instead of calling disconnect, because I want the connection to remain open. But "int64Written" is -1 and I see in the "Application Output":

                        device not open
                        

                        [Edit] Observing something odd...in my constructor for clsSocketClient I output:

                        qdbg() << __FILE__ << "," << __LINE__ << ",isOpen: " << pClient->isOpen();
                        

                        This results in:

                        D00000000000000000020:../clsSocketClient.cpp,28,isOpen: true
                        

                        Ignore the D number thats just my own message prefix, 28 is the line number, the at this point the socket is open. Then when it returns from the constructor and calls the function sendJSON:

                        void clsSocketClient::sendJSON(QJsonObject& objJSON) {
                            //Associate this TCP socket with the output data stream
                            QByteArray arybytMsg;
                            QDataStream dsOut(&arybytMsg, QIODevice::WriteOnly);
                            dsOut.setVersion(clsJSON::mscintQtVersion);
                            //Send message to data stream
                            dsOut << QJsonDocument(objJSON).toJson(QJsonDocument::Compact);
                            //Write message
                            qint64 int64Written = write(arybytMsg);
                            //Make sure the data is written now
                            flush();
                            //disconnectFromHost();           //Is this required?
                            qdbg() << "sendJSON: " << int64Written;
                        }
                        

                        The "Application Output" shows:

                        W00000000000000000022:QIODevice::write (clsSocketClient): device not open
                        D00000000000000000023:sendJSON: -1
                        

                        Kind Regards,
                        Sy

                        KroMignonK 1 Reply Last reply
                        0
                        • SPlattenS SPlatten

                          @KroMignon , I'm starting to think the problem is not source code related but another factor. In the example I am looking at FortuneServer there is a fundamental difference. It gets the port to use automatically, where as in my code I am using a specific port 8123.

                          I've got the source to both FortuneServer and my own side by side and FortuneServer is much simpler in that it only implements one signal from QTcpServer "newConnection".

                          When a new client connection in the FortuneServer occurs it calls in the sendFortune slot:

                           QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
                          

                          The difference is that the clientConnection status of isOpen is true, where as in my own slot, it's false.

                          In my slot that is called on a new Connection:

                          void clsSocketServer::sayHello() {
                              clsSocketClient* pClient = new clsSocketClient(nextPendingConnection());
                          
                              if ( pClient != nullptr ) {
                              //Construct the message to send
                                  QJsonObject objMsg;
                                  objMsg.insert(clsJSON::mscszModule, clsMainWnd::mscstrTitle);
                                  pClient->sendJSON(objMsg);
                              }
                          }
                          

                          In the function sendJSON:

                          void clsSocketClient::sendJSON(QJsonObject& objJSON) {
                              //Associate this TCP socket with the output data stream
                              QByteArray arybytMsg;
                              QDataStream dsOut(&arybytMsg, QIODevice::WriteOnly);
                              dsOut.setVersion(clsJSON::mscintQtVersion);
                              //Send message to data stream
                              dsOut << QJsonDocument(objJSON).toJson(QJsonDocument::Compact);
                              //Write message
                              qint64 int64Written = write(arybytMsg);
                              //Make sure the data is written now
                              flush();
                              qdbg() << "sendJSON: " << int64Written;
                          }
                          

                          The call to flush was just to ensure that the written content is not held in a buffer instead of calling disconnect, because I want the connection to remain open. But "int64Written" is -1 and I see in the "Application Output":

                          device not open
                          

                          [Edit] Observing something odd...in my constructor for clsSocketClient I output:

                          qdbg() << __FILE__ << "," << __LINE__ << ",isOpen: " << pClient->isOpen();
                          

                          This results in:

                          D00000000000000000020:../clsSocketClient.cpp,28,isOpen: true
                          

                          Ignore the D number thats just my own message prefix, 28 is the line number, the at this point the socket is open. Then when it returns from the constructor and calls the function sendJSON:

                          void clsSocketClient::sendJSON(QJsonObject& objJSON) {
                              //Associate this TCP socket with the output data stream
                              QByteArray arybytMsg;
                              QDataStream dsOut(&arybytMsg, QIODevice::WriteOnly);
                              dsOut.setVersion(clsJSON::mscintQtVersion);
                              //Send message to data stream
                              dsOut << QJsonDocument(objJSON).toJson(QJsonDocument::Compact);
                              //Write message
                              qint64 int64Written = write(arybytMsg);
                              //Make sure the data is written now
                              flush();
                              //disconnectFromHost();           //Is this required?
                              qdbg() << "sendJSON: " << int64Written;
                          }
                          

                          The "Application Output" shows:

                          W00000000000000000022:QIODevice::write (clsSocketClient): device not open
                          D00000000000000000023:sendJSON: -1
                          
                          KroMignonK Offline
                          KroMignonK Offline
                          KroMignon
                          wrote on last edited by KroMignon
                          #20

                          @SPlatten No this cannot work.

                          As far as I can understand your code clsSocketClient is as subclass of QTcpSocket.
                          So simple C++ error: auto pClient = new clsSocketClient(nextPendingConnection()); is calling QTcpSocket copy constructor is using the QTcpSocket as parent.

                          One solution would be to use incomingConnection to create the instance and save it in the queue:

                          /**
                           * @brief clsSocketService::incomingConnection
                           * @param sfd : Client Socket Descriptor
                           */
                          void clsSocketServer::incomingConnection(qintptr sfd) {
                              auto pClient = new clsSocketClient(this);
                              pClient->setSocketDescriptor(sfd);
                              addPendingConnection(pClient);
                          }
                          

                          And then:

                          void clsSocketServer::sayHello() {
                              auto pClient = qobject_cast<clsSocketClient*>(nextPendingConnection());
                          
                              if ( pClient != nullptr ) {
                              //Construct the message to send
                                  QJsonObject objMsg;
                                  objMsg.insert(clsJSON::mscszModule, clsMainWnd::mscstrTitle);
                                  pClient->sendJSON(objMsg);
                              }
                          }
                          

                          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
                          2
                          • KroMignonK KroMignon

                            @SPlatten No this cannot work.

                            As far as I can understand your code clsSocketClient is as subclass of QTcpSocket.
                            So simple C++ error: auto pClient = new clsSocketClient(nextPendingConnection()); is calling QTcpSocket copy constructor is using the QTcpSocket as parent.

                            One solution would be to use incomingConnection to create the instance and save it in the queue:

                            /**
                             * @brief clsSocketService::incomingConnection
                             * @param sfd : Client Socket Descriptor
                             */
                            void clsSocketServer::incomingConnection(qintptr sfd) {
                                auto pClient = new clsSocketClient(this);
                                pClient->setSocketDescriptor(sfd);
                                addPendingConnection(pClient);
                            }
                            

                            And then:

                            void clsSocketServer::sayHello() {
                                auto pClient = qobject_cast<clsSocketClient*>(nextPendingConnection());
                            
                                if ( pClient != nullptr ) {
                                //Construct the message to send
                                    QJsonObject objMsg;
                                    objMsg.insert(clsJSON::mscszModule, clsMainWnd::mscstrTitle);
                                    pClient->sendJSON(objMsg);
                                }
                            }
                            
                            SPlattenS Offline
                            SPlattenS Offline
                            SPlatten
                            wrote on last edited by
                            #21

                            @KroMignon, thank you, for some reason I'm having some problems with the line:

                            clsSocketClient* pClient = new clsSocketClient(this);
                            

                            no matching constructor for initialization of 'clsSocketClient', the header is present, I've even tried changing the constructor to expect QAbstractSocket*, still the same.

                            Kind Regards,
                            Sy

                            jsulmJ KroMignonK 2 Replies Last reply
                            0
                            • SPlattenS SPlatten

                              @KroMignon, thank you, for some reason I'm having some problems with the line:

                              clsSocketClient* pClient = new clsSocketClient(this);
                              

                              no matching constructor for initialization of 'clsSocketClient', the header is present, I've even tried changing the constructor to expect QAbstractSocket*, still the same.

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by jsulm
                              #22

                              @SPlatten said in QTcpSocket / QTcpServer, connection closed:

                              clsSocketClient

                              How does its constructors look like?

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0
                              • SPlattenS SPlatten

                                @KroMignon, thank you, for some reason I'm having some problems with the line:

                                clsSocketClient* pClient = new clsSocketClient(this);
                                

                                no matching constructor for initialization of 'clsSocketClient', the header is present, I've even tried changing the constructor to expect QAbstractSocket*, still the same.

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

                                @SPlatten said in QTcpSocket / QTcpServer, connection closed:

                                no matching constructor for initialization of 'clsSocketClient', the header is present, I've even tried changing the constructor to expect QAbstractSocket*, still the same.

                                I don't know what clsSocketClient is.
                                I supposed it was:

                                class clsSocketClient : public QTcpSocket
                                {
                                    Q_OBJECT
                                ....
                                };
                                

                                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
                                0
                                • KroMignonK KroMignon

                                  @SPlatten said in QTcpSocket / QTcpServer, connection closed:

                                  no matching constructor for initialization of 'clsSocketClient', the header is present, I've even tried changing the constructor to expect QAbstractSocket*, still the same.

                                  I don't know what clsSocketClient is.
                                  I supposed it was:

                                  class clsSocketClient : public QTcpSocket
                                  {
                                      Q_OBJECT
                                  ....
                                  };
                                  
                                  SPlattenS Offline
                                  SPlattenS Offline
                                  SPlatten
                                  wrote on last edited by
                                  #24

                                  @KroMignon , clsSocketClient:

                                      class clsSocketClient : public QTcpSocket {
                                      Q_OBJECT
                                  
                                      public:
                                          explicit clsSocketClient(QTcpSocket* pClient = nullptr);
                                     ...
                                  

                                  Implementation:

                                  clsSocketClient::clsSocketClient(QTcpSocket* pClient) : QTcpSocket(pClient) {
                                      QObject::connect(this, &QTcpSocket::connected,     this, &clsSocketClient::onConnected);
                                      QObject::connect(this, &QTcpSocket::bytesWritten,  this, &clsSocketClient::onBytesWritten);
                                      QObject::connect(this, &QTcpSocket::disconnected,  this, &QObject::deleteLater);
                                      QObject::connect(this, &QTcpSocket::disconnected,  this, &clsSocketClient::onDisconnected);
                                      QObject::connect(this, &QTcpSocket::errorOccurred, this, &clsSocketClient::onErrorOccurred);
                                      QObject::connect(this, &QTcpSocket::readyRead,     this, &clsSocketClient::onDataIn);
                                      QObject::connect(this, &QTcpSocket::stateChanged,  this, &clsSocketClient::onStateChanged);
                                  }
                                  

                                  Kind Regards,
                                  Sy

                                  jsulmJ KroMignonK 2 Replies Last reply
                                  0
                                  • SPlattenS SPlatten

                                    @KroMignon , clsSocketClient:

                                        class clsSocketClient : public QTcpSocket {
                                        Q_OBJECT
                                    
                                        public:
                                            explicit clsSocketClient(QTcpSocket* pClient = nullptr);
                                       ...
                                    

                                    Implementation:

                                    clsSocketClient::clsSocketClient(QTcpSocket* pClient) : QTcpSocket(pClient) {
                                        QObject::connect(this, &QTcpSocket::connected,     this, &clsSocketClient::onConnected);
                                        QObject::connect(this, &QTcpSocket::bytesWritten,  this, &clsSocketClient::onBytesWritten);
                                        QObject::connect(this, &QTcpSocket::disconnected,  this, &QObject::deleteLater);
                                        QObject::connect(this, &QTcpSocket::disconnected,  this, &clsSocketClient::onDisconnected);
                                        QObject::connect(this, &QTcpSocket::errorOccurred, this, &clsSocketClient::onErrorOccurred);
                                        QObject::connect(this, &QTcpSocket::readyRead,     this, &clsSocketClient::onDataIn);
                                        QObject::connect(this, &QTcpSocket::stateChanged,  this, &clsSocketClient::onStateChanged);
                                    }
                                    
                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #25

                                    @SPlatten said in QTcpSocket / QTcpServer, connection closed:

                                    explicit clsSocketClient(QTcpSocket* pClient = nullptr);

                                    I guess "this" is not QTcpSocket right? So, how can that work?

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                                      @jsulm , this is instance of clsSocketServer which is:

                                          class clsSocketServer : public QTcpServer {
                                          Q_OBJECT
                                         ...
                                      

                                      As I said I tried changing the clsSocketClient to accept QAbstractSocket* but still the same, aren't QTcpServer and QTcpSocket both based on QAbstractSocket ?

                                      Kind Regards,
                                      Sy

                                      jsulmJ 1 Reply Last reply
                                      0
                                      • SPlattenS SPlatten

                                        @jsulm , this is instance of clsSocketServer which is:

                                            class clsSocketServer : public QTcpServer {
                                            Q_OBJECT
                                           ...
                                        

                                        As I said I tried changing the clsSocketClient to accept QAbstractSocket* but still the same, aren't QTcpServer and QTcpSocket both based on QAbstractSocket ?

                                        jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #27

                                        @SPlatten I'm reffering to this:

                                        clsSocketClient* pClient = new clsSocketClient(this);
                                        

                                        What is "this" here? If it is not QTcpSocket then it can't work!

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        SPlattenS 1 Reply Last reply
                                        0
                                        • jsulmJ jsulm

                                          @SPlatten I'm reffering to this:

                                          clsSocketClient* pClient = new clsSocketClient(this);
                                          

                                          What is "this" here? If it is not QTcpSocket then it can't work!

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

                                          @jsulm , I did answer, this is clsSocketServer which is derived from QTcpServer.

                                          Kind Regards,
                                          Sy

                                          jsulmJ 2 Replies 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