PDA

View Full Version : QSslSocket problems



TheOnlyDan
25th September 2007, 14:33
Hi,

I'm currently developping with Qt 4.3.1 on Windows XP SP2 using VS2005 SP1. I'm trying to get a SSL Server to work. Both the ssl client and the ssl server are Qt project and are run on the same machine. I'm using ip address 127.0.0.1 and I have installed openssl for windows from http://www.slproweb.com/products/Win32OpenSSL.html.

The structure for the client is to have a QThread create the ssl socket in the run method, connect the socket to the thread slot using Queued connections and try connecting to the ssl server by calling connectToHost. When the socket raises the connected signal, the socket then calls startClientEncryption.

The ssl server program starts a QThread that creates an ssl server. On receiving on incoming connection, the server creates an ssl socket, sets the descriptor. The Thread then connects the socket to its own slots using queued connections and then calls startServerEncryption.

No error occurs so far. Then the following error messages appears in the client ouput : "QSocketNotifier: socket notifiers cannot be enabled from another thread".

If I try creating a ThreadNotifiger object in the client thread and connect that to the ssl socket, the server falls over with an UnknownSocketError and the connection is broken.

I have tried changing the nature of the connections, changing the ssl protocl, creating the client socket in main UI client thread. Nothing has worked. Sometimes the server crashes or I always get the UnknowSocketError. No SSL errors are raised. The server error string is empty.

I have tried to check the Qt documents about which version of openssl I should be using. I couldn't fing anything about this.

Does anyone have any advice about how to get this to work ?

Thanks,
Pascal.

wysota
25th September 2007, 15:00
Make sure that you call setEnabled() from the same thread where the notifier was created. Remember that the thread object does not live in its own thread! Thus if the notifier is a member of your QThread subclass, it won't work.

TheOnlyDan
25th September 2007, 15:45
I should have explained that the ThreadNotifier class is my own class. It's not derived from QSocketNotifier. It gets created inside the thread run method using new.

I should also add that the ssl server is derived from QTcpServer with nextPendingConnection and incomingConnection overridden.

Am I supposed to call both startClientEncryption and startServerEncrytption or only one of them ?

And which version of the ssl libs am I supposed to use with Qt 4.3.1 ?

Thanks for your input,
Pascal.

wysota
25th September 2007, 17:20
I should have explained that the ThreadNotifier class is my own class. It's not derived from QSocketNotifier. It gets created inside the thread run method using new.
Qt complains about QSocketNotifier, not ThreadNotifier. One way or the other your calling objects accross threads.


And which version of the ssl libs am I supposed to use with Qt 4.3.1 ?

That doesn't matter. SSL is not a problem here.

TheOnlyDan
26th September 2007, 15:33
I have fixed the problem of messages crossing threads but now the ssl server falls over with an UnkwonSocketError.

Everything seems to work fine if I don't try to do the ssl encryption handshake. I am using default configured ssl sockets. Do they need to be properly configured beforehand ?

Cheers,
Pascal.

wysota
26th September 2007, 17:12
Try these:
http://labs.trolltech.com/blogs/2007/03/26/ssl-with-5-lines-of-code/
http://labs.trolltech.com/blogs/2007/06/27/first-ssl-example-secure-socket-client/

TheOnlyDan
27th September 2007, 11:54
Thanks for the links. I'll check them out.

Cheers,
Pascal.