【发布时间】:2014-05-16 12:59:56
【问题描述】:
我已经按照教程“使用 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);
}
你能帮帮我吗?提前致谢!
【问题讨论】:
-
隐式显示相关代码怎么样?
-
我已经用我的代码更新了帖子
-
为什么不用QSSLSocket?
-
嗯好主意!是的。 tnx!