[Solved] QTcpServer with console application (Event Loop)
-
wrote on 15 Oct 2011, 16:09 last edited by
Hello everyone
I have a very simple application to use QTcpServer.
@
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);Server * server; server=new Server(); server->listen(QHostAddress::Any,1984); qDebug()<<"Listening ..."; return a.exec();
}
@
Where the server is just a class that inherits the QTcpServer and reimplement the incomingconnection method to handle the clients in separate thread.
the problem:After the first client connects everything is working perfectly, but then the server seems to be stop listening on the local port.
Writing the dtor I've noticed after the first client served the server is removed (I mean the dtor is called)So I declared the (server) obj to be global one, before main, but I had the same problem.
I know the event loop will wait for incoming events including the incomingconnection, but what I can't get why the server is destructed after the first client?thanks
-
wrote on 15 Oct 2011, 16:17 last edited by
Sorry I found the problem
@
connect(thread,SIGNAL(finished()),this,SLOT(deleteLater()));
@
I've connected the finished signal of the thread to deleteLater which will remove the server object after the first client is served. -
wrote on 15 Oct 2011, 16:24 last edited by
Hi Maherall,
Thanks for sharing your solution to the community!
-
wrote on 15 Oct 2011, 16:25 last edited by
I'm sorry for wasting your time but the mistake was the wrong pointer
I had to put it like:@connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater()))@
instead of
@
connect(thread,SIGNAL(finished()),this,SLOT(deleteLater()))@ -
wrote on 15 Oct 2011, 16:36 last edited by
I don't think it's a waste of time. By sharing others can learn from it. Thanks.
-
wrote on 15 Oct 2011, 16:38 last edited by
Thank you Eddy for your comment.
4/6