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:
{
listen(address, port);
}
void Server::stop()
{
close();
}
void Server::incomingConnection(int descriptor)
{
try
{
// The session is a QThread.
// When the thread finishes, the object executes deleteLater().
// So I think we don't need to worry about a memory leak.
Session* session = new Session(descriptor, this);
session->start();
}
catch(Exception& exception)
{
qDebug() << "Error:" << exception.message();
}
}
void Server::start(QHostAddress address, unsigned int port)
{
listen(address, port);
}
void Server::stop()
{
close();
}
void Server::incomingConnection(int descriptor)
{
try
{
// The session is a QThread.
// When the thread finishes, the object executes deleteLater().
// So I think we don't need to worry about a memory leak.
Session* session = new Session(descriptor, this);
session->start();
}
catch(Exception& exception)
{
qDebug() << "Error:" << exception.message();
}
}
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):
{
if(!socket.setSocketDescriptor(descriptor))
throw Exception("Unable to set session descriptor!");
QObject::connect(this,
SIGNAL(finished
()),
this,
SLOT(deleteLater
()));
qDebug() << "[Session] constructed";
}
Session::~Session()
{
// If the socket is still open, then we need to close it.
if(socket.isOpen())
socket.close();
qDebug() << "[Session] destructed";
}
void Session::run()
{
while(socket.isOpen() && socket.waitForReadyRead())
available();
qDebug() << "[Session] leaving main loop";
}
void Session::available()
{
// Read the data.
qDebug() << "read" << data.size() << "bytes";
}
Session::Session(int descriptor, QObject* parent): QThread(parent)
{
if(!socket.setSocketDescriptor(descriptor))
throw Exception("Unable to set session descriptor!");
QObject::connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
qDebug() << "[Session] constructed";
}
Session::~Session()
{
// If the socket is still open, then we need to close it.
if(socket.isOpen())
socket.close();
qDebug() << "[Session] destructed";
}
void Session::run()
{
while(socket.isOpen() && socket.waitForReadyRead())
available();
qDebug() << "[Session] leaving main loop";
}
void Session::available()
{
// Read the data.
QByteArray data = socket.readAll();
qDebug() << "read" << data.size() << "bytes";
}
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:
[Session] constructed
read 151 bytes
[Session] constructed
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!
The program has unexpectedly finished.
/mnt/Server/Projects/qtibia/qtibia exited with code 0
The program has unexpectedly finished.
/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?
Bookmarks