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.
  • C Offline
    C Offline
    calmstack
    wrote on 19 Nov 2020, 00:43 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 19 Nov 2020, 00:56 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.

      C C 2 Replies Last reply 19 Nov 2020, 06:13
      4
      • B Bonnie
        19 Nov 2020, 00:56

        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.

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 19 Nov 2020, 06:13 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
          19 Nov 2020, 00:56

          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.

          C Offline
          C Offline
          calmstack
          wrote on 19 Nov 2020, 22:41 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 20 Nov 2020, 05:57
          0
          • C calmstack
            19 Nov 2020, 22:41

            @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 20 Nov 2020, 05:57 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

            C 1 Reply Last reply 20 Nov 2020, 13:35
            2
            • jsulmJ jsulm
              20 Nov 2020, 05:57

              @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

              C Offline
              C Offline
              calmstack
              wrote on 20 Nov 2020, 13:35 last edited by
              #6

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

              jsulmJ 1 Reply Last reply 20 Nov 2020, 13:39
              0
              • C calmstack
                20 Nov 2020, 13:35

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

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on 20 Nov 2020, 13:39 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

                C 1 Reply Last reply 20 Nov 2020, 17:13
                2
                • jsulmJ jsulm
                  20 Nov 2020, 13:39

                  @calmstack said in QtNetwork: incomingConnection not triggered:

                  QThread

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

                  C Offline
                  C Offline
                  calmstack
                  wrote on 20 Nov 2020, 17:13 last edited by
                  #8

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

                  SGaistS 1 Reply Last reply 20 Nov 2020, 18:24
                  0
                  • C calmstack
                    20 Nov 2020, 17:13

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

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 20 Nov 2020, 18:24 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

                    1/9

                    19 Nov 2020, 00:43

                    • Login

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