Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Qt中使用QSslSocket的Ssl服务器_Qt_Ssl_Qtnetwork_Qtcpserver_Qsslsocket - Fatal编程技术网

在Qt中使用QSslSocket的Ssl服务器

在Qt中使用QSslSocket的Ssl服务器,qt,ssl,qtnetwork,qtcpserver,qsslsocket,Qt,Ssl,Qtnetwork,Qtcpserver,Qsslsocket,我已经使用QSslSocket实现了一个ssl服务器,并正确运行了它。但我有一些问题,我不能马上解决。 我认为只要将readyRead()信号连接到一个用于读取缓冲区的插槽就足够了,但我已经认识到在这种情况下readyRead()根本不会发出信号,我还必须在代码中使用waitForReadyRead()函数。但问题是使用此函数会导致阻止读取缓冲区。实际上,我想知道,当数据到达时,如何在不阻塞的情况下读取缓冲区 下面是我实现的ssl服务器: #include "sslserver.h" #incl

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

下面是我实现的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";
}
#包括“sslserver.h”
#包括
#包括
#包括
#包括
#包括
#包括
SslServer::SslServer(QObject*parent):QTcpServer(parent)
{
服务器=新的QTcpServer(此);
如果(!server->listen(QHostAddress::Any,9996))
{
qDebug()sslErrors();
对于(int i=0;isocketDescriptor());
}
void SslServer::incomingConnection(Qintpttr socketDescriptor)
{
插座=新的QSslSocket(本);
socket->setProtocol(QSsl::SslV3);
连接(套接字、信号(sslErrors(QList))、此、插槽(showErrors());
连接(套接字、信号(加密())、此、插槽(就绪());
连接(套接字、信号(readyRead())、此、插槽(readChannel());
//从文件中读取密钥
QByteArray键;
QFile密钥文件(“server.key”);
if(KeyFile.open(QIODevice::ReadOnly))
{
key=KeyFile.readAll();
KeyFile.close();
}
其他的
{
qDebug()setPrivateKey(sslKey);
//从文件加载服务器ssl证书
QByteArray证书;
QFile证书文件(“server.csr”);
if(CertFile.open(QIODevice::ReadOnly))
{
cert=CertFile.readAll();
CertFile.close();
}
其他的
{
qDebug()setLocalCertificate(sslCert);
QSslConfiguration cfg=socket->sslConfiguration();
cfg.caCertificates();
如果(!socket->setSocketDescriptor(socketDescriptor))ee
{
qDebug()启动服务器加密();
如果(套接字->isEncrypted())
发出套接字->加密();
如果(!socket->waitForEncrypted(3000)){
qDebug(“等待加密!!!!”);
返回;
}
while(true){
套接字->waitForReadyRead();
}
}
void SslServer::readChannel()
{
QByteArray qstrbytes=socket->readLine();

qDebug()我在实现另一个客户机/服务器时发现了问题,但这次是使用QTcpSocket。我不知道确切原因,但我想问题是因为使用socketDescriptor创建QSslSocket。当我使用QTcpSocket创建客户机和服务器时,它们在没有任何事件循环的情况下工作得很好,并且只有通过连接readyRead()才能工作向插槽发送信号。之后,为了测试某些情况,我使用socketDescriptor创建了QTcpSocket。然后我发现问题在于使用socketDescriptor创建套接字,因为这一次readyRead()信号不像以前那样工作。

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