Results 1 to 2 of 2

Thread: QSslSocket closes connection

  1. #1
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    1

    Default QSslSocket closes connection

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

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: QSslSocket closes connection

    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?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Replies: 0
    Last Post: 11th November 2011, 19:18
  2. Last dialog closes app
    By been_1990 in forum Qt Programming
    Replies: 2
    Last Post: 12th April 2010, 15:17
  3. Replies: 1
    Last Post: 2nd April 2010, 06:42
  4. Replies: 1
    Last Post: 15th November 2009, 22:14
  5. Dialog closes on pressing Esc key?
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 3rd July 2008, 08:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.