Results 1 to 4 of 4

Thread: QTcpServer--cleaning up when clients disconnect

  1. #1
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpServer--cleaning up when clients disconnect

    I want to use QTcpServer (I think) to create QTcpsocket each time a client connects.

    I want the connection to stay open until the client side disconnects. Then I want the socket to be closed and associated memory freed.

    So, I was thinking something like this for the newConnection slot of QTcpServer:


    Qt Code:
    1. oid EjsServer::newConnection()
    2. {
    3.  
    4. QTcpSocket *socket = server->nextPendingConnection();
    5. connect(socket,SIGNAL(disconnected()),socket,SLOT(deleteLater()));
    6. clientList.append(socket);
    7. socket->write("Connected to APES EJS Server\r\n");
    8. socket->flush();
    9.  
    10. socket->waitForBytesWritten(3000);
    11.  
    12. //socket->close();
    13. }
    To copy to clipboard, switch view to plain text mode 

    But, I think once newConnection finishes, "socket" will go out of scope. But will the memory it's pointing still be good so it's ok to have it as an argument to the deleteLater() slot?

    I also need to remove the socket from "clientList, so instead of just connecting to the socket's "deleteLater", I'm thinking I need a slot in EjsServer that removes the socket from the list then calls deleteLater. But, how to get a handle to the right socket? None of the QTcpSocket signals include a pointer to the QTcpSocket the emitted the signal.

    I guess I need a good QTcpServer example. The fortune example isn't helpful, since it just writes to a client on connection, then closes the connection. Or, implement without using QTcpserver.

    Thanks


    Added after 21 minutes:


    I could use QObject::sender() in the slot to remove the right QTcpSocket * from the list and invoke deleteLater(), but it "feels like" i'm not using the class as intended.
    Last edited by davethomaspilot; 20th February 2015 at 21:32.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpServer--cleaning up when clients disconnect

    Quote Originally Posted by davethomaspilot View Post
    But, I think once newConnection finishes, "socket" will go out of scope.
    Yes. the pointer will go out of scope. The object behind it will not.

    But will the memory it's pointing still be good so it's ok to have it as an argument to the deleteLater() slot?
    Yes, your approach is correct.

    I also need to remove the socket from "clientList, so instead of just connecting to the socket's "deleteLater", I'm thinking I need a slot in EjsServer that removes the socket from the list then calls deleteLater. But, how to get a handle to the right socket? None of the QTcpSocket signals include a pointer to the QTcpSocket the emitted the signal.
    There is QObject::sender() and there is also QObject::destroyed(QObject*) which you can use.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTcpServer--cleaning up when clients disconnect

    Thanks,

    For reference, here's what I did:
    Qt Code:
    1. void EjsServer::newConnection()
    2. {
    3. // need to grab the socket
    4. QTcpSocket *socket = server->nextPendingConnection();
    5. connect(socket,SIGNAL(disconnected()),this,SLOT(clientDisconnected()));
    6. clientList.append(socket);
    7. socket->write("Connected to APES EJS Server\r\n");
    8. socket->flush();
    9.  
    10. socket->waitForBytesWritten(3000);
    11.  
    12. //socket->close();
    13. }
    14.  
    15. void EjsServer::clientDisconnected()
    16. {
    17. QTcpSocket *clientSocket = qobject_cast<QTcpSocket *>(QObject::sender());
    18.  
    19. int idx = clientList.indexOf(clientSocket);
    20. if (idx!=-1)
    21. clientList.removeAt(idx);
    22.  
    23. clientSocket->deleteLater();
    24. }
    To copy to clipboard, switch view to plain text mode 

    That should be leak free?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer--cleaning up when clients disconnect

    Yes, that looks ok.

    Cheers,
    _

Similar Threads

  1. Slow QTcpServer with lots of simultaneous clients
    By Mechan in forum Qt Programming
    Replies: 1
    Last Post: 29th April 2012, 10:11
  2. QTcpServer with threaded clients problem
    By Witek in forum Qt Programming
    Replies: 2
    Last Post: 21st September 2011, 22:10
  3. Replies: 4
    Last Post: 30th November 2010, 21:09
  4. Extend QTcpServer to handle multiple clients
    By DiamonDogX in forum Qt Programming
    Replies: 5
    Last Post: 24th February 2010, 19:49
  5. Replies: 4
    Last Post: 23rd May 2008, 09:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.