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); 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.
-
I think your
while
statement inmain.cpp
blocks the main thread.
If you want do it in the blocking way, tryQTcpServer::waitForNewConnection
.
(BTW, do you have QCoreApplication / QApplication in themain.cpp
?)
But it is recommended to use the non-blocking / asynchronous way: signals.
Also I don't see the necessity for you to reimplementincomingConnection
.
You can just do the Client creating fromnextPendingConnection()
as the normal way. -
@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.
-
@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 -
@calmstack said in QtNetwork: incomingConnection not triggered:
QThread
Why QThread?! As I said: Qt is asynchronous.
-
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.