Hello! I'm using QTcpSocket to send messages to a remote server.
Here's a quick example to give you a heads-up:
{
// Connect to server and launch session:
socket->connectToHost("server.example.com",12345);
socket->write("Session: self;Start");
// OK, Session is connected.
// If I call this:
socket->write("Session: self; Disconnect");
// The session closes successfully.
}
Myclass::Myclass(QWidget *parent) : QWidget(parent)
{
QTcpSocket *socket = new QTcpSocket; // To know what socket is
// Connect to server and launch session:
socket->connectToHost("server.example.com",12345);
socket->write("Session: self;Start");
// OK, Session is connected.
// If I call this:
socket->write("Session: self; Disconnect");
// The session closes successfully.
}
To copy to clipboard, switch view to plain text mode
However...
Myclass::~Myclass() {
socket->write("Session: self; Disconnect");
qDebug() << "Program closing...";
}
Myclass::~Myclass() {
socket->write("Session: self; Disconnect");
qDebug() << "Program closing...";
}
To copy to clipboard, switch view to plain text mode
On closing my application (Alt+F4, closing the window), the debug console says:
Program closing...
but the socket is not sending the message to the server and my session remains opened.
If I do this:
Myclass::~Myclass()
{
socket->write("Session: this; Disconnect");
QMessageBox::warning(this,
"Closing",
"Program is closing...");
qDebug() << "Program closed.";
}
Myclass::~Myclass()
{
socket->write("Session: this; Disconnect");
QMessageBox::warning(this,"Closing", "Program is closing...");
qDebug() << "Program closed.";
}
To copy to clipboard, switch view to plain text mode
And voila, it works. it sends the message, pops up the warning box, and prints the message. Is there a way to make it work without the Messagebox ?
Bookmarks