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

}

查看全部
aa1807a
aa1807a
2015/06/19 06:09
  • t sl
  • 点赞
  • 收藏
  • 回答
    私信

2个回复