2 aa1807a aa1807a 于 2015.06.19 14:09 提问

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();

}

2个回答

oyljerry
oyljerry   Ds   Rxr 2015.06.19 14:39

你发起的连接,被server拒绝了,是否允许你连接

aa1807a
aa1807a   2015.06.19 15:03

才发现 例题的server里面 加了个 QMAKE_CXXFLAGS += -std=c++11......

Csdn user default icon
上传中...
上传图片
插入图片