Results 1 to 4 of 4

Thread: My server (using QTcpServer and QTcpSocket) crashes

  1. #1
    Join Date
    Aug 2008
    Posts
    35
    Thanks
    7
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default My server (using QTcpServer and QTcpSocket) crashes

    This is the first server application I make using Qt. I've checked the examples in the documentation, but I couldn't find what was wrong...

    The code

    This is my server (subclass of QTcpServer) code:
    Qt Code:
    1. void Server::start(QHostAddress address, unsigned int port)
    2. {
    3. listen(address, port);
    4. }
    5.  
    6. void Server::stop()
    7. {
    8. close();
    9. }
    10.  
    11. void Server::incomingConnection(int descriptor)
    12. {
    13. try
    14. {
    15. // The session is a QThread.
    16. // When the thread finishes, the object executes deleteLater().
    17. // So I think we don't need to worry about a memory leak.
    18. Session* session = new Session(descriptor, this);
    19. session->start();
    20. }
    21. catch(Exception& exception)
    22. {
    23. qDebug() << "Error:" << exception.message();
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 
    And this is the code of the session (subclass of QThread with socket a QTcpSocket):
    Qt Code:
    1. Session::Session(int descriptor, QObject* parent): QThread(parent)
    2. {
    3. if(!socket.setSocketDescriptor(descriptor))
    4. throw Exception("Unable to set session descriptor!");
    5. QObject::connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
    6. qDebug() << "[Session] constructed";
    7. }
    8.  
    9. Session::~Session()
    10. {
    11. // If the socket is still open, then we need to close it.
    12. if(socket.isOpen())
    13. socket.close();
    14. qDebug() << "[Session] destructed";
    15. }
    16.  
    17. void Session::run()
    18. {
    19. while(socket.isOpen() && socket.waitForReadyRead())
    20. available();
    21. qDebug() << "[Session] leaving main loop";
    22. }
    23.  
    24. void Session::available()
    25. {
    26. // Read the data.
    27. QByteArray data = socket.readAll();
    28. qDebug() << "read" << data.size() << "bytes";
    29. }
    To copy to clipboard, switch view to plain text mode 

    The problem

    Now when I connect and send data to the server, it receives it.
    So the server gives this output:
    Qt Code:
    1. [Session] constructed
    2. read 151 bytes
    To copy to clipboard, switch view to plain text mode 
    The server doesn't answer yet, so I just close the client connection, and BANG!
    Qt Code:
    1. The program has unexpectedly finished.
    2. /mnt/Server/Projects/qtibia/qtibia exited with code 0
    To copy to clipboard, switch view to plain text mode 

    So something happens when I close the connection that makes the server crash. I've tried to leave out the delateLater() slot, and all kind of stuff... Sometimes it just works, and sometimes it just crashes...

    What am I doing wrong?

  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: My server (using QTcpServer and QTcpSocket) crashes

    One of the problem surely is that you create the socket in the main thread and try to access it in the session thread. Remember the constructor of your Session object runs in the main thread and not in the session thread. You don't need any theads there, by the way - especially that you are waiting for incoming data all the time there so you can just connect the readyRead() signal to a slot and have a single thread for everything.
    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. The following user says thank you to wysota for this useful post:

    supergillis (14th June 2010)

  4. #3
    Join Date
    Aug 2008
    Posts
    35
    Thanks
    7
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: My server (using QTcpServer and QTcpSocket) crashes

    Ok! Thanks for the fast reply!
    I think I understand what you mean, actually I'm pretty sure, because I changed it and it works now .


    This is what I got now:
    Qt Code:
    1. Session::Session(int descriptor, QObject* parent): QTcpSocket(parent)
    2. {
    3. if(!setSocketDescriptor(descriptor))
    4. throw Exception("Unable to set session descriptor!");
    5. QObject::connect(this, SIGNAL(disconnected()), this, SLOT(deleteLater()));
    6. QObject::connect(this, SIGNAL(readyRead()), this, SLOT(available()));
    7. qDebug() << "[Session] constructed";
    8. }
    9.  
    10. Session::~Session()
    11. {
    12. // If the socket is still open, then we need to close it.
    13. if(isOpen())
    14. close();
    15. qDebug() << "[Session] destructed";
    16. }
    17.  
    18. void Session::available()
    19. {
    20. // Read the data.
    21. QByteArray data = readAll();
    22. qDebug() << "read" << data.size() << "bytes";
    23. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void Server::incomingConnection(int descriptor)
    2. {
    3. try
    4. {
    5. // The session is a QTcpSocket.
    6. // When the client disconnects, the object executes deleteLater().
    7. // So I think we don't need to worry about a memory leak.
    8. new Session(descriptor, this);
    9. }
    10. catch(Exception& exception)
    11. {
    12. qDebug() << "Error:" << exception.message();
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    Also, is this necessary, or does deleting the socket automatically close it too?
    Qt Code:
    1. // If the socket is still open, then we need to close it.
    2. if(isOpen())
    3. close();
    To copy to clipboard, switch view to plain text mode 

  5. #4
    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: My server (using QTcpServer and QTcpSocket) crashes

    Deleting a socket should close it but not gracefully (you should wait for the other end to close the connection too). But if you are sure you are not the one initiating the closing, it's fine.
    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.


Similar Threads

  1. QTcpServer - get the correct QTcpSocket
    By elcuco in forum Qt Programming
    Replies: 4
    Last Post: 11th May 2010, 10:49
  2. QTcpServer QTcpSocket problem
    By jmsbc in forum Qt Programming
    Replies: 0
    Last Post: 20th November 2009, 18:42
  3. File Transfer with QTcpServer and QTcpSocket
    By NoRulez in forum Qt Programming
    Replies: 2
    Last Post: 21st October 2009, 18:12
  4. QTcpSocket, QTcpServer problem
    By frido in forum Qt Programming
    Replies: 3
    Last Post: 4th April 2009, 00:16
  5. QTcpServer & QTcpSocket questions...
    By jxmot in forum Qt Programming
    Replies: 2
    Last Post: 24th April 2008, 22:38

Tags for this Thread

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.