6.0.0-rc4Source

QSslServer Class

The QSslServer class accepts TLS connections. More…

Header#include <QSslServer>
CMaketarget_link_libraries(app PRIVATE Nt::Network)
ModuleNt Network C++ Classes
InheritsQTcpServer

List of all members, including inherited members

Public Functions

QSslServer(QObject *parent = nullptr)
~QSslServer()
voidsetSslConfiguration(const QSslConfiguration &sslConfiguration)
QSslConfigurationsslConfiguration() const
voidsetHandshakeTimeout(int timeout)
inthandshakeTimeout() const

Signals

Reimplemented Protected Functions

voidincomingConnection(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

[explicit] QSslServer::QSslServer(QObject *parent = nullptr)

Constructs a server with the given parent.

[override] QSslServer::~QSslServer()

Destroys the server.

[signal] 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().

[override, protected] 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().

[signal] void QSslServer::sslErrors(QSslSocket *socket, const QList<QSslError> &errors)

This signal is emitted when socket reports verification errors.

[signal] void QSslServer::startedEncryptionHandshake(QSslSocket *socket)

This signal is emitted when a handshake begins on socket.