Solved QTcpServer newConnection signal not emitted when QMainWindow is instantiated
-
-
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
byQHostAddress::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 127.0.0.1 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?
-
@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? -
@izkb No, QMainWindow does not create any threads- why should it? Even if it would why should those threads break QTcpServer?
-
@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); QTimer::singleShot(0,this,[=]{ server = new QTcpServer(this); if(!server->listen()){ qDebug() << "Unable to open Server"; }else qDebug() << "Server Read for Connection"; });
The QTimer of 0ms means will be executed next eventloop cycle.
-
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:256000000Removing 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.
-
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.