Pong

June 9th 2014, SSL Client /w QSslSocket

June 9th 2014 SSL Server with QTcpServer | | June 9th 2014 SSL Client and Server Application

Here is a simple synchronous client that communicates with the SSL server:

class SSLClient: public QObject
{
   Q_OBJECT

public:

   SSLClient(QObject *parent = NULL)
   {
      // start writing after connection is encrypted
      connect(&socket_, SIGNAL(encrypted()),
              this, SLOT(startWriting()));
   }

   virtual ~SSLClient() {}

   // start transmission
   void start(QString hostName, quint16 port)
   {
      socket_.setProtocol(QSsl::TlsV1);
      socket_.setPeerVerifyMode(QSslSocket::VerifyNone);

      socket_.connectToHostEncrypted(hostName, port);
      socket_.waitForDisconnected();
   }

   QSslSocket socket_;

protected slots:

   // start writing after connection is established
   void startWriting()
   {
      // write data to the ssl socket
      socket_.write("test", 4);

      // disconnect the ssl socket
      socket_.disconnectFromHost();
   }

};

Note that the peerVerifyMode needs to be set to QSslSocket::VerifyNone. Otherwise the connection is regarded as untrusted due to the server’s self-certified certificate and the client won’t accept the encrypted connection.

Also note that the client class is blocking, meaning that it returns from the start method after the data has been transmitted or a time-out has been generated. It does not require an event loop to be running.

In case you need a non-blocking client, simply let it run in a QThread (by instanciating a client in the run method of a QThread).

June 9th 2014 SSL Server with QTcpServer | | June 9th 2014 SSL Client and Server Application

Options: