Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 269
Дмитрий Сидельников Avatar asked Dec 19 '25 16:12

Дмитрий Сидельников


1 Answers

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:

  • disconnectFromHost is what you should use to close the client or the server
  • It's the proper behavior for the server to emit an error that indicates that a remote host closed the connection
like image 103
skypjack Avatar answered Dec 22 '25 05:12

skypjack



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!