Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] QTcpServer with console application (Event Loop)

[Solved] QTcpServer with console application (Event Loop)

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 5.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maherali
    wrote on 15 Oct 2011, 16:09 last edited by
    #1

    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&#40;&#41;;
    

    }
    @
    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

    life is just lines of code

    1 Reply Last reply
    0
    • M Offline
      M Offline
      maherali
      wrote on 15 Oct 2011, 16:17 last edited by
      #2

      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.

      life is just lines of code

      1 Reply Last reply
      0
      • E Offline
        E Offline
        Eddy
        wrote on 15 Oct 2011, 16:24 last edited by
        #3

        Hi Maherall,

        Thanks for sharing your solution to the community!

        Qt Certified Specialist
        www.edalsolutions.be

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maherali
          wrote on 15 Oct 2011, 16:25 last edited by
          #4

          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()))@

          life is just lines of code

          1 Reply Last reply
          0
          • E Offline
            E Offline
            Eddy
            wrote on 15 Oct 2011, 16:36 last edited by
            #5

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

            Qt Certified Specialist
            www.edalsolutions.be

            1 Reply Last reply
            0
            • M Offline
              M Offline
              maherali
              wrote on 15 Oct 2011, 16:38 last edited by
              #6

              Thank you Eddy for your comment.

              life is just lines of code

              1 Reply Last reply
              0

              4/6

              15 Oct 2011, 16:25

              • Login

              • Login or register to search.
              4 out of 6
              • First post
                4/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved