PDA

View Full Version : QSslSocket closes connection



Cerberus
17th October 2015, 15:00
I have an client-server application that currently works with QTcpSocket which I wanted to migrate to QSslSocket. I followed the instructions on http://doc.qt.io/qt-5/qsslsocket.html
That is the code for the client I have:



ConnectionHandler::ConnectionHandler(QString ip, int port, QObject *parent) : QObject(parent) {
connect(this->socket, SIGNAL(connected()), this, SLOT(connected()));
connect(this->socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
connect(this->socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(this->socket, SIGNAL(encrypted()), this, SLOT(encryptedReady()));
connect(this->socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(SSLerrorOccured(const QList<QSslError> &)));

this->ip = ip;
this->port = port;
}


void ConnectionHandler::encryptedReady() {
qDebug() << "READY";

}


void ConnectionHandler::SSLerrorOccured(const QList<QSslError> &) {
qDebug() << "EEROR";
}


void ConnectionHandler::connectToServer() {
// this->socket->connectToHost(this->ip, this->port);
this->socket->connectToHostEncrypted(this->ip, this->port);

if (!this->socket->waitForConnected(5000)) {
this->socket->close();
this->errorMsg = this->socket->errorString();
}
}


void ConnectionHandler::connected() {
qDebug() << "connected";
this->connectedHostAddress = this->socket->peerAddress().toString();
this->connectionEstablished = true;
this->localIP = this->socket->localAddress().toString();
this->localPort = this->socket->localPort();
}

void ConnectionHandler::disconnected() {
this->connectionEstablished = false;
this->socket->close();

this->errorMsg = "Connection to server no longer available";
emit hostDisconnected();
}




And the one for the server




void ClientHandler::run() {

if (!this->fd)
return;

this->socket = new QSslSocket;

connect(this->socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
connect(this->socket, SIGNAL(disconnected()), this, SLOT(disconnected()), Qt::DirectConnection);
connect(this->socket, SIGNAL(encrypted()), this, SLOT(encryptedReady()));
connect(this->socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(SSLerrorOccured(const QList<QSslError> &)));

if (!this->socket->setSocketDescriptor(this->fd)) {
emit error(socket->error());
return;
} else {
this->socket->startServerEncryption();
}

this->peerIP = socket->peerAddress().toString();
QString tmp;
tmp.append(QString("%1").arg(socket->peerPort()));
this->peerPort = tmp;

QHostInfo::lookupHost(this->peerIP, this, SLOT(lookedUp(QHostInfo)));
}


void ClientHandler::SSLerrorOccured(const QList<QSslError> &) {
qDebug() << "EEROR";
}


void ClientHandler::encryptedReady() {
qDebug() << "READY";

}




The connection is established and I get the output when the conneted() method is called in the client. But then the connection is disconnected because of any reason and the disconnected method is called!?
Am I missing here something??

jefftee
21st October 2015, 01:49
Does your ClientHandler class subclass QThread? If so, when that thread is started and ClientHandler::run is executed, it looks like you end that method without really doing anything with the socket. I would think your thread should continue to run while it interacts with the client, no?