我已经按照“使用Qt编写安全服务器应用程序”教程(https://blog.quickmediasolutions.com/2012/09/18/writing-secure-server-applications-with-qt.html)但我有错误:
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();
}
MainWindow.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
MainWindow.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);
}
你能帮帮我吗?提前谢谢!
答案 0 :(得分:3)
您可以使用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文件。
答案 1 :(得分:1)
此用法:
CSecureSocket * socket = new CSecureSocket(this);
与此声明/定义不兼容:
CSecureSocket();
尝试:
CSecureSocket* socket = new CSecureSocket();
...
答案 2 :(得分:0)
问题是没有为CSampleServer和CSecureSocket类生成moc_ *文件。
要使该示例正常工作,您需要:
答案 3 :(得分:0)
1.void CSampleServer :: incomingConnection(int handle) 参数应该是qintptr而不是int。
2.connect(套接字,SIGNAL(StatusMessage(QString)),SIGNAL(StatusMessage(QString))));声明是错误的接收者对象在哪里
3.void CSecureSocket :: Process(int handle)使用qintptr
4。要正确阅读文档还有更多错误