问题描述
- QtSsl 出现the remote host closed connection 问题
- 刚开始学QtSsl 一直找不到什么例子 好不容易看到个 问题是 运行的时候 Client出现了the remote host closed connection的问题 代码如下:
ClientExample::ClientExample(QObject *parent):
QObject(parent)
{}
void ClientExample::run()
{
QString hostName = ""127.0.0.1"";
quint16 port = 23333;QByteArray cert;QSslSocket client_socket;QFile file_cert(""c:\Ssl\bin\servercert.pem"");if(file_cert.open(QIODevice::ReadOnly)){ qDebug()<<""Cert is Ok""; cert = file_cert.readAll(); file_cert.close();}else{ qDebug()<<file_cert.errorString();}QSslCertificate ssl_cert(cert);client_socket.addCaCertificate(ssl_cert);client_socket.connectToHostEncrypted(hostName port);qDebug()<<""Client has connect the IP""<<hostName<<port;if(client_socket.waitForEncrypted(-1)){ qDebug()<<""Connected""; client_socket.write(""Hello World!""); if(client_socket.waitForBytesWritten(-1)) qDebug()<<""Message sent""; else qDebug().nospace()<<""Error: Coulen't send message (""<<qPrintable(client_socket.errorString())<<"")""; while(!client_socket.waitForDisconnected()){ QThread::msleep(10); qDebug()<<""Disconnected""; }}else{ qDebug().nospace()<<""Error: Couldn't not establish encrypted connection (""<<qPrintable(client_socket.errorString())<<"")"";}this->deleteLater();QThread::currentThread()->quit();qApp->exit();
}
解决方案
你发起的连接,被server拒绝了,是否允许你连接
解决方案二:
才发现 例题的server里面 加了个 QMAKE_CXXFLAGS += -std=c++11......
时间: 2024-12-06 15:13:13