hi,
a few days before, I have asked similar.
with the code below the client doesen't get the data with bad or good password.
the thread.cpp for the client:
#include "netthread.h"
{
this->hostName = hostName;
this->port = port;
this->passw = passw;
}
void NetThread::run()
{
mutex.lock();
quint16 serverPort = port;
mutex.unlock();
while(!quit)
{
const int Timeout = 5 * 1000;
qDebug() << serverName;
qDebug() << serverPort;
sslsocket = new QSslSocket;
sslsocket->connectToHostEncrypted(serverName, serverPort);
sslsocket->ignoreSslErrors();
if(!sslsocket->waitForEncrypted())
{
emit error(sslsocket->error(), sslsocket->errorString());
return;
}
sslsocket->write(passw);
qDebug() << sslsocket->bytesToWrite();
sslsocket->flush();
while (sslsocket->bytesAvailable() < (int)sizeof(quint16))
{
if (!sslsocket->waitForReadyRead(Timeout))
{
emit error(sslsocket->error(), sslsocket->errorString());
return;
}
}
quint16 blockSize;
in >> blockSize;
while(sslsocket->bytesAvailable() < blockSize)
{
if (!sslsocket->waitForReadyRead(Timeout))
{
emit error(sslsocket->error(), sslsocket->errorString());
return;
}
}
in >> logfile;
qDebug() << "NetThread: " << logfile.data();
emit newNet(logfile);
cond.wait(&mutex);
serverName = hostName;
serverPort = port;
}
}
#include "netthread.h"
NetThread::NetThread(const QString &hostName, quint16 port, QByteArray passw, QObject *parent) : QThread(parent), quit(false)
{
this->hostName = hostName;
this->port = port;
this->passw = passw;
QMutexLocker locker(&mutex);
}
void NetThread::run()
{
mutex.lock();
QString serverName = hostName;
quint16 serverPort = port;
mutex.unlock();
while(!quit)
{
const int Timeout = 5 * 1000;
qDebug() << serverName;
qDebug() << serverPort;
sslsocket = new QSslSocket;
sslsocket->connectToHostEncrypted(serverName, serverPort);
sslsocket->ignoreSslErrors();
if(!sslsocket->waitForEncrypted())
{
emit error(sslsocket->error(), sslsocket->errorString());
return;
}
sslsocket->write(passw);
qDebug() << sslsocket->bytesToWrite();
sslsocket->flush();
while (sslsocket->bytesAvailable() < (int)sizeof(quint16))
{
if (!sslsocket->waitForReadyRead(Timeout))
{
emit error(sslsocket->error(), sslsocket->errorString());
return;
}
}
quint16 blockSize;
QDataStream in(sslsocket);
in.setVersion(QDataStream::Qt_4_0);
in >> blockSize;
while(sslsocket->bytesAvailable() < blockSize)
{
if (!sslsocket->waitForReadyRead(Timeout))
{
emit error(sslsocket->error(), sslsocket->errorString());
return;
}
}
QMutexLocker locker(&mutex);
QByteArray logfile;
in >> logfile;
qDebug() << "NetThread: " << logfile.data();
emit newNet(logfile);
cond.wait(&mutex);
serverName = hostName;
serverPort = port;
}
}
To copy to clipboard, switch view to plain text mode
and the thread.cpp for the server:
#include "thread.h"
SslThread
::SslThread(int socketDescriptor,
QString &log,
QObject *parent
) : QThread(parent
), socketDescriptor
(socketDescriptor
){
logfile
= new QFile(log);
pass_control = ":passwort";
security = false;
}
void SslThread::run()
{
serverSocket = new QSslSocket();
connect(serverSocket, SIGNAL(readyRead()), this, SLOT(lesen()));
while(true)
{
serverSocket->setLocalCertificate("server.cert.crt");
serverSocket->setPrivateKey("privkey.pem",QSsl::Rsa,QSsl::Pem,pass);
if(serverSocket->setSocketDescriptor(socketDescriptor))
{
connect(serverSocket, SIGNAL(encrypted()), this, SLOT(ready()));
serverSocket->startServerEncryption();
}
else
{
emit error(serverSocket->error());
delete serverSocket;
return;
}
qDebug() << serverSocket->peerAddress();
if(security)
{
qDebug() << "I'm inside";
{
qDebug() << "Lese Fehler!";
exit();
}
out << (quint16)0;
out << log.data();
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
serverSocket->write(block);
qDebug() << serverSocket->bytesToWrite();
serverSocket->flush();
}
serverSocket->waitForDisconnected();
serverSocket->disconnectFromHost();
}
}
void SslThread::ready()
{
qDebug() << "Connection encrypted!";
}
char* SslThread
::toChar(QString &string
) {
char *c_str2 = ba.data();
return c_str2;
}
void SslThread::lesen()
{
//qDebug() << line;
if(line.startsWith(':'))
{
qDebug() << "password: " << line;
if(line != pass_control)
{
security = false;
//serverSocket->abort();
qDebug() << "password: " << line << "control: " << pass_control;
}
else
{
security = true;
}
}
//...
}
#include "thread.h"
SslThread::SslThread(int socketDescriptor, QString &log, QObject *parent) : QThread(parent), socketDescriptor(socketDescriptor)
{
logfile = new QFile(log);
pass_control = ":passwort";
security = false;
}
void SslThread::run()
{
serverSocket = new QSslSocket();
connect(serverSocket, SIGNAL(readyRead()), this, SLOT(lesen()));
while(true)
{
QByteArray pass("certificatpasswort");
serverSocket->setLocalCertificate("server.cert.crt");
serverSocket->setPrivateKey("privkey.pem",QSsl::Rsa,QSsl::Pem,pass);
if(serverSocket->setSocketDescriptor(socketDescriptor))
{
connect(serverSocket, SIGNAL(encrypted()), this, SLOT(ready()));
serverSocket->startServerEncryption();
}
else
{
emit error(serverSocket->error());
delete serverSocket;
return;
}
qDebug() << serverSocket->peerAddress();
if(security)
{
qDebug() << "I'm inside";
if(!(logfile->open(QIODevice::ReadOnly | QIODevice::Text)))
{
qDebug() << "Lese Fehler!";
exit();
}
QByteArray log = logfile->readAll();
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << log.data();
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
serverSocket->write(block);
qDebug() << serverSocket->bytesToWrite();
serverSocket->flush();
}
serverSocket->waitForDisconnected();
serverSocket->disconnectFromHost();
}
}
void SslThread::ready()
{
qDebug() << "Connection encrypted!";
}
char* SslThread::toChar(QString &string)
{
QByteArray ba = string.toLatin1();
char *c_str2 = ba.data();
return c_str2;
}
void SslThread::lesen()
{
QByteArray line = serverSocket->readAll();
//qDebug() << line;
if(line.startsWith(':'))
{
qDebug() << "password: " << line;
if(line != pass_control)
{
security = false;
//serverSocket->abort();
qDebug() << "password: " << line << "control: " << pass_control;
}
else
{
security = true;
}
}
//...
}
To copy to clipboard, switch view to plain text mode
the server tells me
moon:/home/lard/Server# ./Server
QHostAddress( "127.0.0.1" )
Connection encrypted!
password: "passwort"
Password true
the server is never inside if(security) so he doesn't send the data.
and the client:
network operation timeout
i have tested much more versions of this, but i can't find a solution.
can someone please help?
mate
Bookmarks