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:
Qt Code:
  1. bool m_bConnected;
  2. QTcpServer *m_server;
  3. QTcpSocket *client;
To copy to clipboard, switch view to plain text mode 
Init:
Qt Code:
  1. m_server = new QTcpServer(this);
  2. connect(m_server, SIGNAL(newConnection()), this, SLOT(connectNewClient()));
  3. m_server->listen(QHostAddress::Any, MY_PORT);
To copy to clipboard, switch view to plain text mode 

Connect:
Qt Code:
  1. void ethComm::connectNewClient(void)
  2. {
  3. client = m_server->nextPendingConnection();
  4. connect(this, SIGNAL(DisconnectClient()), client, SLOT(deleteLater()));
  5. connect(client, SIGNAL(disconnected()), this, SLOT(myClientDisconnected()));
  6. m_bConnected = true;
  7. connect(client, SIGNAL(readyRead()), this, SLOT(processPendingData()));
  8. }
To copy to clipboard, switch view to plain text mode 

when disconnected:
Qt Code:
  1. void ethComm::myClientDisconnected(void)
  2. {
  3. m_bConnected = false;
  4.  
  5. disconnect(client, SIGNAL(disconnected()), this, SLOT(myClientDisconnected()));
  6. disconnect(client, SIGNAL(readyRead()), this, SLOT(processPendingData()));
  7.  
  8. client->close();
  9.  
  10. emit DisconnectClient();
  11. }
To copy to clipboard, switch view to plain text mode 
Destructor:
Qt Code:
  1. ethComm::~ethComm(void)
  2. {
  3. if(m_bConnected)
  4. client->close();
  5. if(m_server)
  6. delete(m_server);
  7. }
To copy to clipboard, switch view to plain text mode