PDA

View Full Version : QTcpSocket - How do I know when it is disconnected ?



probine
3rd April 2006, 14:51
I have a thread that handles different connections from different computers.

I want to know when one of those connections (telnet) has been closed. I am trying to use the disconnect signal, but the method clientDisconnected is never called.



# include <QTcpSocket>

# include <iostream>
# include "client.h"
# include "server.h"
using namespace std;

Client :: Client(int socketDescriptor, Server *server, QObject *parent) : QThread(parent)
{
this->socketDescriptor = socketDescriptor;
this->server = server;
chattingWith = new QString();
connect(this, SIGNAL(finished()), this, SLOT(finished()));
}


void Client :: run()
{
cout << "void Client :: run()\n";
tcpSocket = new QTcpSocket();
if(!tcpSocket->setSocketDescriptor(socketDescriptor))
{
cout << "Error - tcpSocket descriptor\n";
return;
}
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readData()));
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
exec();
}


void Client :: readData()
{
cout << "void Client :: readData()\n";
server->addClient("12345", this);
}


void Client :: clientDisconnected()
{
cout << "void Client :: clientDisconnected()\n";
}

void Client :: finished()
{
cout << "The thread has finished\n";
}



Why ? What is wrong ?

probine
3rd April 2006, 20:25
Why is the QTcpSocket disconnect signal never emitted ?

probine
3rd April 2006, 21:05
I found the problem:

The signal was never emitted because I was only compiling the program with "make". In order to have the signal working I had to type "qmake" first, then "make".

Out of this test I assume that whenever I code something with SIGNALS and SLOTS, then I will "qmake" my program first.