gpt4 book ai didi

c++ - 如何在 Qt C++ 中使用 Secure WebSocket 连接服务器

转载 作者:太空宇宙 更新时间:2023-11-03 14:26:35 24 4
gpt4 key购买 nike

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

我使用的协议(protocol)是 WebSocket。我使用库进行连接。

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

QsslSocket::supportsSsl() 返回false,然后发现我缺少 openssl 库文件(libeay32.dll、ssleay32.dll ) 然后我将这些文件放在我的应用程序的路径中并再次运行代码,现在 QSslSocket::supportsSsl() 正在返回 true。但是当我尝试连接 websocket 时,它显示了以下错误。

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)

来自客户端 (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;
}

如何解决这个任何知道如何连接安全网络套接字的人。我检查了SSL Echo Client Example但似乎服务器端也应该是 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? (i.e. 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.

最佳答案

毕竟我确实找到了解决方案,

在我使用这条线打开到远程服务器的连接之前,

wsocket.open(serverURL);

我添加了 QSslConfiguration 并忽略了用于开发的自签名证书的 ssl 错误,当然我会在生产中更正它。

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

关于c++ - 如何在 Qt C++ 中使用 Secure WebSocket 连接服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56504297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com