QTcpServer newConnection signal not emitted when QMainWindow is instantiated

  • Hi. I have a QTcpServer object instantiated inside a derived QMainWindow class.
    After connecting the newConnection() signal to a slot in the QMainWindow class and calling listen(QHostAddress::Any, 2021), the newConnection signal should be emitted once a client connects. This does however not happen - A client using QTcpSocket can connect to the server, but the newConnection signal is not emitted by the server.

    I'm guessing this is a eventloop issue. Even removing the server from the QMainWindow derived class and placing it in main does not work if the QMainWindow derived class is still being instantiated from main as well.
    I would greatly appreciate some help on this topic.
    Please see the attached code below:

    #include "netmansim2_class.h"
    #include <QApplication>
    int main(int argc, char *argv[])
       QApplication a(argc, argv);
       NetmanSim2_class w;
       return a.exec();
    namespace Ui
    class NetmanSim2_class;
    class NetmanSim2_class : public QMainWindow
       explicit NetmanSim2_class(QWidget *parent = 0);
    private slots:
       void slotServerConnected();
       Ui::NetmanSim2_class *ui;
       QTcpServer* server;
    #include "netmansim2_class.h"
    #include "ui_netmansim2_class.h"
    NetmanSim2_class::NetmanSim2_class(QWidget *parent) :
       ui(new Ui::NetmanSim2_class)
       server = new QTcpServer();
       connect(server, SIGNAL(newConnection()), this, SLOT(slotServerConnected()));
       if (server->listen(QHostAddress::Any, 2021))
          qDebug() << "Server listening on port " << 2021;
    void NetmanSim2_class::slotServerConnected()
       qDebug() << "Server connected";
       delete ui;

  • Qt Champions 2018

    @izkb You should make sure connect succeeded:

    qDebug() << connect(server, SIGNAL(newConnection()), this, SLOT(slotServerConnected()));

    it should print true.

  • @jsulm Adding the qDebug() does display true

  • Qt Champions 2018

    @izkb And the client connects on port 2021?

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

  • Qt Champions 2018

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

  • Moderators

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

  • @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:

    class tcp_client_class : public QObject
        QTcpSocket tcpClientSocket;
    private slots:
        void slotRxReady(void);
        void slotHandleTcpError(const QAbstractSocket::SocketError socketError);
        void slotTcpConnectSuccess(void);
    tcp_client_class::tcp_client_class(): QObject()
       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("", 2021);
    void tcp_client_class::slotTcpConnectSuccess()
       emit sigSocketConnected();
       qDebug() << "TCP Client Interface: TCP Socket connection success";

  • Qt Champions 2018

    @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?

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

  • Moderators

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

  • Qt Champions 2018

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

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

    @jsulm Yes that is displayed

  • Lifetime Qt Champion

    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.

  • Hi @SGaist
    Replacing QHostAddress::Any with QHostAddress::LocalHost and printing server->serverAddress displays 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?

  • Qt Champions 2018

    @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?

  • @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?

  • Qt Champions 2018

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

  • Moderators

    @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);
        server = new QTcpServer(this);
            qDebug() << "Unable to open Server";
        }else qDebug() << "Server Read for Connection";

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

  • Lifetime Qt Champion

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

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

  • Lifetime Qt Champion

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

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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.