Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSslSocket and QThread
Qt 6.12 LTS is out! See what's new in the release blog

QSslSocket and QThread

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi,

    I am trying to get a QSslSocket to work in QThread but I cant seem to get it to work.
    The following is the code :
    @
    // Local includes
    #include "main.h"

    // Constructor of the timer class
    Timer::Timer(QObject *parent) : QObject(parent)
    {
    qDebug() << "inConstrutor";
    QTimer *timer = new QTimer();
    connect(timer, SIGNAL(timeout()), this, SLOT(startBackup()));
    timer->start(5000);
    }

    // Start the backup
    int Timer::startBackup(){

    qDebug() << "inStart" << backingUP;
    //return 0;
    
    if(backingUP){
        return 2;
    }
    
    // Start the backup thread
    Backup *backup = new Backup();
    
    // Make a connection for deleting this later when done
    //connect(backup, SIGNAL(finished()), backup, SLOT(deleteLater()));
    
    // Set that we are running ATM
    backingUP = true;
    
    backup->start();
    
    return 1;
    

    }

    // The constructor
    Backup::Backup(QObject *parent)
    : QThread(parent)
    {

    // This thread can never be terminated
    //this->setTerminationEnabled(false);
    

    }

    // The function that does everything
    void Backup::run()
    {

    this->doit();
    

    }

    void Backup::doit(){

    qDebug() << "Doit";
    
    socket = new QSslSocket;
    
    connect(socket, SIGNAL(readyRead()),
            this, SLOT(socketReadyRead()));
    connect(socket, SIGNAL(sslErrors(QList<QSslError>)),
            this, SLOT(sslErrors(QList<QSslError>)));
    connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
            this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
    connect(socket, SIGNAL(encrypted()),
            this, SLOT(socketEncrypted()));
    
    // Connect to the server
    socket->connectToHostEncrypted(NSettings->value("hostname").toString(), NSettings->value("port").toInt());
    
    // Wait for it to reach the encryted and connected state
    if (!socket->waitForEncrypted()) {
        qDebug() << "waitForEncrypted Error";
        return; // An error occured
    }
    
    // This is the protocol
    socket->write("ENTERHEADER\r\n");
    socket->waitForBytesWritten();
    
    QByteArray username = NSettings->value("user").toByteArray();
    QByteArray device = NSettings->value("device").toByteArray();
    
    // We first need to LOGIN
    this->rite("L"+ username.toBase64() +" "+ device.toBase64());
    socket->waitForBytesWritten();
    

    }

    // Called whenever there is something to READ in the buffer
    void Backup::socketReadyRead(){

    qDebug() << socket->bytesAvailable();
    QString str = socket->readAll();
    qDebug() << str;
    
    bool unconnected = !socket || socket->state() == QAbstractSocket::UnconnectedState;
    
    if(str == "LOGIN SUCCESSFUL"){
    
        for(int i = 1; i <= 1000; i++){
    
            // Was the login successful ?
            if(unconnected){
                qDebug() << socket->state();
                return;
            }
    
            this->rite("STORE ABC\r\n");
            socket->waitForBytesWritten();
        }
    
    }
    
    qDebug() << "End READ";
    
    // Disconnect from the server
    //socket->disconnectFromHost();
    

    }

    // Called when the connection is fully established
    void Backup::socketEncrypted()
    {
    qDebug() << "Connection Established";
    }

    // Called when there are SSL errors
    void Backup::sslErrors(const QList<QSslError> &errors)
    {
    foreach (const QSslError &error, errors){
    qDebug() << error.errorString();
    }

    // Ignore Errors
    socket->ignoreSslErrors();
    

    }

    // Whenever there is a change in the connection STATE, this is called
    void Backup::socketStateChanged(QAbstractSocket::SocketState state)
    {
    qDebug() << state;
    }

    // Socket write safe as it appends the length to the beginning of the string
    qint64 Backup::rite(const QByteArray & data)
    {
    return socket->write(data);
    }
    @

    When I run this I get the following error :
    @inConstrutor
    inStart false
    Doit
    QObject::connect: Cannot queue arguments of type 'QAbstractSocket::SocketState'
    (Make sure 'QAbstractSocket::SocketState' is registered using qRegisterMetaType().)
    waitForEncrypted Error
    @

    If I dont call the backup->start(); and call the backup->doit(); then the code works perfectly.
    I would really be happy, if someone can shed any light on what I am doing wrong.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt
    • Unsolved