Qt: Why is `QAbstractSocket::error(QAbstractSocket::SocketError)` signal not generated when internet is disconnected?

走远了吗. 提交于 2019-12-24 07:01:17

问题


I am trying to achieve a signal when the internet is disconnected for an already connected SSL socket. Here is the way I have derived the QSslSocket:

struct CloudSSL::Socket : public QSslSocket
{
  Q_OBJECT public:

  void ConnectSlots ()
  {
    connect(this, SIGNAL(readyRead()), this, SLOT(ReceiveData()));
    connect(this, SIGNAL(disconnected()), this, SLOT(Disconnected()));
    // *** None of the above or below is invoking when internet disconnects ***
    connect(this, SIGNAL(error(QAbstractSocket::SocketError)),
            this, SLOT(Error(QAbstractSocket::SocketError)));
  }

  virtual ~Socket ()
  {
    QObject::disconnect();
    QSslSocket::abort();
  }

  public slots:
  void ReceiveData ()
  {
    LOG("Socket received data...");
  }

  void Disconnected ()
  {
    LOG("Socket got disconnected...");
  }

  void Error (QAbstractSocket::SocketError error)
  {
    LOG("Socket error ", error);
  }
}

Here is how it's initialized:

m_pSSLSocket = new Socket;
m_pSSLSocket->setProtocol(QSsl::TlsV1_2);
m_pSSLSocket->setLocalCertificateChain(QSslCertificate::fromPath(":/Certificate.pem", QSsl::Pem));
m_pSSLSocket->setPrivateKey(QSslKey(privateKeyFile.readAll(), QSsl::Rsa));
m_pSSLSocket->setSocketOption(QAbstractSocket::LowDelayOption, true);  // <---
m_pSSLSocket->setSocketOption(QAbstractSocket::KeepAliveOption, true);  // <---
m_pSSLSocket->connectToHostEncrypted(SAARATHY_URL, SAARATHY_PORT);
m_pSSLSocket->ignoreSslErrors();

Things work fine in general. However, if I disable wifi in my Ubuntu PC, then I don't get any network error as expected from the QAbstractSocket::SocketError:

QAbstractSocket::NetworkError -- 7 -- An error occurred with the network (e.g., the network cable was accidentally plugged out).

Referred following posts before this Qn:

  • QTcpSocket state always connected, even unplugging ethernet wire
  • Qt TCP/IP socket connection check

Question: What is the Qt exclusive way of receiving a signal when the internet is disconnected?


回答1:


Unless the protocols you use have some sort of keep-alive, if you don't send anything nothing will be sent and no attempt of error checking will be done.

If you want to see if there's problem with a connection you actually have to send something. If a cable is unplugged or there is any other problem between you and the remote host, then (after a suitable timeout and retries) you will get an error.

To see if the remote host has closed the connection in a nice way, you have to attempt to read something, in which case the receive call will return that it has read zero bytes.



来源:https://stackoverflow.com/questions/44803126/qt-why-is-qabstractsocketerrorqabstractsocketsocketerror-signal-not-gen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!