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 7.0k 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 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?

          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?

                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!

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

                        @jsulm ,@KroMignon , sorted, it's now working and communicating, thank you:

                        void clsSocketServer::incomingConnection(qintptr sfd) {
                            clsSocketClient* pClient = new clsSocketClient(new QTcpSocket());
                            pClient->setSocketDescriptor(sfd);
                            addPendingConnection(pClient);
                        }
                        

                        Kind Regards,
                        Sy

                        KroMignonK 1 Reply 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);
                          }
                          
                          KroMignonK Offline
                          KroMignonK Offline
                          KroMignon
                          wrote on last edited by
                          #30

                          @SPlatten said in QTcpSocket / QTcpServer, connection closed:

                          class clsSocketClient : public QTcpSocket {
                          Q_OBJECT

                          public:
                              explicit clsSocketClient(QTcpSocket* pClient = nullptr);
                          

                          ...

                          Please change this to:

                          class clsSocketClient : public QTcpSocket 
                          {
                              Q_OBJECT
                          public:
                              explicit clsSocketClient(QObject *parent = nullptr);
                          
                          

                          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 SPlatten

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

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #31
                            This post is deleted!
                            1 Reply Last reply
                            0
                            • SPlattenS SPlatten

                              @jsulm ,@KroMignon , sorted, it's now working and communicating, thank you:

                              void clsSocketServer::incomingConnection(qintptr sfd) {
                                  clsSocketClient* pClient = new clsSocketClient(new QTcpSocket());
                                  pClient->setSocketDescriptor(sfd);
                                  addPendingConnection(pClient);
                              }
                              
                              KroMignonK Offline
                              KroMignonK Offline
                              KroMignon
                              wrote on last edited by
                              #32

                              @SPlatten said in QTcpSocket / QTcpServer, connection closed:

                              void clsSocketServer::incomingConnection(qintptr sfd) {
                              clsSocketClient* pClient = new clsSocketClient(new QTcpSocket());
                              pClient->setSocketDescriptor(sfd);
                              addPendingConnection(pClient);
                              }

                              Why to you do this?
                              clsSocketClient* pClient = new clsSocketClient(new QTcpSocket()); is an nonsense!

                              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
                              1
                              • SPlattenS SPlatten

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

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

                                @SPlatten said in QTcpSocket / QTcpServer, connection closed:

                                which is derived from QTcpServer

                                QTcpServer != QTcpSocket

                                1 Reply Last reply
                                0
                                • KroMignonK KroMignon

                                  @SPlatten said in QTcpSocket / QTcpServer, connection closed:

                                  void clsSocketServer::incomingConnection(qintptr sfd) {
                                  clsSocketClient* pClient = new clsSocketClient(new QTcpSocket());
                                  pClient->setSocketDescriptor(sfd);
                                  addPendingConnection(pClient);
                                  }

                                  Why to you do this?
                                  clsSocketClient* pClient = new clsSocketClient(new QTcpSocket()); is an nonsense!

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

                                  @KroMignon It compiles and works, I tried several other things that don't.

                                  Kind Regards,
                                  Sy

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • SPlattenS SPlatten

                                    @KroMignon It compiles and works, I tried several other things that don't.

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

                                    @SPlatten said in QTcpSocket / QTcpServer, connection closed:

                                    It compiles and works

                                    Doesn't change the fact that it is nonsense.
                                    Do it the way @KroMignon shown you.

                                    SPlattenS 1 Reply Last reply
                                    0
                                    • jsulmJ jsulm

                                      @SPlatten said in QTcpSocket / QTcpServer, connection closed:

                                      It compiles and works

                                      Doesn't change the fact that it is nonsense.
                                      Do it the way @KroMignon shown you.

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

                                      @jsulm , @KroMignon , changed and compiled, still working :)

                                      Kind Regards,
                                      Sy

                                      KroMignonK 1 Reply Last reply
                                      0
                                      • SPlattenS SPlatten

                                        @jsulm , @KroMignon , changed and compiled, still working :)

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

                                        @SPlatten said in QTcpSocket / QTcpServer, connection closed:

                                        changed and compiled, still working :)

                                        It is really disappointing that you don't try to understand what you are doing wrong.
                                        This give the bad taste that you are just placing code together but not have a clue what you are doing.

                                        The class constructor QTcpSocket(QObject *parent) is used to define the parent object to this instance.
                                        Parent is used, in Qt, to handle automatic instance cleanup when parent instance is destroyed.
                                        This simplify also threading, moving parent instance to a thread will also moves all his "childrens".

                                        • https://doc.qt.io/qt-5/object.html
                                        • https://doc.qt.io/qt-5/objecttrees.html

                                        Please, please, read a little bit of Qt documentation to understand what you are doing.

                                        By doing clsSocketClient* pClient = new clsSocketClient(new QTcpSocket()); you a creating a class which will have a parent QObject, but for what?

                                        And the QTcpSocket instance you have created will never be deleted!!!

                                        This not clean programming!

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

                                          JKSHJ Offline
                                          JKSHJ Offline
                                          JKSH
                                          Moderators
                                          wrote on last edited by JKSH
                                          #38

                                          @SPlatten said in QTcpSocket / QTcpServer, connection closed:

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

                                          The important difference here is that the FortuneServer example does not subclass QTcpServer or QTcpSocket. When you subclass it and reimplement functions, there are more gotchas to watch out for.

                                          Your program looks quite complex; I suggest you start by implementing a simple "lean" program from scratch first (without subclassing) and make sure that works. After that, you can gradually add the extra features that you want.

                                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                          SPlattenS 1 Reply Last reply
                                          2

                                          • Login

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