I have a QTcpServer app and QTcpClient app. See my screenshot. When a client after interacting with server is disconnecting from server, on server side appears event (in client socket - in slot):
void CMyClient::onSocketDisplayError(QAbstractSocket::SocketError socketError)
{
QString sErr = m_pClientSocket->errorString();
m_pWin->AddMessageFormClient("Was gotten some error! " + sErr);
}
Error message:
The remote host closed the connection.
After that appears an event:
void CMyClient::onSocketDisconnected()
{
m_pWin->AddMessageFormClient("Client is disconnected!");
m_pWin->UpdateDisconnectUI();
}
Is it proper behavior on server side to generate onSocketDisplayError?
The code to disconnect on client side:
void MainWindow::on_pushButton_DisconnectFromServ_clicked()
{
m_pSocket->disconnectFromHost();
m_pSocket->waitForDisconnected(3000);
}
According with the documentation of QAbstractSocket, that is the class behind a QTcpSocket and thus your client and server (emphasis mine):
To close the socket, call
disconnectFromHost().QAbstractSocketentersQAbstractSocket::ClosingState. After all pending data has been written to the socket,QAbstractSocketactually closes the socket, entersQAbstractSocket::UnconnectedState, and emitsdisconnected(). If you want to abort a connection immediately, discarding all pending data, callabort()instead. If the remote host closes the connection,QAbstractSocketwill emiterror(QAbstractSocket::RemoteHostClosedError), during which the socket state will still beConnectedState, and then thedisconnected()signal will be emitted.
Therefore I'd say that:
disconnectFromHost is what you should use to close the client or the serverIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With