请举一个服务器端的例子,它显示使用 TLS。
现在我有以下代码:
#include <QCoreApplication>
#include "server.h"
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
Server h;
return a.exec();
}
///////////////////////////////////////
#ifndef SERVER_H
#define SERVER_H
#include <QTcpServer>
class Server : public QTcpServer {
public:
Server();
void incomingConnection(int);
};
#endif // SERVER_H
///////////////////////////////////////////
#include "server.h"
#include <QSslSocket>
#include <QSslCertificate>
Server::Server() {
if (!listen(QHostAddress::Any, 80)) {
//error
}
}
void Server::incomingConnection(int d) {
QSslSocket * socket = new QSslSocket();
if(socket->setSocketDescriptor(d)) {
QString c, k;
c = "site.crt";
k = "site.key";
socket->setLocalCertificate(c);
socket->setPrivateKey(k);
socket->startServerEncryption();
if(socket->waitForEncrypted()) {
if(socket->waitForReadyRead()) {
socket->write(socket->readAll());
socket->waitForBytesWritten();
socket->disconnectFromHost();
if(socket->state() == QTcpSocket::UnconnectedState) {
socket->waitForDisconnected();
}
socket->close();
socket->deleteLater();
}
else {
delete socket;
}
}
else {
delete socket;
}
}
}
如何将其更改为先使用协议 TLS,然后再使用 SSL?
常见的任务是编写一个支持 SNI 的服务器。