Catalog
Basic concepts
Look at the signal before putting in the code:
[signal] void QAbstractSocket::stateChanged(QAbstractSocket::SocketState socketState)
This information is triggered when the socket state changes, where the socketState parameter is the current socket state.
Note here that QAbstractSocket::SocketState is an unregistered metatype. If it is a queue connection (the fourth parameter of connection, which is mainly introduced in my other blog posts on cross-threading), it must be declared by Q_DECLARE_METATYPE() and entered by qRegisterMetaType. Row registration (also with corresponding blog notes)
[signal] void QAbstractSocket::error(QAbstractSocket::SocketError socketError)
When an error is sent, the signal is triggered and the parameter is the description of the error.
Here's an official suggestion: when the socket is not connected, it triggers the signal, but it can be avoided by using an event loop mechanism, such as setting the latency using QTimer::singleShot().
Here's an official example of connect
connect(abstractSocket, static_cast<void(QAbstractSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error), [=](QAbstractSocket::SocketError socketError){ /* ... */ });
Let's look at the last signal:
[signal] void QSslSocket::sslErrors(const QList<QSslError> &errors)
This signal is mainly sent after the ssl handshake. Such errors are usually connection errors, such as the inability to identify the other party, and so on.
Officials have also offered some suggestions, such as using QSslSocket::ignoreSslErrors() or calling sslErrors() to process the connection later if you want to ignore the error.
Code and examples
After running the program for tens of seconds, I disconnected the network. The running screenshots are as follows:
The source code is as follows:
#include <QCoreApplication> #include <QSslSocket> #include <QAbstractSocket> #include <QSslCipher> #include <QObject> #include <QDebug> #include <QList> #include <QEventLoop> #include <QSslError> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QSslSocket *socket = new QSslSocket; socket->connectToHostEncrypted("www.baidu.com", 443); QObject::connect(socket, static_cast<void(QSslSocket::*)(QAbstractSocket::SocketState)>(&QSslSocket::stateChanged),[=](QAbstractSocket::SocketState state){ qDebug() << "The socket state is : " << state; }); QObject::connect(socket, &QSslSocket::encrypted, [=](){ QSslCipher ciph = socket->sessionCipher(); QString cipher = QString("%1, %2 (%3/%4)").arg(ciph.authenticationMethod()) .arg(ciph.name()).arg(ciph.usedBits()).arg(ciph.supportedBits());; qDebug() << "the cipher info is : " << cipher; }); QObject::connect(socket, static_cast<void(QSslSocket::*)(QAbstractSocket::SocketError)>(&QSslSocket::error), [=](QAbstractSocket::SocketError error){ qDebug() << "The error is : " << error; }); QEventLoop loop; QObject::connect(socket, SIGNAL(sslErrors(QList<QSslError>)), &loop, SLOT(quit())); loop.exec(); qDebug() << socket->sslErrors(); return a.exec(); }