SSLサーバーとクライアント
QSslSockets を使用したセキュアなリモートオブジェクトネットワークの設定。
完全に制御できないネットワークを介してデータを転送する必要がある場合、通信の暗号化は極めて重要です。この例の2つのアプリケーションでは、SSL接続を介してリモートオブジェクトを共有する方法と、それらにアクセスする方法を示しています。

sslserverとsslcppclientの両方は、sslserver/cert ディレクトリにあるカスタムルート CA 証明書を使用して、互いの証明書を検証しています。
SSL サーバー
sslserverは、証明書と秘密鍵を使用して設定されます。
autoconfig=QSslConfiguration::defaultConfiguration();
config.setCaCertificates(QSslCertificate::fromPath(QStringLiteral(":/sslcert/rootCA.pem")));
QFile certificateFile(QStringLiteral(":/sslcert/server.crt"));
if(certificateFile.open(QIODevice::ReadOnly|QIODevice::Text))
config.setLocalCertificate(QSslCertificate(certificateFile.readAll(),QSsl::Pem));
else
qFatal("Could not open certificate file");
QFile keyFile(QStringLiteral(":/sslcert/server.key"));
if(keyFile.open(QIODevice::ReadOnly|QIODevice::Text)) {
QSslKey key(keyFile.readAll(),QSsl::Rsa,QSsl::Pem,QSsl::PrivateKey);
if(key.isNull())
qFatal("Key is not valid");
config.setPrivateKey(key);
}else{
qFatal("Could not open key file");
}
config.setPeerVerifyMode(QSslSocket::VerifyPeer);
QSslConfiguration::setDefaultConfiguration(config);その後、QRemoteObjectHost オブジェクトとQSslServer オブジェクトが作成されます。QSslServer オブジェクトはポート65511でリスニングを行います。続いて、QRemoteObjectHost オブジェクトに対して、QSslServer オブジェクトのURLを引数としてsetHostUrlが呼び出されます。
QRemoteObjectHost host;
QSslServer server;
server.listen(QHostAddress::Any, 65511);
host.setHostUrl(server.serverAddress().toString(), QRemoteObjectHost::AllowExternalRegistration);ラムダ式を使用して、errorOccurredシグナルを処理し、エラーをターミナルに出力します。2つ目のラムダ式はpendingConnectionAvailableシグナルに接続され、エラーハンドラを接続するとともに、QRemoteObjectHost オブジェクトに対して、着信ソケットを引数としてaddHostSideConnectionを呼び出し、ホストオブジェクトがそのソケットを通信に使用するようにします。
QObject::connect(&server, &QSslServer::errorOccurred,
[](QSslSocket*socket,QAbstractSocket::SocketErrorerror) {
Q_UNUSED(socket);
qDebug() << "QSslServer::errorOccurred" << error;
});
QObject::connect(&server, &QSslServer::pendingConnectionAvailable, [&server, &host]() {
qDebug() << "New connection available";
QSslSocket*socket =qobject_cast<QSslSocket*>(server.nextPendingConnection());
Q_ASSERT(socket);
QObject::connect(socket, &QSslSocket::errorOccurred,
[](QAbstractSocket::SocketErrorerror) {
qDebug() << "QSslSocket::error" << error;
});
host.addHostSideConnection(socket);
});最後に、MinuteTimer オブジェクトを作成し、QRemoteObjectHost オブジェクトに対して MinuteTimer オブジェクトを引数として enableRemoting を呼び出し、共有できるようにします。
MinuteTimer timer;
host.enableRemoting(&timer);SSLクライアント
sslcppclientは、ルートCA証明書を設定した後、Testerオブジェクトを作成します。
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
auto config = QSslConfiguration::defaultConfiguration();
config.setCaCertificates(QSslCertificate::fromPath(QStringLiteral(":/sslcert/rootCA.pem")));
QSslConfiguration::setDefaultConfiguration(config);
Tester t;
return a.exec();
}Testerのコンストラクタでは、一時的なQRemoteObjectNode オブジェクトが作成され、setupConnectionを使用してQSslSocket オブジェクトを作成・設定します。エラーハンドラが接続され、QRemoteObjectNode オブジェクトは、addClientSideConnectionを呼び出すことでQSslSocket を利用します。
QRemoteObjectNode m_client;
autosocket=setupConnection();
connect(socket, &QSslSocket::errorOccurred,
socket, [](QAbstractSocket::SocketErrorerror){
qDebug() << "QSslSocket::error" << error;
}) ;
m_client.addClientSideConnection(socket);次に、Testerクラスのメンバである3つのQScopedPointer が、QRemoteObjectNode オブジェクトのacquireメソッドを使用して、MinuteTimerの3つのレプリカに接続されます。最後に、QTimer::singleShot が4回呼び出され、遅延後にresetが実行されます。
ptr1.reset(m_client.acquire< MinuteTimerReplica >());
ptr2.reset(m_client.acquire< MinuteTimerReplica >());
ptr3.reset(m_client.acquire< MinuteTimerReplica >());
QTimer::singleShot(0, this, &Tester::clear);
QTimer::singleShot(1, this, &Tester::clear);
QTimer::singleShot(10000, this, &Tester::clear);
QTimer::singleShot(11000, this, &Tester::clear);Tester::clear が最初の3回呼び出されると、1つのポインタがバインドされているか確認され、リセットされます。この際、毎回異なるポインタが対象となります。4回目に呼び出されると、アプリケーションが終了します。
voidclear()
{
static inti= 0;
if(i== 0) {
i++;
if(ptr1.isNull())
qCritical() << "Pointer 1 was not set";
ptr1.reset();
}else if(i== 1) {
i++;
if(ptr2.isNull())
qCritical() << "Pointer 2 was not set";
ptr2.reset();
}else if(i== 2) {
i++;
if(ptr3.isNull())
qCritical() << "Pointer 3 was not set";
ptr3.reset();
}else{
qApp->quit();
}
}© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.