supergillis
2nd June 2010, 14:05
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:
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();
}
}
And this is the code of the session (subclass of QThread with socket a QTcpSocket):
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";
}
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
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
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?
The code
This is my server (subclass of QTcpServer) code:
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();
}
}
And this is the code of the session (subclass of QThread with socket a QTcpSocket):
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";
}
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
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
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?