Important: Please read the Qt Code of Conduct -

QtNetwork: incomingConnection not triggered

  • Hello,

    I am quite new in Qt and trying for hours now to solve an issue on QtNetwork, working with QTcpSocket.

    I try to establish a simple server/client connection for a small game and while my clients successfuly "connect" to the server (telnet works for example), the incomingConnection method is never triggered.

    The main.cpp part starting the server :

    Server *server = new Server(host, port, nb_players);
    std::cout << "Waiting for " << nb_players << " players to join..." << std::endl;
    while (static_cast<int>(server->getClients().size()) < nb_players);
    std::cout << "Perfect, " << nb_players << " joined !" << std::endl;

    Here is my Server.h :

    class Server : public QTcpServer {
        Server(std::string listen_host, int listen_port, int max_clients, QObject *parent = nullptr);
        void start(void);
        void stop(void);
        std::vector<Client*> getClients();
        void incomingConnection(qintptr socketDescriptor) Q_DECL_OVERRIDE;
    private slots:
        void readyRead();
        void disconnected();
        std::map<QTcpSocket*, Client> clients;
        QString listen_host;
        int listen_port;
        int max_clients;

    My Server.cpp :

    Server::Server(std::string listen_host, int listen_port, int max_clients, QObject *parent)
        : QTcpServer(parent)
        this->listen_host = QString::fromUtf8(listen_host.c_str());
        this->listen_port = listen_port;
        this->max_clients = max_clients;
    void Server::start(void)
        QHostAddress address;
        listen(address, (qint16)this->listen_port);
    void Server::stop(void) { close(); }
    void Server::incomingConnection(qintptr socketDescriptor)
        QTcpSocket *socket_client = new QTcpSocket(this);
        if (static_cast<int>(this->clients.size()) >= max_clients) {
            qDebug() << "No additional client accepted";
        Client client = Client(socket_client);
        this->clients.insert(std::pair<QTcpSocket*, Client>(socket_client, client));
        connect(socket_client, SIGNAL(readyRead()), this, SLOT(readyRead()));
        connect(socket_client, SIGNAL(disconnected()), this, SLOT(disconnected()));
    void Server::readyRead()
        QTcpSocket *socket_client = (QTcpSocket*)sender();
        Client client = clients.find(socket_client)->second;
        // other stuff
    void Server::disconnected()
        QTcpSocket *socket_client = (QTcpSocket*)sender();
        Client client = clients.find(socket_client)->second;
        // other stuff
    std::vector<Client*> Server::getClients()
        std::vector<Client*> ptr_clients;
        std::map<QTcpSocket*, Client>::iterator it;
        for ( it = this->clients.begin(); it != this->clients.end(); it++ )
        return ptr_clients;

    Could you provide me with any help for debugging this problem ?

    Thank you.

  • I think your while statement in main.cpp blocks the main thread.
    If you want do it in the blocking way, try QTcpServer::waitForNewConnection.
    (BTW, do you have QCoreApplication / QApplication in the main.cpp ?)
    But it is recommended to use the non-blocking / asynchronous way: signals.
    Also I don't see the necessity for you to reimplement incomingConnection.
    You can just do the Client creating from nextPendingConnection() as the normal way.

  • Lifetime Qt Champion

    @Bonnie said in QtNetwork: incomingConnection not triggered:

    I think

    Why only think? That's the reason.

    @calmstack don't block the event loop then all is working fine.

  • @Bonnie Thank you very much for the precisions, that was it.

    Yes I have a QCoreApplication.

    Solving my problem using :

    while ((int)server->getClients().size() < nb_players)

  • Lifetime Qt Champion

    @calmstack said in QtNetwork: incomingConnection not triggered:

    while ((int)server->getClients().size() < nb_players)

    There is no need for this at all if you use Qt in the way it is supposed to be used.
    Qt is asynchronous. Just connect a slot to

  • @jsulm Yes I've finished by choosing the QThread way.

  • Lifetime Qt Champion

    @calmstack said in QtNetwork: incomingConnection not triggered:


    Why QThread?! As I said: Qt is asynchronous.

  • Lifetime Qt Champion

Log in to reply