gpt4 book ai didi

c++ - 使用 Qt 编写安全的服务器应用程序

转载 作者:太空宇宙 更新时间:2023-11-04 16:15:47 26 4
gpt4 key购买 nike

我已经按照教程“使用 Qt 编写安全服务器应用程序”(https://blog.quickmediasolutions.com/2012/09/18/writing-secure-server-applications-with-qt.html)进行操作,但出现错误:

qt error

CSampleServer.h

#ifndef CSAMPLESERVER_H
#define CSAMPLESERVER_H

#include <QTcpServer>

class CSampleServer : public QTcpServer
{
public:
CSampleServer();

private:
void incomingConnection(int);

signals:
void StatusMessage(QString);
};

#endif // CSAMPLESERVER_H

CSampleServer.cpp

#include "CSampleServer.h"
#include "CSecureSocket.h"

CSampleServer::CSampleServer()
{
}

void CSampleServer::incomingConnection(int handle)
{
CSecureSocket * socket = new CSecureSocket(this);
connect(socket, SIGNAL(StatusMessage(QString)), SIGNAL(StatusMessage(QString)));
socket->Process(handle);
}

CSecureSocket.h

#ifndef CSECURESOCKET_H
#define CSECURESOCKET_H

#include <QSslSocket>

class CSecureSocket : public QSslSocket
{
public:
CSecureSocket();
void Process(int);


signals:
void StatusMessage(QString);
};

#endif // CSECURESOCKET_H

CSecureSocket.cpp

#include "CSecureSocket.h"
#include <QHostAddress>

CSecureSocket::CSecureSocket()
{
}

void CSecureSocket::Process(int handle)
{
/* Emit a message indicating the address of the client. */
setSocketDescriptor(handle);
emit StatusMessage(tr("Incoming connection from %1.").arg(peerAddress().toString()));

/* Set the certificate and private key. */
setLocalCertificate("server.crt");
setPrivateKey("server.key.insecure");

/* Start the server encryption process and wait for it to complete. */
startServerEncryption();
if(waitForEncrypted())
{
emit StatusMessage("Session with client is now encrypted.");

/* Respond with a basic HTTP message. */
write("HTTP/1.1 200 OK\r\n"
"Content-type: text/plain\r\n"
"Content-length: 12\r\n"
"\r\n"
"Hello World!");

emit StatusMessage("Message sent to client. Closing connection.");
}
else
emit StatusMessage(tr("An error occurred: %1.").arg(errorString()));

/* Close the connection once the data is written. */
disconnectFromHost();
}

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "CSampleServer.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

private slots:
void OnStart();
void OnStatusMessage(QString);

private:
Ui::MainWindow *ui;
CSampleServer m_server;
};

#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&m_server, SIGNAL(StatusMessage(QString)), SLOT(OnStatusMessage(QString)));
connect(ui->pushButton, SIGNAL(clicked()), SLOT(OnStart()));
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::OnStart()
{
quint16 port = ui->spinBox->value();
if(m_server.listen(QHostAddress::Any, port))
OnStatusMessage(tr("Listening on port %1...").arg(port));
else
OnStatusMessage(tr("Unable to listen on port %1.").arg(port));
}

void MainWindow::OnStatusMessage(QString message)
{
ui->plainTextEdit->appendPlainText(message);
}

你能帮帮我吗?提前致谢!

最佳答案

您可以使用 SSL 创建安全服务器。这是一个使用 QSsLSocket 的示例服务器:

#include <QtNetwork>
#include <QMessageBox>

class server : public QTcpServer
{
Q_OBJECT
public:
explicit server(QObject *parent = 0);
~server();

QSslSocket server_socket;

public slots:


void tcpReady();

void encrypted();

void sslError( QList<QSslError> errors );

bool start_listen(int port_no);

protected:
void incomingConnection( int descriptor );

};

server::server(QObject *parent) :
QTcpServer(parent)
{

server_socket.setProtocol(QSsl::SslV3);

QByteArray key;
QByteArray cert;

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

QFile file_cert("server.crt");
if(file_cert.open(QIODevice::ReadOnly))
{
cert = file_cert.readAll();
file_cert.close();
}
else
{
qDebug() << file_cert.errorString();
}


QSslKey ssl_key(key, QSsl::Rsa,QSsl::Pem,QSsl::PrivateKey,"server");

QSslCertificate ssl_cert(cert);

server_socket.addCaCertificate(ssl_cert);
server_socket.setLocalCertificate(ssl_cert);
server_socket.setPrivateKey(ssl_key);


connect( &server_socket, SIGNAL(sslErrors(QList<QSslError>)),
this, SLOT(sslError(QList<QSslError>)) );

connect(&server_socket,SIGNAL(encrypted()),this,SLOT(encrypted()));

server_socket.setSocketOption(QAbstractSocket::KeepAliveOption, true );
}

server::~server()
{
server_socket.disconnectFromHost();
server_socket.waitForDisconnected();
}

void server::tcpReady()
{
QByteArray array = server_socket.read( server_socket.bytesAvailable() );
//...
}

void server::encrypted()
{
connect( &server_socket, SIGNAL(readyRead()),
this, SLOT(tcpReady()) );
emit connection_established();
}

void server::sslError(QList<QSslError> errors)
{
QString erroStr="";
foreach (const QSslError &e, errors)
erroStr.append(e.errorString()).append("\n");

QMessageBox::warning( (QWidget *)this->parent(), tr("Error"),erroStr );

server_socket.ignoreSslErrors();
}


bool server::start_listen(int port_no)
{
if( !this->listen( QHostAddress::Any, port_no ) )
{
QMessageBox::warning( (QWidget *)this->parent(), tr("Error!"), tr("Cannot listen to port %1").arg(port_no) );

}
else
return true;
}

void server::incomingConnection(int descriptor)
{
if(server_socket.state()!=QAbstractSocket::UnconnectedState)
return;
if( !server_socket.setSocketDescriptor( descriptor ) )
{
QMessageBox::warning( (QWidget *)this->parent(), tr("Error!"), tr("Socket error!") );
return;
}
else
{
server_socket.startServerEncryption();
}
}

您应该为您的应用程序生成一个 server.key 和一个 server.crt 文件。

关于c++ - 使用 Qt 编写安全的服务器应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22858978/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com