Results 1 to 15 of 15

Thread: QSslSocket: connecting SIGNAL and SLOT

  1. #1
    Join Date
    Sep 2012
    Posts
    34
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QSslSocket: connecting SIGNAL and SLOT

    Hi,

    I cannot figure out what is wrong with following code. Code won't compile. I've got a feeling I'm somehow creating the whole connection object wrong, but I guess you guys notice it right away if that's the case. It's interesting that I've seen at least dozen snapshots of code where the socket is created just like this. That's why I'm suspecting the mainwindow.cpp part (below).

    Compiling errors:
    Qt Code:
    1. ..\muutoshallinta\client_connection.cpp: In member function 'bool client_connection::init_connection()':
    2. ..\muutoshallinta\client_connection.cpp:21: error: invalid conversion from 'QSslSocket*' to 'SOCKET'
    3. ..\muutoshallinta\client_connection.cpp:21: error: cannot convert 'const char*' to 'const sockaddr*' for argument '2' to 'int connect(SOCKET, const sockaddr*, int)'
    4. ..\muutoshallinta\client_connection.cpp:22: error: invalid conversion from 'QSslSocket*' to 'SOCKET'
    5. ..\muutoshallinta\client_connection.cpp:22: error: cannot convert 'const char*' to 'const sockaddr*' for argument '2' to 'int connect(SOCKET, const sockaddr*, int)'
    To copy to clipboard, switch view to plain text mode 

    client_connection.cpp:
    Qt Code:
    1. #include "client_connection.h"
    2. #include "common.h"
    3. #include <QtNetwork>
    4. #include <stdlib.h>
    5. #include "mainwindow.h"
    6.  
    7. client_connection::client_connection()
    8. {
    9. }
    10.  
    11. bool client_connection::init_connection()
    12. {
    13. if (!QSslSocket::supportsSsl()) {
    14. return false;
    15. }
    16. ptr_socket = new QSslSocket();
    17.  
    18. if (!ptr_socket) {
    19.  
    20. connect(ptr_socket, SIGNAL(encrypted()), this, SLOT(connection_encrypted()));
    21. connect(ptr_socket, SIGNAL(encrypted()), this, SLOT(connection_disconnected()));
    22. /* connect(socket, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));*/
    23. }
    24.  
    25. ptr_socket->connectToHostEncrypted("localhost", 555);
    26.  
    27. return true;
    28. }
    To copy to clipboard, switch view to plain text mode 

    client_connection.h
    Qt Code:
    1. #ifndef CLIENT_CONNECTION_H
    2. #define CLIENT_CONNECTION_H
    3. #include <QtNetwork>
    4.  
    5.  
    6. class client_connection
    7. {
    8. public:
    9. client_connection();
    10.  
    11. bool init_connection();
    12.  
    13.  
    14. private slots:
    15. void connection_encrypted();
    16. void connection_disconnected();
    17.  
    18. private:
    19. QSslSocket *ptr_socket;
    20. };
    21.  
    22. #endif // CLIENT_CONNECTION_H
    To copy to clipboard, switch view to plain text mode 

    I'm creating the connection object in mainwindow.cpp:

    Qt Code:
    1. #ifdef FEA_CLIENT
    2. client_connection *ptr_connection = new client_connection();
    3. if(ptr_connection->init_connection())
    4. {
    5. //connection ready
    6. }
    7. #endif
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance!

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSslSocket: connecting SIGNAL and SLOT

    client_connection must be QObject or use QObject::connect

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSslSocket: connecting SIGNAL and SLOT

    cant simply use QObject::connect because client_connection contains slots - these slots won't be correctly moc'd without inheriting from QObject and using Q_OBJECT macro.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSslSocket: connecting SIGNAL and SLOT

    My mistake, client_connection must be inherited from QObject.

  5. #5
    Join Date
    Sep 2012
    Posts
    34
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSslSocket: connecting SIGNAL and SLOT

    Ok, so I was doing something wrong in the mainwindow while creating the object. Can't just figure out how to do this correctly. Could you guys push me to the right direction with some code?

    Thanks for your time!

    Edit. Some progress finally:

    After editing the client_connection.h like this, it now compiles. Is this what you meant?

    Qt Code:
    1. #ifndef CLIENT_CONNECTION_H
    2. #define CLIENT_CONNECTION_H
    3. #include <QtNetwork>
    4.  
    5.  
    6. class client_connection : public QObject
    7. {
    8. public:
    9. client_connection( ) : QObject( )
    10. {
    11. }
    12.  
    13. bool init_connection();
    14.  
    15. private slots:
    16. void connection_encrypted();
    17. void connection_disconnected();
    18.  
    19. private:
    20. QSslSocket *ptr_socket;
    21. };
    22.  
    23. #endif // CLIENT_CONNECTION_H
    To copy to clipboard, switch view to plain text mode 

    However, now the problem is that I cannot get the encrypted signal. Is it correctly defined, in client_connection.c?
    Last edited by Mobility; 16th December 2012 at 17:48.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSslSocket: connecting SIGNAL and SLOT

    Forgot about the Q_OBJECT macro. Between lines 7 and 8 put :
    Qt Code:
    1. Q_OBJECT
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Sep 2012
    Posts
    34
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSslSocket: connecting SIGNAL and SLOT

    Tried that but getting following error while running:

    Qt Code:
    1. ./debug\mainwindow.o:mainwindow.cpp:(.text$_ZN17client_connectionC1Ev[client_connection::client_connection()]+0x20): undefined reference to `vtable for client_connection'
    2. collect2: ld returned 1 exit status
    To copy to clipboard, switch view to plain text mode 

    I'm now wondering if the connection even gets encrypted. The following returns always false saying that connection would not get encrypted...

    Qt Code:
    1. bool client_connection::init_connection()
    2. {
    3. ptr_socket = new QSslSocket();
    4.  
    5. if (!ptr_socket) {
    6. // connect(ptr_socket, SIGNAL(encrypted()), this, SLOT(connection_encrypted()));
    7. }
    8.  
    9. ptr_socket->connectToHostEncrypted("localhost", 555);
    10. if(ptr_socket->waitForEncrypted(2000))
    11. {
    12. return true;
    13. }
    14. return false;
    15. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QSslSocket: connecting SIGNAL and SLOT

    Quote Originally Posted by Mobility View Post
    Tried that but getting following error while running:

    Qt Code:
    1. ./debug\mainwindow.o:mainwindow.cpp:(.text$_ZN17client_connectionC1Ev[client_connection::client_connection()]+0x20): undefined reference to `vtable for client_connection'
    2. collect2: ld returned 1 exit status
    To copy to clipboard, switch view to plain text mode 
    Run QMake after adding the macro.
    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
    Sep 2012
    Posts
    34
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSslSocket: connecting SIGNAL and SLOT

    Ok, QMake did the trick. But the signals are still not working. After debugging a while, I noticed from server side traces that connection - socket - seems to be deleted right after connection is created. Just for a moment, server thinks that connection is fine, but then it finds out that connection is lost. So my thinking is that I'm still not correctly setting up the client_connection as it seems that after running the init_connection() function the socket is lost. How should I set up the connection so that it does not get deleted? Thanks!

    Edit after couple of hours...

    It really seems that the encryption just fails. I've tried to simplify this as much as possible by putting everything to the mainwindow and not using any slots for the connection:

    Qt Code:
    1. socket = new QSslSocket();
    2. socket->connectToHostEncrypted("localhost", 555);
    3. if(socket->waitForEncrypted(1000))
    4. {
    5. TRACE("Connected!");
    6. }
    To copy to clipboard, switch view to plain text mode 

    Still I don't get the "Connected!" trace. My understanding is that it should not need anything else to get it connected. Right? Still from tester side I can see that connection is set up for a moment. What's wrong here??
    Last edited by Mobility; 17th December 2012 at 19:27.

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSslSocket: connecting SIGNAL and SLOT

    Well:
    • there may be nothing listening at port 555 on your machine,
    • something is listening but may not support SSL,
    • something is listening but it may not complete a full SSL handshake in less than 1 second,
    • your certificates may be missing/broken/expired/unverifiable,
    • you may not have the relevant OpenSSL DLL available for on-demand load by your client,
    • any of a jillion other things.


    Have you looked at sslErrors() or error() ?

  11. #11
    Join Date
    Sep 2012
    Posts
    34
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSslSocket: connecting SIGNAL and SLOT

    Cheers!

    There is a server running. I've tried to use the existing fortuna SSL server example in addition to my own server. And if I use the fortuna client (SSL version) against either one of them, connection succeeds. This implies that the problem is with this client I'm implementing now. My understanding is that certificates are not needed in client side if the server is not requesting them. Anyway I'll have to take a look at the errors you mentioned.

  12. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSslSocket: connecting SIGNAL and SLOT

    Client certificates are rarely required, but I was thinking about the server cert. If the server is offering a self-signed certificate (or one signed by an unrecognised CA) then the client cannot verify the server and the connection will not proceed by default.

  13. #13
    Join Date
    Sep 2012
    Posts
    34
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSslSocket: connecting SIGNAL and SLOT

    Actually server certificate is self-signed one (created by myself), but I just cannot see why the problem is seen only with the client under implementation. Why the fortuna secure client works fine?

    Yesterday I checked the sslErrors() on client side and this was what I found: "The host name did not match any of the valid hosts for this certificate". I have not yet figured out what does that exactly mean in this case. I thought that in this basic scenario on client side it would not check any certificates...

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

    Default Re: QSslSocket: connecting SIGNAL and SLOT

    Quote Originally Posted by Mobility View Post
    Actually server certificate is self-signed one (created by myself), but I just cannot see why the problem is seen only with the client under implementation. Why the fortuna secure client works fine?
    Because it ignores the error.

    See the docs for QSslSocket::ignoreSslErrors().
    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.


  15. #15
    Join Date
    Sep 2012
    Posts
    34
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSslSocket: connecting SIGNAL and SLOT

    Thanks guys! I did miss that one line which can be used to ignore the SSL errors. After getting the connection working, I managed (with your help) to get also the client_connection class working and now I'm able to send & receive data as planned from any existing class in my program. Thanks again!

Similar Threads

  1. Connecting custom signal and slot
    By DmitryNik in forum Newbie
    Replies: 4
    Last Post: 12th September 2011, 14:15
  2. Replies: 2
    Last Post: 7th August 2011, 11:50
  3. Connecting signal and slot by name
    By grayfox in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2011, 09:00
  4. Replies: 3
    Last Post: 17th November 2010, 14:12
  5. Connecting signal to custom slot?
    By dbrmik in forum Qt Tools
    Replies: 2
    Last Post: 30th April 2009, 09:28

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.