October 15, 2011

maherali maherali
Lab Rat
38 posts

[Solved] QTcpServer with console application (Event Loop)

 

Hello everyone

I have a very simple application to use QTcpServer.

  1. int main(int argc, char *argv[])
  2. {
  3.     QCoreApplication a(argc, argv);
  4.  
  5.     Server * server;
  6.     server=new Server();
  7.  
  8.     server->listen(QHostAddress::Any,1984);
  9.  
  10.     qDebug()<<"Listening ...";
  11.  
  12.     return a.exec();
  13. }

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

 Signature 

life is just lines of code

5 replies

October 15, 2011

maherali maherali
Lab Rat
38 posts

Sorry I found the problem

  1.  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.

 Signature 

life is just lines of code

October 15, 2011

Eddy Eddy
Area 51 Engineer
1295 posts

Hi Maherall,

Thanks for sharing your solution to the community!

 Signature 

Qt Certified Specialist
Qt Ambassador

October 15, 2011

maherali maherali
Lab Rat
38 posts

I’m sorry for wasting your time but the mistake was the wrong pointer
I had to put it like:

  1. connect(thread,SIGNAL(finished()),thread,SLOT(deleteLater()))

instead of

  1. connect(thread,SIGNAL(finished()),this,SLOT(deleteLater()))

 Signature 

life is just lines of code

October 15, 2011

Eddy Eddy
Area 51 Engineer
1295 posts

I don’t think it’s a waste of time. By sharing others can learn from it. Thanks.

 Signature 

Qt Certified Specialist
Qt Ambassador

October 15, 2011

maherali maherali
Lab Rat
38 posts

Thank you Eddy for your comment.

 Signature 

life is just lines of code

 
  ‹‹ how to find intersection rectitem co ordinates      How i can detect when mouse is pressed and released inside widget area not outside it ? ››

You must log in to post a reply. Not a member yet? Register here!