1

我已经使用 QSslSocket 实现了一个 ssl 服务器并正确运行它。但我有一些问题,我无法立即解决。我认为只需将 readyRead() 信号连接到用于读取缓冲区的插槽就足够了,但我已经认识到在这种情况下 readyRead() 根本不会发出,我还必须在我的代码中使用 waitForReadyRead() 函数。但问题是使用此函数会导致阻塞读取缓冲区。实际上我想知道当数据到达时我如何读取缓冲区而不阻塞?

Bellow 是我实现的 ssl 服务器:

#include "sslserver.h"
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include <QFile>
#include <QtNetwork/QSslKey>
#include <QtNetwork/QSslConfiguration>
#include <QtNetwork/QSslError>

SslServer::SslServer(QObject *parent) : QTcpServer(parent)
{
    server = new QTcpServer(this);

    if(!server->listen(QHostAddress::Any, 9996))
    {
        qDebug() << "Server could not start";
    }
    else
    {
        qDebug() << "Server started!";
    }

    connect(server, SIGNAL(newConnection()), this, SLOT(newConnectionRecognized()));

}

void SslServer::showErrors()
{
    this-> err = socket->sslErrors();
    for(int i=0;i<err.size();i++)
        qDebug() << err[i];
}

SslServer::~SslServer()
{

}

void SslServer::newConnectionRecognized()
{

    incomingConnection(server->nextPendingConnection()->socketDescriptor());

}

void SslServer::incomingConnection(qintptr socketDescriptor)
{
    socket = new QSslSocket(this);
    socket->setProtocol(QSsl::SslV3);

    connect(socket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(showErrors()));
    connect(socket, SIGNAL(encrypted()), this, SLOT(ready()));
    connect(socket, SIGNAL(readyRead()), this, SLOT(readChannel()));

    // Read Key from file

    QByteArray key;
    QFile KeyFile("server.key");
    if(KeyFile.open(QIODevice::ReadOnly))
    {
        key = KeyFile.readAll();
        KeyFile.close();
    }
    else
    {
        qDebug() << KeyFile.errorString();
    }

    QSslKey sslKey(key, QSsl::Rsa);
    socket->setPrivateKey(sslKey);


    // Load server ssl certificate from file
    QByteArray cert;
    QFile CertFile("server.csr");
    if(CertFile.open(QIODevice::ReadOnly))
    {
        cert = CertFile.readAll();
        CertFile.close();
    }
    else
    {
        qDebug() << CertFile.errorString();
    }

    QSslCertificate sslCert(cert);
    socket->setLocalCertificate(sslCert);

    QSslConfiguration cfg = socket->sslConfiguration();
    cfg.caCertificates();

    if (!socket->setSocketDescriptor(socketDescriptor))ee
    {
        qDebug() << ("! Couldn't set socket descriptor");
        delete socket;
        return;
    }

    socket->startServerEncryption();

    if (socket->isEncrypted())
        emit socket->encrypted();

    if(!socket->waitForEncrypted(3000)) {
        qDebug("Wait for encrypted!!!!");
        return;
    }

    while (true) {
        socket->waitForReadyRead();
    }
}


void SslServer::readChannel()
{
    QByteArray qstrbytes = socket->readLine();
    qDebug() << qstrbytes;

}

void SslServer::ready()
{
    qDebug() << "Encrypted";
}
4

1 回答 1

0

当我实现另一个客户端/服务器但这次使用 QTcpSocket 时,我发现了问题。我不知道确切原因,但我想问题是因为使用 socketDescriptor 来创建 QSslSocket。当我使用 QTcpSocket 创建客户端和服务器时,它们可以在没有任何事件循环的情况下完美运行,并且只需将 readyRead() 信号连接到插槽即可。之后为了测试某些情况,我使用 socketDescriptor 创建了 QTcpSocket。然后我发现问题出在使用 socketDescriptor 创建套接字,因为这一次 readyRead() 信号不像以前那样工作。

于 2015-06-11T14:14:19.277 回答