PDA

View Full Version : [QTcpServer] Lost Connection



Markus_AC
10th January 2012, 11:58
Hi,

I'm struggeling about how to disconnect a TCP connection correctly when this connection fails, e.g. when unplugging the ethernet cable.
I have a QTcpServer which is listening for any client at one specific port. When a client gets connected, my slot connectNewClient is triggered. When I close my connection I can reconnect without any problem.
But when my connection is closed by any unexpected purpose (e.g. unplugging the cable), my slot myClientDisconnected is triggered. But when I'm trying to reconnect, the client isn't able to connect again. I have to do a reboot of my (Windows-) computer. Just disabling and enabling my NIC doesn't help.
All TCP-Client-Server examples instantaneously disconnect the client when data was sent. But my client-server-connection should keep opened when connected once.
I hope s.o. can help me.

I have this code:
my header:


bool m_bConnected;
QTcpServer *m_server;
QTcpSocket *client;

Init:


m_server = new QTcpServer(this);
connect(m_server, SIGNAL(newConnection()), this, SLOT(connectNewClient()));
m_server->listen(QHostAddress::Any, MY_PORT);


Connect:


void ethComm::connectNewClient(void)
{
client = m_server->nextPendingConnection();
connect(this, SIGNAL(DisconnectClient()), client, SLOT(deleteLater()));
connect(client, SIGNAL(disconnected()), this, SLOT(myClientDisconnected()));
m_bConnected = true;
connect(client, SIGNAL(readyRead()), this, SLOT(processPendingData()));
}


when disconnected:


void ethComm::myClientDisconnected(void)
{
m_bConnected = false;

disconnect(client, SIGNAL(disconnected()), this, SLOT(myClientDisconnected()));
disconnect(client, SIGNAL(readyRead()), this, SLOT(processPendingData()));

client->close();

emit DisconnectClient();
}

Destructor:


ethComm::~ethComm(void)
{
if(m_bConnected)
client->close();
if(m_server)
delete(m_server);
}

Markus_AC
11th January 2012, 07:58
some more additional information:

my server tries to send data to the client, but as the TCP packet isn't ACK-ed by the client, KeepAlive-messages are send. If these 5 KeepAlive-messages fail, my QTcpServer recognized that the client is disconencted (as the signal "disconnected" is emitted).

wysota
12th January 2012, 12:08
So what is the problem exactly? Technically speaking unpluging the network cable from a computer DOES NOT break a TCP connection.