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?