I am interested in how QTcpServer works behind the scenes regarding threads and blocking. QTcpServer has a listen() method which returns immediately. If listening started successfully the server will emit the signal newConnection(). I'm interested into how is the server listening (is it on the main thread) when the listen() method has returned. The usual example of a console application with a QTcpServer is something like this:
//main.cpp
int main(int argc, char* argv[])
{
QCoreApplication app;
MyServer server;
app.exec();
}
//MyServer.cpp
MyServer::MyServer(QObject *parent) : QObject(parent)
{
this->server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(on_newConnection()));
if (!server->listen(QHostAddress::Any, 1234))
//do something in case of error
}
void MyServer::on_newConnection()
{
QTcpSocket* socket = server->nextPendingConnection();
//do some communication...
}
Is QTcpServer dependent upon a QCoreApplication (or maybe a QRunLoop) existing and running to receive network events. Can it work properly without a QCoreApplication::exec() being called?