Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTcpServer newConnection signal not emitted when QMainWindow is instantiated
Qt 6.11 is out! See what's new in the release blog

QTcpServer newConnection signal not emitted when QMainWindow is instantiated

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 4 Posters 23.7k Views 2 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.
  • I izkb

    @jsulm Yes, the client connects successfully, but the server does not register this.
    Connecting to localhost on port 2021 with telnet does however not succeed, probably because the server does not send a connection reply

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

    @izkb Are you sure your client connects successfully? How do you know?

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

    I 1 Reply Last reply
    0
    • I izkb

      @jsulm Yes, the client connects successfully, but the server does not register this.
      Connecting to localhost on port 2021 with telnet does however not succeed, probably because the server does not send a connection reply

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #7

      @izkb stupid question, but one easily forgotten, is your server-program blocked by your firewall?


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • jsulmJ jsulm

        @izkb Are you sure your client connects successfully? How do you know?

        I Offline
        I Offline
        izkb
        wrote on last edited by
        #8

        @jsulm The connected() signal is emitted by the client upon connection. Here is a broken-down example of the client code which is built and run in a different project:

        //.h
        class tcp_client_class : public QObject
        {
            Q_OBJECT
        public:
           tcp_client_class();
        private:
            QTcpSocket tcpClientSocket;
        private slots:
            void slotRxReady(void);
            void slotHandleTcpError(const QAbstractSocket::SocketError socketError);
            void slotTcpConnectSuccess(void);
        }
        
        //.cpp
        tcp_client_class::tcp_client_class(): QObject()
        {
           tcpClientSocket.setSocketOption(QAbstractSocket::LowDelayOption,1);
           connect(&tcpClientSocket, SIGNAL(connected()), this, SLOT(slotTcpConnectSuccess()));
           connect(&tcpClientSocket, SIGNAL(readyRead()), this, SLOT(slotRxReady()));
           connect(&tcpClientSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slotHandleTcpError(const QAbstractSocket::SocketError)));
        
        tcpClientSocket.connectToHost("127.0.0.1", 2021);
        }
        
        void tcp_client_class::slotTcpConnectSuccess()
        {
           emit sigSocketConnected();
           qDebug() << "TCP Client Interface: TCP Socket connection success";
        }
        
        jsulmJ 1 Reply Last reply
        0
        • I izkb

          @jsulm The connected() signal is emitted by the client upon connection. Here is a broken-down example of the client code which is built and run in a different project:

          //.h
          class tcp_client_class : public QObject
          {
              Q_OBJECT
          public:
             tcp_client_class();
          private:
              QTcpSocket tcpClientSocket;
          private slots:
              void slotRxReady(void);
              void slotHandleTcpError(const QAbstractSocket::SocketError socketError);
              void slotTcpConnectSuccess(void);
          }
          
          //.cpp
          tcp_client_class::tcp_client_class(): QObject()
          {
             tcpClientSocket.setSocketOption(QAbstractSocket::LowDelayOption,1);
             connect(&tcpClientSocket, SIGNAL(connected()), this, SLOT(slotTcpConnectSuccess()));
             connect(&tcpClientSocket, SIGNAL(readyRead()), this, SLOT(slotRxReady()));
             connect(&tcpClientSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(slotHandleTcpError(const QAbstractSocket::SocketError)));
          
          tcpClientSocket.connectToHost("127.0.0.1", 2021);
          }
          
          void tcp_client_class::slotTcpConnectSuccess()
          {
             emit sigSocketConnected();
             qDebug() << "TCP Client Interface: TCP Socket connection success";
          }
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #9

          @izkb Last idea I have: do you see the debug message "Server listening on port"? Maybe something else is listening on this port? Is another instance of your server app still running?

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

          I 1 Reply Last reply
          0
          • jsulmJ jsulm

            @izkb Last idea I have: do you see the debug message "Server listening on port"? Maybe something else is listening on this port? Is another instance of your server app still running?

            I Offline
            I Offline
            izkb
            wrote on last edited by
            #10

            @J-Hilk Turning off the firewall and antivirus does not solve the problem and the client still connects

            @jsulm No nothing else is listening on this port. I checked in the Windows Resource monitor and closing and re-opening Qt also does not help

            J.HilkJ jsulmJ 2 Replies Last reply
            0
            • I izkb

              @J-Hilk Turning off the firewall and antivirus does not solve the problem and the client still connects

              @jsulm No nothing else is listening on this port. I checked in the Windows Resource monitor and closing and re-opening Qt also does not help

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #11

              @izkb mmh, can you connect to ther fortune-server example with your client?


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              I 1 Reply Last reply
              0
              • I izkb

                @J-Hilk Turning off the firewall and antivirus does not solve the problem and the client still connects

                @jsulm No nothing else is listening on this port. I checked in the Windows Resource monitor and closing and re-opening Qt also does not help

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

                @izkb And do you see "Server listening on port"?

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

                1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @izkb mmh, can you connect to ther fortune-server example with your client?

                  I Offline
                  I Offline
                  izkb
                  wrote on last edited by
                  #13

                  @J.Hilk Yes I can connect to the fortuneserver example

                  @jsulm Yes that is displayed

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #14

                    Hi and welcome to devnet,

                    From what I can see, you try to bind to localhost while listening on any of the network interfaces. You should replace QHostAddress::Any by QHostAddress::LocalHost.

                    In any case, a call to QTcpServer::serverAddress should give you a clue about which address to use to connect to sour server object.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    3
                    • I Offline
                      I Offline
                      izkb
                      wrote on last edited by izkb
                      #15

                      Hi @SGaist
                      Replacing QHostAddress::Any with QHostAddress::LocalHost and printing server->serverAddress displays 127.0.0.1 which is the address to which the client is connecting and as mentioned the client connects successfully, the problem is on the server side which does not register a new connection.

                      The problem must be on the eventloop and possibly results from the server being contained in a QMainWindow object, since the FortuneServer example derives a QDialog class and works on my system. Are there any known issues of this sort with QMainWindow?

                      jsulmJ 1 Reply Last reply
                      0
                      • I izkb

                        Hi @SGaist
                        Replacing QHostAddress::Any with QHostAddress::LocalHost and printing server->serverAddress displays 127.0.0.1 which is the address to which the client is connecting and as mentioned the client connects successfully, the problem is on the server side which does not register a new connection.

                        The problem must be on the eventloop and possibly results from the server being contained in a QMainWindow object, since the FortuneServer example derives a QDialog class and works on my system. Are there any known issues of this sort with QMainWindow?

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

                        @izkb I'm quite sure the issue isn't QMainWindow as it has an event loop as well. It doesn't matter whether it is QMainWindow or QDialog. As long as you don't block the event loop it should work.
                        What else are you doing in your server? Do you by any chance blocking the event loop?

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

                        1 Reply Last reply
                        0
                        • I Offline
                          I Offline
                          izkb
                          wrote on last edited by izkb
                          #17

                          @jsulm
                          The server code provided is the only code I am using at the moment and as far as I know nothing there should block the eventloop...
                          Could it not be that QMainWindow creates separate threads which might break the inner workings of the QTcpServer class?

                          jsulmJ J.HilkJ 2 Replies Last reply
                          0
                          • I izkb

                            @jsulm
                            The server code provided is the only code I am using at the moment and as far as I know nothing there should block the eventloop...
                            Could it not be that QMainWindow creates separate threads which might break the inner workings of the QTcpServer class?

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

                            @izkb No, QMainWindow does not create any threads- why should it? Even if it would why should those threads break QTcpServer?

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

                            1 Reply Last reply
                            0
                            • I izkb

                              @jsulm
                              The server code provided is the only code I am using at the moment and as far as I know nothing there should block the eventloop...
                              Could it not be that QMainWindow creates separate threads which might break the inner workings of the QTcpServer class?

                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #19

                              @izkb If you think, creating the server in the constructor of your mainclass is the issue, why don't you move that away than?

                              #include <QTimer>
                              
                              server = Q_NULLPTR;
                                 connect(server, QTcpServer::newConnection, this, &NetmanSim2_class::slotServerConnected);
                              
                              QTimer::singleShot(0,this,[=]{
                                  server = new QTcpServer(this);
                                  if(!server->listen()){
                                      qDebug() << "Unable to open Server";
                                  }else qDebug() << "Server Read for Connection";
                              });
                              

                              The QTimer of 0ms means will be executed next eventloop cycle.


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              1 Reply Last reply
                              0
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #20

                                One thing that is missing in your code: acceptError handling. That might provide additional information about what is happening.

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                2
                                • I Offline
                                  I Offline
                                  izkb
                                  wrote on last edited by
                                  #21

                                  I found the solution:

                                  Due to a stackoverflow upon initial project setup, I added the following to the .pro file:
                                  QMAKE_LFLAGS += /STACK:256000000

                                  Removing one 0 from the size resulted in the server working correctly.

                                  Why would a large stack size break the eventloop though? I have used the same stack size for other projects without problems.

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #22

                                    Out of curiosity, what triggers the need to modify to such a high value ?

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • I Offline
                                      I Offline
                                      izkb
                                      wrote on last edited by
                                      #23

                                      @SGaist
                                      There was no need, the value was simply copied from a larger project where it was required. Thanks all for the attempts at helping.

                                      1 Reply Last reply
                                      0

                                      • Login

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