QSslServer Class
The QSslServer class accepts TLS connections. More…
| Header | #include <QSslServer> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Network) |
| Module | Nt Network C++ Classes |
| Inherits | QTcpServer |
List of all members, including inherited members
Public Functions
| QSslServer(QObject *parent = nullptr) | |
| ~QSslServer() | |
| void | setSslConfiguration(const QSslConfiguration &sslConfiguration) |
| QSslConfiguration | sslConfiguration() const |
| void | setHandshakeTimeout(int timeout) |
| int | handshakeTimeout() const |
Signals
| void | sslErrors(QSslSocket *socket, const QList<QSslError> &errors) |
| void | errorOccurred(QSslSocket *socket, QAbstractSocket::SocketError error) |
| void | startedEncryptionHandshake(QSslSocket *socket) |
Reimplemented Protected Functions
| void | incomingConnection(qintptr socket) |
Detailed Description
Set a configuration holding a certificate and key, then use it like QTcpServer. newConnection() is emitted only for clients that finish the handshake, and nextPendingConnection() returns a socket that is already encrypted:
QSslConfiguration config = QSslConfiguration::defaultConfiguration();
config.setLocalCertificate(certificate);
config.setPrivateKey(key);
QSslServer server;
server.setSslConfiguration(config);
server.listen(QHostAddress::Any, 8443);
QObject::connect(&server, &QSslServer::newConnection, [&] {
auto *client = static_cast<QSslSocket *>(server.nextPendingConnection());
client->write("hello\n");
});
A client that does not finish its handshake within handshakeTimeout() is dropped, so a slow client cannot hold connections open indefinitely.
See also QSslSocket and QSslConfiguration.
Differences from Qt
The alertSent, alertReceived, peerVerifyError, preSharedKeyAuthenticationRequired and handshakeInterruptedOnError signals are absent.
Member Function Documentation
QSslServer::QSslServer(QObject *parent = nullptr)
Constructs a server with the given parent.
QSslServer::~QSslServer()
Destroys the server.
void QSslServer::errorOccurred(QSslSocket *socket, QAbstractSocket::SocketError error)
This signal is emitted when socket fails with error before its handshake finishes. The socket is then deleted.
int QSslServer::handshakeTimeout() const
Returns how long a client has to finish its handshake, in milliseconds. The default is 5000.
See also setHandshakeTimeout().
void QSslServer::incomingConnection(qintptr socket)
Reimplements QTcpServer::incomingConnection().
void QSslServer::setHandshakeTimeout(int timeout)
Sets how long a client has to finish its handshake to timeout milliseconds.
See also handshakeTimeout().
void QSslServer::setSslConfiguration(const QSslConfiguration &sslConfiguration)
Sets the configuration for new connections to sslConfiguration.
See also sslConfiguration().
QSslConfiguration QSslServer::sslConfiguration() const
Returns the configuration for new connections.
See also setSslConfiguration().
void QSslServer::sslErrors(QSslSocket *socket, const QList<QSslError> &errors)
This signal is emitted when socket reports verification errors.
void QSslServer::startedEncryptionHandshake(QSslSocket *socket)
This signal is emitted when a handshake begins on socket.