c++qtsocketssslqsslsocket

Calling QSslSocket::startServerEncryption, but nothing happens


I'm trying to implement an SSL server using the sample code from Qt documentation.

But after serverSocket->startServerEncryption(); is called, nothing happens - neither the encrypted() nor the sslErrors() signals are emitted (I've put breakpoints in the slots connected to them).

I test it by connecting an QSslSocket using connectToHostEncrypted to the port I'm listening on. The socket sends data, but my server does not respond (I'm using a TCP sniffer/proxy to see all the data being sent from client to server and from server to client).

This is my code for the server:

void SslServer::incomingConnection(int socketDescriptor)
{
    qDebug() << "SslServer::incomingConnection()";
    QSslSocket *serverSocket = new QSslSocket(this);
    if (serverSocket->setSocketDescriptor(socketDescriptor)) {
        connect(serverSocket, SIGNAL(encrypted()), this, SLOT(ready()));
        connect(serverSocket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>)));
        serverSocket->startServerEncryption();
    } else {
        delete serverSocket;
    }
}

And this is how I connect to it:

server = new SslServer(this);
server->listen(QHostAddress::Any, 3333);
QSslSocket *socket = new QSslSocket(this);
socket->connectToHostEncrypted("127.0.0.1", 3333);

Solution

  • According to the documentation:

    Both the key and the local certificate are required if you are creating an SSL server socket.

    And if you don't provide them, a "regular" error(QAbstractSocket::SocketError) signal is emitted by the socket. As you found out, the server doesn't send any data in that case.