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. QtNetwork: incomingConnection not triggered
QtWS25 Last Chance

QtNetwork: incomingConnection not triggered

Scheduled Pinned Locked Moved Solved General and Desktop
qtnetworksocket
9 Posts 5 Posters 1.2k Views
  • 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.
  • calmstackC Offline
    calmstackC Offline
    calmstack
    wrote on last edited by calmstack
    #1

    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);
    server->start();
    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 {
        Q_OBJECT
    
    public:
        Server(std::string listen_host, int listen_port, int max_clients, QObject *parent = nullptr);
        void start(void);
        void stop(void);
        std::vector<Client*> getClients();
    
    protected:
        void incomingConnection(qintptr socketDescriptor) Q_DECL_OVERRIDE;
    
    private slots:
        void readyRead();
        void disconnected();
    
    private:
        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;
        address.setAddress(this->listen_host);
        listen(address, (qint16)this->listen_port);
    }
    
    void Server::stop(void) { close(); }
    
    void Server::incomingConnection(qintptr socketDescriptor)
    {
        QTcpSocket *socket_client = new QTcpSocket(this);
        socket_client->setSocketDescriptor(socketDescriptor);
    
        if (static_cast<int>(this->clients.size()) >= max_clients) {
            socket_client->disconnect();
            qDebug() << "No additional client accepted";
            return;
        }
    
        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
        clients.erase(socket_client);
    }
    
    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++ )
            ptr_clients.push_back(&it->second);
        return ptr_clients;
    }
    

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

    Thank you.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      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.

      Christian EhrlicherC calmstackC 2 Replies Last reply
      4
      • B Bonnie

        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.

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0
        • B Bonnie

          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.

          calmstackC Offline
          calmstackC Offline
          calmstack
          wrote on last edited by
          #4

          @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)
                  server->waitForNewConnection();
          
          jsulmJ 1 Reply Last reply
          0
          • calmstackC calmstack

            @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)
                    server->waitForNewConnection();
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @calmstack said in QtNetwork: incomingConnection not triggered:

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

            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 https://doc.qt.io/qt-5/qtcpserver.html#newConnection

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

            calmstackC 1 Reply Last reply
            2
            • jsulmJ jsulm

              @calmstack said in QtNetwork: incomingConnection not triggered:

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

              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 https://doc.qt.io/qt-5/qtcpserver.html#newConnection

              calmstackC Offline
              calmstackC Offline
              calmstack
              wrote on last edited by
              #6

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

              jsulmJ 1 Reply Last reply
              0
              • calmstackC calmstack

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

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

                @calmstack said in QtNetwork: incomingConnection not triggered:

                QThread

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

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

                calmstackC 1 Reply Last reply
                2
                • jsulmJ jsulm

                  @calmstack said in QtNetwork: incomingConnection not triggered:

                  QThread

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

                  calmstackC Offline
                  calmstackC Offline
                  calmstack
                  wrote on last edited by
                  #8

                  @jsulm Isn't it what the official documentation recommends ?

                  SGaistS 1 Reply Last reply
                  0
                  • calmstackC calmstack

                    @jsulm Isn't it what the official documentation recommends ?

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    @calmstack said in QtNetwork: incomingConnection not triggered:

                    @jsulm Isn't it what the official documentation recommends ?

                    No, it's one usage example. You have the same example without threads.

                    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

                    • Login

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