|
|
Source code |
1 2 |
QByteArray data = mClientSocket->readLine(10);
qDebug() << data;
|
|
|
Source code |
1 2 3 |
mSslSocket->write("Login\r\n", 10);
mSslSocket->flush();
mSslSocket->waitForBytesWritten(1000);
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
#ifndef SERVER_H
#define SERVER_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "clientconnection.h"
static const int port = 9986;
class Server : public QTcpServer
{
Q_OBJECT
public:
Server() : QTcpServer() {}
virtual ~Server() {}
void startServer();
protected:
virtual void incomingConnection( int socketDescriptor );
};
#endif // SERVER1_H
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include "server.h"
void Server::startServer()
{
if(!this->listen(QHostAddress::Any, port))
{
qDebug() << "Server Startup Failed";
}
else
{
qDebug() << "Server Startup Succeded";
}
}
void Server::incomingConnection( int socketDescriptor )
{
ClientConnection *clientConnection = new ClientConnection();
clientConnection->incomingConnection(socketDescriptor);
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#ifndef CLIENTCONNECTION_H
#define CLIENTCONNECTION_H
#include
#include
#include
#include
#include
#include
#include
class ClientConnection : public QObject
{
Q_OBJECT
public:
explicit ClientConnection(QObject *parent = 0);
void incomingConnection(int socketDescriptor);
private:
QSslSocket *mClientSocket;
private slots:
void readyRead();
void encrypted();
void sslModeChanged( QSslSocket::SslMode );
void sslErrors( const QList& );
void disconnected();
void connected();
void error( QAbstractSocket::SocketError );
void stateChanged( QAbstractSocket::SocketState );
};
#endif // CLIENTCONNECTION_H
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
#include "clientconnection.h"
#include <QBuffer>
ClientConnection::ClientConnection(QObject *parent) :
QObject(parent)
{
}
void ClientConnection::incomingConnection(int socketDescriptor)
{
qDebug() << "MySslServer::incomingConnection(" << socketDescriptor << ")";
mClientSocket = new QSslSocket( this );
connect( mClientSocket, SIGNAL(readyRead()), this, SLOT(readyRead()) );
connect( mClientSocket, SIGNAL(encrypted()), this, SLOT(encrypted()) );
connect( mClientSocket, SIGNAL(modeChanged(QSslSocket::SslMode)), this, SLOT(sslModeChanged(QSslSocket::SslMode)) );
connect( mClientSocket, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(sslErrors(const QList<QSslError>&)) );
connect( mClientSocket, SIGNAL(disconnected()), this, SLOT(disconnected()) );
connect( mClientSocket, SIGNAL(connected()), this, SLOT(connected()) );
connect( mClientSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)) );
connect( mClientSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this,
SLOT(stateChanged(QAbstractSocket::SocketState)) );
QByteArray key;
QByteArray cert;
QFile fileKey(":/ssl/Resources/localhost.key");
if(fileKey.open(QIODevice::ReadOnly))
{
key = fileKey.readAll();
fileKey.close();
}
else
{
qDebug() << fileKey.errorString();
}
QFile fileCert(":/ssl/Resources/localhost.cert");
if(fileCert.open(QIODevice::ReadOnly))
{
cert = fileCert.readAll();
fileCert.close();
}
else
{
qDebug() << fileCert.errorString();
}
QSslKey sslKey(key, QSsl::Rsa);
QSslCertificate sslCert(cert);
mClientSocket->setLocalCertificate(sslCert);
mClientSocket->setPrivateKey(sslKey);
#if 0
qDebug() << " Supported ciphers:";
foreach( QSslCipher cipher, QSslSocket::supportedCiphers() )
qDebug() << cipher.name();
qDebug() << " Default ciphers:";
foreach( QSslCipher cipher, QSslSocket::defaultCiphers() )
qDebug() << cipher.name();
#endif
if( mClientSocket->setSocketDescriptor( socketDescriptor ) )
{
qDebug() << "sslSocket mode: " << (int)mClientSocket->mode();
qDebug() << "sslSocket state:" << (int)mClientSocket->state();
}
else
{
qWarning() << "Couldn't setSocketDescriptor(" << socketDescriptor << ") for this connection";
delete mClientSocket;
}
}
void ClientConnection::encrypted()
{
qDebug() << "encrypted()";
qDebug() << "Calling sslSocket->startServerEncryption()";
mClientSocket->startServerEncryption();
}
void ClientConnection::sslModeChanged( QSslSocket::SslMode mode )
{
qDebug() << "sslModeChanged(" << (int)mode << ")";
}
void ClientConnection::sslErrors( const QList<QSslError> &errList )
{
qDebug() << "sslErrors(";
foreach( QSslError err, errList )
qDebug() << " " << err.errorString();
qDebug() << ")";
}
void ClientConnection::disconnected()
{
qDebug() << "disconnected()";
}
void ClientConnection::connected()
{
qDebug() << "connected()";
}
void ClientConnection::error( QAbstractSocket::SocketError err )
{
QSslSocket *socket = qobject_cast<QSslSocket*>( sender() );
Q_ASSERT( socket );
qDebug() << "error(" << (int)err << socket->errorString() << ")";
}
void ClientConnection::stateChanged( QAbstractSocket::SocketState state )
{
qDebug() << "stateChanged(" << (int)state << ")";
}
void ClientConnection::readyRead()
{
/*quint16 blockSize;
QDataStream in(mClientSocket);
in.setVersion(QDataStream::Qt_4_0);
//if (mClientSocket->bytesAvailable() < (int)sizeof(quint16))
// return;
in >> blockSize;
//if (mClientSocket->bytesAvailable() < blockSize)
//return;
QString packet;
in >> packet;
qDebug() << packet;*/
QByteArray data = mClientSocket->readLine(10);
qDebug() << data;
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#ifndef LOGIN_H
#define LOGIN_H
#include
#include
namespace Ui {
class Login;
}
class Login : public QWidget
{
Q_OBJECT
public:
explicit Login(QWidget *parent = 0);
~Login();
private:
Ui::Login *ui;
QSslSocket *mSslSocket;
void createSocket();
private slots:
void loginButtonClicked();
void connectedToServer();
void connectionErrors(QAbstractSocket::SocketError err);
};
#endif // LOGIN_H
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
#include
#include "login.h"
#include "ui_login.h"
Login::Login(QWidget *parent) :
QWidget(parent),
ui(new Ui::Login)
{
ui->setupUi(this);
QDesktopWidget *desktop = QApplication::desktop();
setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint & Qt::WindowMinimizeButtonHint);
int screenWidth, width;
int screenHeight, height;
int x, y;
QSize windowSize;
screenWidth = desktop->width(); // get width of screen
screenHeight = desktop->height(); // get height of screen
windowSize = size(); // size of our application window
width = windowSize.width();
height = windowSize.height();
// little computations
x = (screenWidth - width) / 2;
y = (screenHeight - height) / 2;
y -= 50;
// move window to desired coordinates
move ( x, y );
createSocket();
connect(ui->loginButton, SIGNAL(clicked()), this, SLOT(loginButtonClicked()));
connect(mSslSocket, SIGNAL(connected()), this, SLOT(connectedToServer()));
connect(mSslSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectionErrors(QAbstractSocket::SocketError)));
}
Login::~Login()
{
delete ui;
}
void Login::createSocket()
{
mSslSocket = new QSslSocket();
}
void Login::loginButtonClicked()
{
mSslSocket->connectToHostEncrypted("localhost", 9986);
}
void Login::connectedToServer()
{
ui->infoLabel->setStyleSheet("QLabel { color : black; }");
ui->infoLabel->setText("Connected");
//QString input = ui->usernameBox->text();
//input.append('\n');
mSslSocket->write("Login\r\n", 10);
mSslSocket->flush();
mSslSocket->waitForBytesWritten(1000);
}
void Login::connectionErrors(QAbstractSocket::SocketError err)
{
ui->infoLabel->setText( mSslSocket->errorString());
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 |
void SslServer::incomingConnection(int socketDescriptor)
{
QSslSocket *serverSocket = new QSslSocket;
if (serverSocket->setSocketDescriptor(socketDescriptor)) {
connect(serverSocket, SIGNAL(encrypted()), this, SLOT(ready()));
serverSocket->startServerEncryption();
} else {
delete serverSocket;
}
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
void ClientConnection::incomingConnection(int socketDescriptor)
{
qDebug() << "Incoming Connection(" << socketDescriptor << ")";
mClientSocket = new QSslSocket( this );
if(mClientSocket->setSocketDescriptor(socketDescriptor))
{
connect(mClientSocket, SIGNAL(encrypted()), this, SLOT(encrypted()));
connect( mClientSocket, SIGNAL(readyRead()), this, SLOT(readyRead()) );
connect( mClientSocket, SIGNAL(modeChanged(QSslSocket::SslMode)), this, SLOT(sslModeChanged(QSslSocket::SslMode)) );
connect( mClientSocket, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(sslErrors(const QList<QSslError>&)) );
connect( mClientSocket, SIGNAL(disconnected()), this, SLOT(disconnected()) );
connect( mClientSocket, SIGNAL(connected()), this, SLOT(connected()) );
connect( mClientSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)) );
connect( mClientSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this,
SLOT(stateChanged(QAbstractSocket::SocketState)) );
mClientSocket->setProtocol(QSsl::AnyProtocol);
mClientSocket->setCiphers("DHE-RSA-AES256-SHA:DHE-DSS-AES256-SHA:AES256-SHA");
mClientSocket->startServerEncryption();
}
else
{
delete mClientSocket;
return;
}
QByteArray key;
QByteArray cert;
QFile fileKey(":/ssl/Resources/localhost.key");
if(fileKey.open(QIODevice::ReadOnly))
{
key = fileKey.readAll();
fileKey.close();
}
else
{
qDebug() << fileKey.errorString();
}
QFile fileCert(":/ssl/Resources/localhost.cert");
if(fileCert.open(QIODevice::ReadOnly))
{
cert = fileCert.readAll();
fileCert.close();
}
else
{
qDebug() << fileCert.errorString();
}
QSslKey sslKey(key, QSsl::Rsa);
QSslCertificate sslCert(cert);
mClientSocket->setLocalCertificate(sslCert);
mClientSocket->setPrivateKey(sslKey);
}
void ClientConnection::encrypted()
{
qDebug() << "Encrypted";
}
void ClientConnection::sslModeChanged( QSslSocket::SslMode mode )
{
}
void ClientConnection::sslErrors( const QList<QSslError> &errList )
{
qDebug() << "sslErrors";
foreach( QSslError err, errList )
qDebug() << " " << err.errorString();
qDebug() << "";
}
void ClientConnection::disconnected()
{
qDebug() << "Client Disconnected";
}
void ClientConnection::connected()
{
qDebug() << "Client Connected";
}
void ClientConnection::error( QAbstractSocket::SocketError err )
{
qDebug() << "error(" << (int)err << mClientSocket->errorString() << ")";
}
void ClientConnection::stateChanged( QAbstractSocket::SocketState state )
{
}
void ClientConnection::readyRead()
{
QByteArray data = mClientSocket->readLine(10);
qDebug() << data;
}
|
|
|
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
Login::Login(QWidget *parent) :
QWidget(parent),
ui(new Ui::Login)
{
ui->setupUi(this);
QDesktopWidget *desktop = QApplication::desktop();
setWindowFlags(this->windowFlags() & ~Qt::WindowContextHelpButtonHint & Qt::WindowMinimizeButtonHint);
int screenWidth, width;
int screenHeight, height;
int x, y;
QSize windowSize;
screenWidth = desktop->width(); // get width of screen
screenHeight = desktop->height(); // get height of screen
windowSize = size(); // size of our application window
width = windowSize.width();
height = windowSize.height();
// little computations
x = (screenWidth - width) / 2;
y = (screenHeight - height) / 2;
y -= 50;
// move window to desired coordinates
move ( x, y );
createSocket();
connect(ui->loginButton, SIGNAL(clicked()), this, SLOT(loginButtonClicked()));
connect(mSslSocket, SIGNAL(connected()), this, SLOT(connectedToServer()));
connect(mSslSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(connectionErrors(QAbstractSocket::SocketError)));
connect(mSslSocket, SIGNAL(encrypted()), this, SLOT(encrypted()));
}
Login::~Login()
{
delete ui;
}
void Login::createSocket()
{
mSslSocket = new QSslSocket(this);
mSslSocket->setCiphers("DHE-RSA-AES256-SHA:DHE-DSS-AES256-SHA:AES256-SHA");
}
void Login::loginButtonClicked()
{
mSslSocket->connectToHostEncrypted("localhost", 9986);
}
void Login::connectedToServer()
{
ui->infoLabel->setText("Connected");
}
void Login::encrypted()
{
ui->infoLabel->setText("Encrypted");
mSslSocket->write("Hey\r\n\r\n");
}
void Login::connectionErrors(QAbstractSocket::SocketError err)
{
ui->infoLabel->setText( mSslSocket->errorString());
}
|