c++qtqtcpsocketqtcpserver

On server side in QTcpServer appears: The remote host closed the connection


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);
}

Solution

  • 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()QAbstractSocket enters QAbstractSocket::ClosingState. After all pending data has been written to the socket, QAbstractSocket actually closes the socket, enters QAbstractSocket::UnconnectedState, and emits disconnected(). If you want to abort a connection immediately, discarding all pending data, call abort() instead. If the remote host closes the connection, QAbstractSocket will emit error(QAbstractSocket::RemoteHostClosedError), during which the socket state will still be ConnectedState, and then the disconnected() signal will be emitted.

    Therefore I'd say that: