简体   繁体   中英

How to connect with server using Secure WebSocket in Qt c++

I am creating Qt application that should be able to connect with server using secure connection (ie wss://xxx.xxx.xxx.xxx:8080), Application is working fine with none-secure connection (ie ws://xxx.xxx.xxx.xxx:8443).

Protocol i am using is WebSocket. I have used library for connection.

Qt Creator 3.4.2 (opensource) Based on Qt 5.5.0 (MSVC 2013, 32 bit)

QsslSocket::supportsSsl() was returning false , then find out that i was missing openssl library files ( libeay32.dll , ssleay32.dll ) then i put these file in the path of my application and run code again now QSslSocket::supportsSsl() is returning true . but when i try to connect websocket it is showing flollowing errors.

connect(&wsocket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(onError(QAbstractSocket::SocketError)), Qt::DirectConnection); connect(&wsocket, SIGNAL(sslErrors(QList)), SLOT(onSslErrors(QList)), Qt::DirectConnection);

onSslErrors : unknown error onError : "The host name did not match any of the valid hosts for this certificate" Error: QAbstractSocket::SocketError(13)

code snippet from client side(Qt)

void onConnectToServer()
{
   qRegisterMetaType<QAbstractSocket::SocketError>();
    qRegisterMetaType<QAbstractSocket::SocketState>();
    connect(&wsocket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(onError(QAbstractSocket::SocketError)), Qt::DirectConnection);
    connect(&wsocket, SIGNAL(sslErrors(QList<QSslError>)), SLOT(onSslErrors(QList<QSslError>)), Qt::DirectConnection);
    connect(&wsocket, SIGNAL(connected()), SLOT(onConnected()), Qt::DirectConnection);
    connect(&wsocket, SIGNAL(binaryMessageReceived(QByteArray)), SLOT(onBinaryMessage(QByteArray)), Qt::DirectConnection);
    connect(&wsocket, SIGNAL(textMessageReceived(QString)), SLOT(onTextMessage(QString)), Qt::DirectConnection);
    connect(&wsocket, SIGNAL(disconnected()), SLOT(onDisconnected()), Qt::DirectConnection);
    connect(&wsocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), SLOT(onStateChanged(QAbstractSocket::SocketState)), Qt::DirectConnection);

    QUrl serverURL;
    QWebSocket wsocket;

     // some code for here and setting **serverURL**

    wsocket.setPauseMode(QAbstractSocket::PauseNever);
    wsocket.open(serverURL);

}

void onSslErrors(const QList<QSslError> &errors)
{
    qDebug() << wsocket.errorString();
}

void onError(const QAbstractSocket::SocketError &socketError)
{
    qDebug() << wsocket.errorString() << " Error:" << socketError;
}

void onConnected()
{
    qDebug() << "Successfully Connected with " << serverURL;
}

void onDisconnected()
{
    qDebug() << wsocket.errorString() << " CloseCode is >>> " << wsocket.closeCode();
}

void onStateChanged(QAbstractSocket::SocketState socketState)
{
    qDebug() << "StateChanged : " << socketState;
}

how to resolve this any one have idea how to connect secure web socket. i did check SSL Echo Client Example but it seems like server side also should be Qt WebSocket.

Q1) How to make secure connection using Qt from client side?

NOTE: server side is configured for secure connection already.

Q2) Server side is not Qt Code and client side is Qt. does it in secure web socket connection? (ie server Socket and client Socket are independent of programming Language for connection or not?)

NOTE: i'm able to connect with same server using none secure connection.

I did found solution after all,

Before I open connection to the remote server using this line,

wsocket.open(serverURL);

I added QSslConfiguration and ignore ssl Error for self signed certificate for development , of course i will correc that in Production.

QSslConfiguration sslConfiguration = wsocket.sslConfiguration();
sslConfiguration.setPeerVerifyMode(QSslSocket::VerifyPeer);
wsocket.setSslConfiguration(sslConfiguration);
wsocket.ignoreSslErrors();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM