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:

Qt Code:
  1. ConnectionHandler::ConnectionHandler(QString ip, int port, QObject *parent) : QObject(parent) {
  2. connect(this->socket, SIGNAL(connected()), this, SLOT(connected()));
  3. connect(this->socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
  4. connect(this->socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
  5. connect(this->socket, SIGNAL(encrypted()), this, SLOT(encryptedReady()));
  6. connect(this->socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(SSLerrorOccured(const QList<QSslError> &)));
  7.  
  8. this->ip = ip;
  9. this->port = port;
  10. }
  11.  
  12.  
  13. void ConnectionHandler::encryptedReady() {
  14. qDebug() << "READY";
  15.  
  16. }
  17.  
  18.  
  19. void ConnectionHandler::SSLerrorOccured(const QList<QSslError> &) {
  20. qDebug() << "EEROR";
  21. }
  22.  
  23.  
  24. void ConnectionHandler::connectToServer() {
  25. // this->socket->connectToHost(this->ip, this->port);
  26. this->socket->connectToHostEncrypted(this->ip, this->port);
  27.  
  28. if (!this->socket->waitForConnected(5000)) {
  29. this->socket->close();
  30. this->errorMsg = this->socket->errorString();
  31. }
  32. }
  33.  
  34.  
  35. void ConnectionHandler::connected() {
  36. qDebug() << "connected";
  37. this->connectedHostAddress = this->socket->peerAddress().toString();
  38. this->connectionEstablished = true;
  39. this->localIP = this->socket->localAddress().toString();
  40. this->localPort = this->socket->localPort();
  41. }
  42.  
  43. void ConnectionHandler::disconnected() {
  44. this->connectionEstablished = false;
  45. this->socket->close();
  46.  
  47. this->errorMsg = "Connection to server no longer available";
  48. emit hostDisconnected();
  49. }
To copy to clipboard, switch view to plain text mode 


And the one for the server

Qt Code:
  1. void ClientHandler::run() {
  2.  
  3. if (!this->fd)
  4. return;
  5.  
  6. this->socket = new QSslSocket;
  7.  
  8. connect(this->socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
  9. connect(this->socket, SIGNAL(disconnected()), this, SLOT(disconnected()), Qt::DirectConnection);
  10. connect(this->socket, SIGNAL(encrypted()), this, SLOT(encryptedReady()));
  11. connect(this->socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(SSLerrorOccured(const QList<QSslError> &)));
  12.  
  13. if (!this->socket->setSocketDescriptor(this->fd)) {
  14. emit error(socket->error());
  15. return;
  16. } else {
  17. this->socket->startServerEncryption();
  18. }
  19.  
  20. this->peerIP = socket->peerAddress().toString();
  21. QString tmp;
  22. tmp.append(QString("%1").arg(socket->peerPort()));
  23. this->peerPort = tmp;
  24.  
  25. QHostInfo::lookupHost(this->peerIP, this, SLOT(lookedUp(QHostInfo)));
  26. }
  27.  
  28.  
  29. void ClientHandler::SSLerrorOccured(const QList<QSslError> &) {
  30. qDebug() << "EEROR";
  31. }
  32.  
  33.  
  34. void ClientHandler::encryptedReady() {
  35. qDebug() << "READY";
  36.  
  37. }
To copy to clipboard, switch view to plain text mode 

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??