I'm writing client-server app, which uses QTcpSocket and QTcpServer. Here are the disconnected() slots:
server:
void testServer::disconnected()
{
socket = qobject_cast<QTcpSocket*>(sender());
qDebug() << " - CLIENT DISCONNECTED";
socket->disconnectFromHost();
if(server->isListening())
{
server->close();
qDebug() << " - SERVER CLOSED";
}
}
client:
void testClient::disconnected()
{
qDebug() << " - SERVER DISCONNECTED";
tcpSocket->disconnectFromHost();
}
I face strange behavior when I try to imitate possible crashes when both sides are connected: when I close the server I can always reconnect to the new one with my client. But if I close the client while being connected, I can reconnect the server to the client just two times. And no more.
On the third "establish connection - restart the client - connect to the server" routine I always fail.
QTcpServer::newConnection()
signal is emitted, but
server->hasPendingConnections()
returns false, so I can't assign the socket with
socket = server->nextPendingConnection()
What's the problem with reconnecting QTcpServer? Also, server and client always runs on const ports, e.x. 1234 for server and 4321 for client. Can this be the issue?
server->close()
if you need a reconnect feature? – Dmitry Sazonov