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;
bool m_bConnected;
QTcpServer *m_server;
QTcpSocket *client;
To copy to clipboard, switch view to plain text mode
Init:
connect(m_server, SIGNAL(newConnection()), this, SLOT(connectNewClient()));
m_server = new QTcpServer(this);
connect(m_server, SIGNAL(newConnection()), this, SLOT(connectNewClient()));
m_server->listen(QHostAddress::Any, MY_PORT);
To copy to clipboard, switch view to plain text mode
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()));
}
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()));
}
To copy to clipboard, switch view to plain text mode
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();
}
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();
}
To copy to clipboard, switch view to plain text mode
Destructor:
ethComm::~ethComm(void)
{
if(m_bConnected)
client->close();
if(m_server)
delete(m_server);
}
ethComm::~ethComm(void)
{
if(m_bConnected)
client->close();
if(m_server)
delete(m_server);
}
To copy to clipboard, switch view to plain text mode
Bookmarks