Results 1 to 14 of 14

Thread: QSslSocket not working in QThread

  1. #1
    Join Date
    Dec 2012
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QSslSocket not working in QThread

    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 :
    Qt Code:
    1. // Local includes
    2. #include "main.h"
    3.  
    4. // Constructor of the timer class
    5. Timer::Timer(QObject *parent) : QObject(parent)
    6. {
    7. qDebug() << "inConstrutor";
    8. QTimer *timer = new QTimer();
    9. connect(timer, SIGNAL(timeout()), this, SLOT(startBackup()));
    10. timer->start(5000);
    11. }
    12.  
    13. // Start the backup
    14. int Timer::startBackup(){
    15.  
    16. qDebug() << "inStart" << backingUP;
    17. //return 0;
    18.  
    19. if(backingUP){
    20. return 2;
    21. }
    22.  
    23. // Start the backup thread
    24. Backup *backup = new Backup();
    25.  
    26. // Make a connection for deleting this later when done
    27. //connect(backup, SIGNAL(finished()), backup, SLOT(deleteLater()));
    28.  
    29. // Set that we are running ATM
    30. backingUP = true;
    31.  
    32. backup->start();
    33.  
    34. return 1;
    35.  
    36. }
    37.  
    38. // The constructor
    39. Backup::Backup(QObject *parent)
    40. : QThread(parent)
    41. {
    42.  
    43. // This thread can never be terminated
    44. //this->setTerminationEnabled(false);
    45. }
    46.  
    47. // The function that does everything
    48. void Backup::run()
    49. {
    50.  
    51. this->doit();
    52.  
    53. }
    54.  
    55.  
    56. void Backup::doit(){
    57.  
    58. qDebug() << "Doit";
    59.  
    60. socket = new QSslSocket;
    61.  
    62. connect(socket, SIGNAL(readyRead()),
    63. this, SLOT(socketReadyRead()));
    64. connect(socket, SIGNAL(sslErrors(QList<QSslError>)),
    65. this, SLOT(sslErrors(QList<QSslError>)));
    66. connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
    67. this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
    68. connect(socket, SIGNAL(encrypted()),
    69. this, SLOT(socketEncrypted()));
    70.  
    71. // Connect to the server
    72. socket->connectToHostEncrypted(NSettings->value("hostname").toString(), NSettings->value("port").toInt());
    73.  
    74. // Wait for it to reach the encryted and connected state
    75. if (!socket->waitForEncrypted()) {
    76. qDebug() << "waitForEncrypted Error";
    77. return; // An error occured
    78. }
    79.  
    80. // This is the protocol
    81. socket->write("ENTERHEADER\r\n");
    82. socket->waitForBytesWritten();
    83.  
    84. QByteArray username = NSettings->value("user").toByteArray();
    85. QByteArray device = NSettings->value("device").toByteArray();
    86.  
    87. // We first need to LOGIN
    88. this->rite("L"+ username.toBase64() +" "+ device.toBase64());
    89. socket->waitForBytesWritten();
    90.  
    91. }
    92.  
    93. // Called whenever there is something to READ in the buffer
    94. void Backup::socketReadyRead(){
    95.  
    96. qDebug() << socket->bytesAvailable();
    97. QString str = socket->readAll();
    98. qDebug() << str;
    99.  
    100. bool unconnected = !socket || socket->state() == QAbstractSocket::UnconnectedState;
    101.  
    102. if(str == "LOGIN SUCCESSFUL"){
    103.  
    104. for(int i = 1; i <= 1000; i++){
    105.  
    106. // Was the login successful ?
    107. if(unconnected){
    108. qDebug() << socket->state();
    109. return;
    110. }
    111.  
    112. this->rite("STORE ABC\r\n");
    113. socket->waitForBytesWritten();
    114. }
    115.  
    116. }
    117.  
    118. qDebug() << "End READ";
    119.  
    120. // Disconnect from the server
    121. //socket->disconnectFromHost();
    122.  
    123. }
    124.  
    125. // Called when the connection is fully established
    126. void Backup::socketEncrypted()
    127. {
    128. qDebug() << "Connection Established";
    129. }
    130.  
    131. // Called when there are SSL errors
    132. void Backup::sslErrors(const QList<QSslError> &errors)
    133. {
    134. foreach (const QSslError &error, errors){
    135. qDebug() << error.errorString();
    136. }
    137.  
    138. // Ignore Errors
    139. socket->ignoreSslErrors();
    140. }
    141.  
    142. // Whenever there is a change in the connection STATE, this is called
    143. void Backup::socketStateChanged(QAbstractSocket::SocketState state)
    144. {
    145. qDebug() << state;
    146. }
    147.  
    148. // Socket write safe as it appends the length to the beginning of the string
    149. qint64 Backup::rite(const QByteArray & data)
    150. {
    151. return socket->write(data);
    152. }
    To copy to clipboard, switch view to plain text mode 

    When I run this I get the following error :
    Qt Code:
    1. inConstrutor
    2. inStart false
    3. Doit
    4. QObject::connect: Cannot queue arguments of type 'QAbstractSocket::SocketState'
    5. (Make sure 'QAbstractSocket::SocketState' is registered using qRegisterMetaType().)
    6. waitForEncrypted Error
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSslSocket not working in QThread

    Well, for one you are not calling waitForReadyRead() so you will never get any data.

    Second, you are doing a lot of cross-thread signal/slot connections in doit (sender belongs to backup thread, receiver belongs to main thread).

    Question: what to you need the thread for?

    Cheers,
    _

  3. #3
    Join Date
    Dec 2012
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSslSocket not working in QThread

    Well, for one you are not calling waitForReadyRead() so you will never get any data.
    I have done a connect for that :
    Qt Code:
    1. connect(socket, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));
    To copy to clipboard, switch view to plain text mode 

    Second, you are doing a lot of cross-thread signal/slot connections in doit (sender belongs to backup thread, receiver belongs to main thread).
    I actually dont get this. Doesnt all the functions belong to the "Backup" class ?
    So why will it be cross thread ?
    Also the connection never happens. socket->waitForEncrypted() gives an error.
    Why would that be ?

    Question: what to you need the thread for?
    I needed it because the Main Window is a GUI and it hangs when there is to much data to write to the socket.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSslSocket not working in QThread

    Quote Originally Posted by micgooapp View Post
    I have done a connect for that :
    Qt Code:
    1. connect(socket, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));
    To copy to clipboard, switch view to plain text mode 
    True, but you are not running the thread's event loop.

    Quote Originally Posted by micgooapp View Post
    I actually dont get this. Doesnt all the functions belong to the "Backup" class ?
    So why will it be cross thread ?
    Yes and the instance of the Backup class belongs to the main thread. Therefore the receiver object is on the main thread, making the Qt::AutoConnection behave like a Qt::QueuedConnection.

    Make a receiver class and create an instance in doit(), just like you do for the socket. then run the thread's even loop by calling exec().
    This will make the same code work that you have already working on the main thread.

    Quote Originally Posted by micgooapp View Post
    I needed it because the Main Window is a GUI and it hangs when there is to much data to write to the socket.
    I assume you didn't do waitFor... when implementing that on the main thread, right?

    Cheers,
    _

  5. #5
    Join Date
    Dec 2012
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSslSocket not working in QThread

    Make a receiver class and create an instance in doit(), just like you do for the socket. then run the thread's even loop by calling exec().
    I am a little new to this. Can you give me an example on how to do this ?

    I assume you didn't do waitFor... when implementing that on the main thread, right?
    I did the waitFor only in this thread as it is.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSslSocket not working in QThread

    You can treat the thread's run() method like it were main(), i.e. just ignore that there is an object around it.

    So you create all objects you need, connect them as needed and then call exec(), i.e. like calling app.exec() in main()

    An alternative is to create the worker object outside the thread, use a QThread wihout subclassing it and calling moveToThread on the worker object before calling QThread::start()

    Cheers,
    _

  7. #7
    Join Date
    Dec 2012
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSslSocket not working in QThread

    I am still not clear with certain things.
    Maybe this will help me clear doubts. The following is the code of a threaded SSL server.

    main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include <QtCore>
    3.  
    4. #include <stdlib.h>
    5.  
    6. #include "server.h"
    7.  
    8. // Number of requests served
    9. quint64 Served = 0;
    10.  
    11. int main(int argc, char *argv[]){
    12.  
    13. QCoreApplication capp(argc, argv);
    14. NServer server;
    15. NServer serverTLS;
    16.  
    17. // Normal Server
    18. if (!server.listen(QHostAddress::Any, 1780)) {
    19. return 2;
    20. }
    21.  
    22. // SSL Server
    23. if (!serverTLS.listen(QHostAddress::Any, 1781)) {
    24. return 2;
    25. }
    26.  
    27. // Set this is a SSL server
    28. serverTLS.isTLS = 1;
    29.  
    30. // Load the certificate
    31. QFile cert("./conf/cert.pem");
    32. if(!cert.open(QIODevice::ReadOnly)){
    33. qDebug() << "Could not open cert.pem";
    34. }
    35.  
    36. serverTLS.Certificate = QSslCertificate(&cert);
    37.  
    38. // Load the KEY
    39. QFile key("./conf/key.pem");
    40. if(!key.open(QIODevice::ReadOnly)){
    41. qDebug() << "Could not open key.pem";
    42. }
    43.  
    44. serverTLS.SslKey = QSslKey(&key, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey, "123456");
    45.  
    46. // Start the event loop
    47. return capp.exec();
    48.  
    49. }
    To copy to clipboard, switch view to plain text mode 

    server.h
    Qt Code:
    1. #ifndef SERVER_H
    2. #define SERVER_H
    3.  
    4. #include <QtNetwork>
    5. #include <qdebug.h>
    6. #include "thread.h"
    7.  
    8. // The Server Class
    9. class NServer : public QTcpServer
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14.  
    15. // Constructor
    16. NServer(QObject *parent = 0);
    17.  
    18. // Is this a SSL server ?
    19. int isTLS;
    20.  
    21. // Certificate
    22. QSslCertificate Certificate;
    23.  
    24. // Key
    25. QSslKey SslKey;
    26.  
    27. protected:
    28.  
    29. void incomingConnection(int socketDescriptor);
    30.  
    31. };
    32.  
    33. #endif
    To copy to clipboard, switch view to plain text mode 

    server.cpp
    Qt Code:
    1. #include "server.h"
    2.  
    3. // Constructor
    4. NServer::NServer(QObject *parent) : QTcpServer(parent), isTLS(0) {
    5. // Nothing to do in the constructor
    6. }
    7.  
    8. // All incoming connections are sent to this function
    9. void NServer::incomingConnection(int socketDescriptor){
    10.  
    11. Thread * thread = new Thread(socketDescriptor, this);
    12. //pool.setMaxThreadCount(10000);
    13.  
    14. // Set the SSL Cert and Key if TLS is on
    15. if(isTLS){
    16. thread->isTLS = isTLS;
    17. thread->Certificate = Certificate;
    18. thread->SslKey = SslKey;
    19. }
    20.  
    21. connect(thread->socket, SIGNAL(sslErrors(QList<QSslError>)),
    22. this, SLOT(sslErrors(QList<QSslError>)));
    23.  
    24. connect(thread, SIGNAL(finished()), thread, SLOT(quit()));
    25. //connect(thread, SIGNAL(finished()), thread, SLOT(test()));
    26. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    27.  
    28. // Start the thread
    29. thread->start();
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 

    thread.h
    Qt Code:
    1. #ifndef THREAD_H
    2. #define THREAD_H
    3.  
    4. #include <QThread>
    5. #include <QtNetwork>
    6. #include <qdebug.h>
    7.  
    8. // Thread class
    9. class Thread : public QThread
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14.  
    15. // Constructor
    16. Thread(int socketDescriptor, QObject *parent);
    17.  
    18. // This is the function which is called on a new connection
    19. void run();
    20.  
    21. // Is it in TLS MODE ?
    22. int isTLS;
    23.  
    24. // Certificate
    25. QSslCertificate Certificate;
    26.  
    27. // Key
    28. QSslKey SslKey;
    29.  
    30. // A socket pointer to the SOCKET so that we can use it to do stuff
    31. QSslSocket * socket;
    32.  
    33. signals:
    34. void error(QTcpSocket::SocketError socketError);
    35.  
    36. private:
    37.  
    38. // The Socket Descriptor
    39. int socketDescriptor;
    40.  
    41. // If the output has started then no header should be allowed
    42. bool outputStarted;
    43.  
    44. // SSL Socket Connection start
    45. bool SSLSocket();
    46.  
    47. // TCP Socket Connection start
    48. bool NormalSocket();
    49.  
    50. // Write Data to the socket
    51. bool socketWrite(const QByteArray &text);
    52.  
    53. // Write a header
    54. bool header(const QByteArray &text);
    55.  
    56. // Write the BODY
    57. bool write(const QByteArray &text);
    58.  
    59. // Write the given BODY and Close the SOCKET as well.
    60. bool end(const QByteArray &text);
    61.  
    62. // Closes the socket
    63. void cleanup();
    64.  
    65. public slots:
    66.  
    67. // Closes the socket
    68. void sslErrors(const QList<QSslError> &errors);
    69.  
    70. // SSL Socket Connection start
    71. void handshakeComplete();
    72.  
    73. void test();
    74.  
    75. };
    76.  
    77. #endif
    To copy to clipboard, switch view to plain text mode 

    thread.cpp
    Qt Code:
    1. #include "thread.h"
    2.  
    3. extern quint64 Served;
    4.  
    5. // Constructor
    6. Thread::Thread(int socketDescriptor, QObject *parent)
    7. : QThread(parent), socketDescriptor(socketDescriptor)
    8. {
    9. this->isTLS = 0;
    10. this->setTerminationEnabled(true);
    11.  
    12. qDebug() << "Socket" << socketDescriptor;
    13.  
    14. //qDebug()<< QThread::idealThreadCount();
    15. }
    16.  
    17. void Thread::test(){
    18. qDebug() << "Finished";
    19. }
    20.  
    21. // When its a normal connection
    22. bool Thread::NormalSocket(){
    23.  
    24. // Create the Socket
    25. socket = new QSslSocket;
    26.  
    27. if (!socket->setSocketDescriptor(socketDescriptor)) {
    28. emit error(socket->error());
    29. delete socket;
    30. return false;
    31. }
    32.  
    33. //qDebug() << "In Normal Socket";
    34.  
    35. return true;
    36.  
    37. }
    38.  
    39. // SSL Connection hence SSL Socket
    40. bool Thread::SSLSocket(){
    41.  
    42. // Create the Socket
    43. socket = new QSslSocket;
    44.  
    45. //qDebug() << Certificate;
    46. //qDebug() << SslKey;
    47.  
    48. connect(socket, SIGNAL(encrypted()),
    49. this, SLOT(handshakeComplete()));
    50.  
    51. connect(socket, SIGNAL(sslErrors(QList<QSslError>)),
    52. this, SLOT(sslErrors(QList<QSslError>)));
    53.  
    54. // Set the protocol and certificates
    55. socket->setProtocol(QSsl::AnyProtocol);
    56. socket->setLocalCertificate(Certificate);
    57. socket->setPrivateKey(SslKey);
    58.  
    59. // Set the descriptor
    60. if (!socket->setSocketDescriptor(socketDescriptor)) {
    61. emit error(socket->error());
    62. delete socket;
    63. return false;
    64. }
    65.  
    66. // No need to verify the peer
    67. socket->setPeerVerifyMode(QSslSocket::VerifyNone);
    68.  
    69. // Start the encryption
    70. socket->startServerEncryption();
    71.  
    72. // Wait for the excryption to start
    73. socket->waitForEncrypted(1000);
    74.  
    75. return true;
    76.  
    77. }
    78.  
    79. // Called when the thread is started
    80. void Thread::run(){
    81.  
    82. //this->exec();
    83.  
    84. Served++;
    85. qDebug() << "Num Req : " << Served;
    86.  
    87. // SSL Socket Start
    88. if(isTLS > 0){
    89.  
    90. qDebug() << "SSL";
    91. if(!SSLSocket()){
    92. return;
    93. }
    94.  
    95. // Normal Socket
    96. }else{
    97.  
    98. if(!NormalSocket()){
    99. return;
    100. }
    101.  
    102. }
    103.  
    104. // Wait for it to become read ready for a MAX of 1 second
    105. socket->waitForReadyRead(1000);
    106. //qDebug()<<"Wait";
    107. QMap<QByteArray, QByteArray> headers;
    108. QByteArray reqType;
    109.  
    110. //qDebug() << "Enc : " << socket->isEncrypted();
    111. //qDebug() << "Errors : " << socket->sslErrors();
    112.  
    113. /////////////////////////////////////
    114. // Determine its an DATA / HTTP Call
    115. /////////////////////////////////////
    116.  
    117. // Get the Request Header
    118. req = socket->read(81920);
    119. //req = socket->readAll();
    120. //qDebug() << "Req : " << req;
    121.  
    122. QByteArray block = "HTTP/1.0 200 Ok\r\n"
    123. "Content-Type: text/html; charset=\"utf-8\"\r\n"
    124. "\r\n"
    125. "<form method=\"post\" action=\"\" name=\"input\">"
    126. "<input type=\"text\" value=\"test\">"
    127. "<input type=\"submit\" value=\"test\">"
    128. "</form>\n"
    129. "Test"
    130. "\r\n"
    131. "\r\n"
    132. "\r\n";
    133.  
    134. this->write(block);
    135.  
    136. /*unsigned char * test = (unsigned char *)malloc(134217728);
    137.   this->sleep(10);
    138.   free(test);*/
    139.  
    140. //this->write(block);
    141.  
    142. cleanup();
    143.  
    144. //delete socket;
    145.  
    146. //emit finished();
    147.  
    148. }
    149.  
    150. bool Thread::socketWrite(const QByteArray &text){
    151.  
    152. socket->write(text);
    153.  
    154. return socket->flush();
    155.  
    156. }
    157.  
    158. void Thread::cleanup(){
    159.  
    160. socket->disconnectFromHost();
    161. if(socket->state() != QAbstractSocket::UnconnectedState){
    162. socket->waitForDisconnected();
    163. }
    164.  
    165. //qDebug() << "Cleaned";
    166.  
    167. // Clear the socket
    168. delete socket;
    169.  
    170. // Delete the thread
    171. //this->exit(0);
    172. //this->wait();
    173. //this->deleteLater();
    174.  
    175. }
    176.  
    177. bool Thread::header(const QByteArray &text){
    178.  
    179. if(outputStarted){
    180. return false;
    181. }
    182.  
    183. return this->socketWrite(text);
    184.  
    185. }
    186.  
    187.  
    188. bool Thread::write(const QByteArray &text){
    189.  
    190. // Set that output has been started
    191. outputStarted = true;
    192.  
    193. return this->socketWrite(text);
    194.  
    195. }
    196.  
    197.  
    198. bool Thread::end(const QByteArray &text){
    199.  
    200. // Set that output has been started
    201. outputStarted = true;
    202.  
    203. return this->socketWrite(text);
    204.  
    205. }
    206.  
    207. // Called when there are SSL errors
    208. void Thread::sslErrors(const QList<QSslError> &errors){
    209. foreach (const QSslError &error, errors){
    210. qDebug() << "[Backup::sslErrors] " << error.errorString();
    211. }
    212.  
    213. // Ignore Errors
    214. //socket->ignoreSslErrors();
    215. }
    216.  
    217. // SSL handshake complete
    218. void Thread::handshakeComplete(){
    219. qDebug() << "[Thread::handshakeComplete]";
    220. }
    To copy to clipboard, switch view to plain text mode 

    When I run this application I get an error as follows :
    QObject::connect: Cannot queue arguments of type 'QList<QSslError>'
    (Make sure 'QList<QSslError>' is registered using qRegisterMetaType().)
    I am not able to understand why is this so ?
    What is the corrective code for this ?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSslSocket not working in QThread

    Quote Originally Posted by micgooapp View Post
    What is the corrective code for this ?
    The correct code is to delete all your threaded code and handle the socket in the main thread.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Dec 2012
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSslSocket not working in QThread

    The issue in doing that is that sometimes when a reques is coming, it might be blocking because there will be a socket read and write like the FTP Protocol.
    Hence I used the Qthread class.

    Lets say a request comes with the header "PROTOTRANS"
    In that case I will need to send writes, wait for confirmation, send writes again and finish.

    Please let me know what have i done wrong in the QThread class. Really appreciate your help.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSslSocket not working in QThread

    Quote Originally Posted by micgooapp View Post
    The issue in doing that is that sometimes when a reques is coming, it might be blocking because there will be a socket read and write like the FTP Protocol.
    No, it won't be blocking.

    Hence I used the Qthread class.
    Don't. Threads with Qt Networking are Evil.

    Lets say a request comes with the header "PROTOTRANS"
    In that case I will need to send writes, wait for confirmation, send writes again and finish.
    Great. You don't need threads for that.

    Please let me know what have i done wrong in the QThread class. Really appreciate your help.
    It's all wrong. First of all you are creating the socket in a wrong thread. I really suggest you discard the use of threads, you don't need them.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Dec 2012
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSslSocket not working in QThread

    >> It's all wrong. First of all you are creating the socket in a wrong thread. I really suggest you discard the use of threads, you don't need them.

    Means I should create the socket in the main thread and pass it to the thread ?
    Also is the moving to thread a better option :
    http://mayaposch.wordpress.com/2011/...l-explanation/

    I will try to avoid the threads and use the non-blocking mode.
    Its just that in the non-blocking mode you need to create a lot of functions and connect them to a signal as a slot.

    I would still like to solve this error just to improve my knowledge what am I doing wrong. Earlier you said that the connect should have happened in the run() function. Here the ssl errors are being connected in the run() function.

    Is there any guide to understand this ?

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSslSocket not working in QThread

    Quote Originally Posted by micgooapp View Post
    Means I should create the socket in the main thread and pass it to the thread ?
    On the contrary. But I might have not read your code carefully enough. If you are creating the socket in the run method of the thread then that's ok (but in that case you are trying to setup a signal-slot connection to an object that doesn't exist yet -- that is the "socket" variable in your Thread class). However, again, you're just making your code needlessly complex by using threads.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #13
    Join Date
    Dec 2012
    Posts
    9
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSslSocket not working in QThread

    >> If you are creating the socket in the run method of the thread then that's ok (but in that case you are trying to setup a signal-slot connection to an object that doesn't exist yet -- that is the "socket" variable in your Thread class).

    I am defining the Socket in the header first and then calling the connect().
    Also I do set the socketDescriptor first as well.
    Also the error (QObject::connect: Cannot queue arguments of type 'QList<QSslError>') is shown when I visit the browser with port 1781 in https. It does successfully serve the page.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSslSocket not working in QThread

    Quote Originally Posted by micgooapp View Post
    I am defining the Socket in the header first and then calling the connect().
    You are only declaring a variable but not initializing it.

    Also the error (QObject::connect: Cannot queue arguments of type 'QList<QSslError>') is shown when I visit the browser with port 1781 in https. It does successfully serve the page.
    Which means you are connecting a signal in thread A to a slot in thread B. Which doesn't make much sense for this particular signal.


    Remember the QThread object does not live in the thread it represents but rather in the thread that created it (the object).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 7
    Last Post: 24th September 2012, 08:17
  2. QThread::IdlePriority not working?
    By montamer in forum Qt Programming
    Replies: 2
    Last Post: 7th April 2011, 10:04
  3. Signals not working in a QThread
    By curreli in forum Qt Programming
    Replies: 1
    Last Post: 10th August 2010, 17:04
  4. Working with QThread
    By Fastman in forum Qt Programming
    Replies: 1
    Last Post: 27th February 2009, 23:35
  5. QThread::terminate () is not working!!!!!
    By biswajithit in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2008, 13:03

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.