Results 1 to 4 of 4

Thread: QSslSocket : Having troubles

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2007
    Posts
    158
    Thanks
    25
    Qt products
    Qt4
    Platforms
    Windows

    Default QSslSocket : Having troubles

    Hi !

    I'm under Windows 2000 Pro, using the beautifull Qt4.3 open source version.
    This has not been recompiled, and it is configured in shared/debug.
    I've downloaed and installed the OpenSSL library, but I don't think this will help me.

    I was using a QtcpSocket on Qt4.2.3 to connect an IMAP server.
    Now I replaced it by a QSslSocket, that inherits from QtcpSocket, but I'm having troubles.



    Myclass.h :
    Qt Code:
    1. #include <QObject>
    2. #include <QTcpSocket>
    3. #include <QList>
    4. #include <QString>
    5. #include <QSslSocket>
    6. #include <QProcess>
    7.  
    8. class MailChecker : public QObject
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. MailChecker();
    14. void Check();
    15.  
    16. private slots:
    17. void SLOT_socket_proxyAuthenticationRequired(const QNetworkProxy &, QAuthenticator *);
    18. void SLOT_socket_sslErrors(const QList<QSslError>);
    19. void SLOT_socket_stateChanged(QAbstractSocket::SocketState);
    20. void SLOT_socket_connected();
    21. void SLOT_socket_disconnected();
    22. void SLOT_socket_encrypted();
    23. void SLOT_socket_hostFound();
    24.  
    25. private:
    26. QSslSocket *sockect_ssl;
    27.  
    28. // Socket management
    29. void openSocket();
    30. void closeSocket();
    31. bool readLineFromSocket(QByteArray&);
    32. bool writeToSocket(const QString &);
    33. };
    To copy to clipboard, switch view to plain text mode 


    Myclass.cpp :
    Qt Code:
    1. MailChecker::MailChecker() : QObject()
    2. { sockect_ssl = new QSslSocket(this);
    3.  
    4. connect(sockect_ssl, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy & proxy, QAuthenticator * authenticator)),
    5. this, SLOT(SLOT_socket_proxyAuthenticationRequired(const QNetworkProxy & proxy, QAuthenticator * authenticator)));
    6. connect(sockect_ssl, SIGNAL(sslErrors(const QList<QSslError>)),this, SLOT(SLOT_socket_sslErrors(const QList<QSslError>)));
    7. connect(sockect_ssl, SIGNAL(stateChanged), this, SLOT(SLOT_socket_stateChanged(QAbstractSocket::SocketState socketState)));
    8. connect(sockect_ssl, SIGNAL(connected()), this, SLOT(SLOT_socket_connected()));
    9. connect(sockect_ssl, SIGNAL(disconnected()),this, SLOT(SLOT_socket_disconnected()));
    10. connect(sockect_ssl, SIGNAL(encrypted()), this, SLOT(SLOT_socket_encrypted()));
    11. connect(sockect_ssl, SIGNAL(hostFound()), this, SLOT(SLOT_socket_hostFound()));
    12. }
    13. // ---------------------------------------------------------------------------------
    14. void MailChecker::SLOT_socket_proxyAuthenticationRequired(const QNetworkProxy & proxy, QAuthenticator * authenticator)
    15. {
    16. cout << "PROXY AUTHENTIFICATION REQUIRED" << endl;
    17. }
    18.  
    19. void MailChecker::SLOT_socket_sslErrors(const QList<QSslError> errors)
    20. {
    21. cout << "SSL ERRORS !" << endl;
    22. }
    23.  
    24. void MailChecker::SLOT_socket_stateChanged(QAbstractSocket::SocketState socketState)
    25. {
    26. cout << "STATE CHANGED" << endl;
    27. }
    28.  
    29. void MailChecker::SLOT_socket_connected()
    30. {
    31. cout << "CONNECTED" << endl;
    32. }
    33.  
    34. void MailChecker::SLOT_socket_disconnected()
    35. {
    36. cout << "DISCONNECTED" << endl;
    37. }
    38.  
    39. void MailChecker::SLOT_socket_encrypted()
    40. {
    41. cout << "ENCRYPTED" << endl;
    42. }
    43.  
    44. void MailChecker::SLOT_socket_hostFound()
    45. {
    46. cout << "HOST FOUND" << endl;
    47. }
    48. // ---------------------------------------------------------------------------------
    49. void MailChecker::openSocket()
    50. {
    51. sockect_ssl = new QSslSocket();
    52. }
    53.  
    54. void MailChecker::closeSocket()
    55. {
    56. if ( sockect_ssl )
    57. {
    58. sockect_ssl->close();
    59. sockect_ssl->deleteLater();
    60. }
    61. }
    62.  
    63. bool MailChecker::readLineFromSocket( QByteArray& data )
    64. {
    65. while ( !sockect_ssl->canReadLine() )
    66. {
    67. if ( !sockect_ssl->waitForReadyRead( 30 * 1000 ) )
    68. {
    69. closeSocket();
    70. return 0;
    71. }
    72. }
    73. if (sockect_ssl->readLine(data.data(), data.size()) == -1)
    74. {
    75. closeSocket();
    76. return false;
    77. }
    78. return true;
    79. }
    80.  
    81. bool MailChecker::writeToSocket( const QString & str )
    82. {
    83. QByteArray ba = str.toLocal8Bit();
    84.  
    85. qint64 wrote = 0;
    86. while ( wrote < (qint64)ba.size() )
    87. {
    88. qint64 ret = sockect_ssl->write( ba.data() + wrote, ba.size() - wrote );
    89. if ( ret == -1 )
    90. return false; // error
    91. wrote += ret;
    92. }
    93. sockect_ssl->waitForBytesWritten();
    94. return true;
    95. }
    To copy to clipboard, switch view to plain text mode 
    My first trouble is that the Slot are never called by my application... And they should be.
    I don't understand why...



    (1) Using the QSslSocket like a QTcpSocket (no encryption) :
    Qt Code:
    1. void MailChecker::Check()
    2. {
    3. sockect_ssl->connectToHost(IMAP_host, IMAP_port);
    4.  
    5. if (sockect_ssl->waitForConnected(15000))
    6. {
    7. if (readLineFromSocket(line))
    8. {
    9. ...
    10. }
    11. else
    12. {
    13. cout << "\n ERROR - Unable to read server response. " << endl;
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    This worked well for my old class using the QTcpSocket.
    Now, this works well too, with the QSslSocket... But the data isn't encrypted.



    (2) Using the QSslSocket with encryption :
    Qt Code:
    1. void MailChecker::Check()
    2. {
    3. sockect_ssl->connectToHostEncrypted(IMAP_host, IMAP_port);
    4.  
    5. if (sockect_ssl->waitForConnected(15000))
    6. {
    7. if (readLineFromSocket(line))
    8. {
    9. ...
    10. }
    11. else
    12. {
    13. cout << "\n ERROR - Unable to read server response. " << endl;
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    This should work. This is like the example given is the documentation...
    But I get the ERROR message immediately : the socket hasn't waited for 15s. trying to connect to host with the encrypted mode.



    (3) Using the QSslSocket with encryption :
    Qt Code:
    1. void MailChecker::Check()
    2. {
    3. sockect_ssl->connectToHost(IMAP_host, IMAP_port);
    4.  
    5. if (sockect_ssl->waitForConnected(15000))
    6. {
    7. sockect_ssl->startClientEncryption();
    8. if (readLineFromSocket(line))
    9. {
    10. ...
    11. }
    12. else
    13. {
    14. cout << "\n ERROR - Unable to read server response. " << endl;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    This is the sameThis code should do the same job as the (2) solution, but is provided for a TLS support. In my case, I get the ERROR like before, without any wait for connection.


    If someone has an idea ?
    Thanks
    Last edited by jacek; 22nd June 2007 at 16:53. Reason: wrapped too long line

Similar Threads

  1. makefile troubles
    By Walsi in forum Qt Programming
    Replies: 6
    Last Post: 12th April 2007, 15:12
  2. libqjpeg troubles
    By TheRonin in forum Installation and Deployment
    Replies: 6
    Last Post: 25th August 2006, 15:48
  3. lupdate *.pro troubles
    By jeff_s in forum Qt Programming
    Replies: 1
    Last Post: 28th July 2006, 10:07
  4. QTextEdit troubles?!
    By Jojo in forum Qt Programming
    Replies: 2
    Last Post: 21st February 2006, 16:54

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.