aa1807a 2015-06-22 06:48 采纳率: 0%
浏览 1675

Qt Ssl 连接问题 急急急

自己写了个Qt的软件 问题服务器和客户端就是连不上 不知道哪写错了 求大神指点
客户端:
Client::Client(QObject* parent):
QObject(parent)
{
start("127.0.0.1", 6666);
client.setProtocol(QSsl::SslV3);
connect(&client, SIGNAL(encrypted()), this, SLOT(startTransfer()));

}

Client::~Client()
{
client.close();
}

void Client::start(QString address, int port)
{
client.connectToHostEncrypted(address, port);
qDebug()<<"Client has connected the host"<<address<<port;
}

void Client::startTransfer()
{
client.peerCertificate();
client.write("Hello,world", 13);
}

服务器:
Server::Server(QObject *parent):
QTcpServer(parent)
{
server = new QTcpServer;
server->listen(QHostAddress::Any, 6666);
qDebug()<<"Server is listen to the port 6666";

connect(server, SIGNAL(newConnection()), this, SLOT(newConnectionRecognized()));

}

void Server::newConnectionRecognized()
{
qDebug()<<"Server has connected to client";
incomingConnection(server->nextPendingConnection()->socketDescriptor());
}

void Server::incomingConnection(qintptr socket_descriptor)
{
socket = new QSslSocket(this);

connect(socket, SIGNAL(readyRead()), this, SLOT(startRead()));
connect(socket, SIGNAL(encrypted()), this, SLOT(startRead()));
if(socket->setSocketDescriptor(socket_descriptor))
    {
    QByteArray key;
    QByteArray cert;

    QFile file_key("c:/Ssl/bin/server.crt");
    QFile file_cert("c:/Ssl/bin/server.key");

    if(file_key.open(QIODevice::ReadOnly))
    {
        qDebug()<<"Key is OK";
        key = file_key.readAll();
        file_key.close();
    }
    else
    {
        qDebug()<<file_key.errorString();
    }

    if(file_cert.open(QIODevice::ReadOnly))
    {
        qDebug()<<"Certificate is OK";
        cert = file_cert.readAll();
        file_cert.close();
    }
    else
    {
        qDebug()<<file_cert.errorString();
    }

    QSslKey ssl_key(key, QSsl::Rsa);
    QSslCertificate ssl_cert(cert);

    socket->setProtocol(QSsl::SslV3);
    socket->setPrivateKey(ssl_key);
    socket->setLocalCertificate(ssl_cert);

    QSslConfiguration cfg = socket->sslConfiguration();
    cfg.caCertificates();


    socket->startServerEncryption();
    }
if(!socket->setSocketDescriptor(socket_descriptor))
{
    qWarning("Couldn't set socket descriptor!");
    delete socket;
    return;
}

}

void Server::startRead()
{
char buffer[1024] = {0};
socket->read(buffer, socket->bytesAvailable());
qDebug()< socket->close();
}

Server::~Server()
{
delete server;
delete socket;
}

  • 写回答

2条回答 默认 最新

  • oyljerry 2015-06-22 11:22
    关注

    先看看具体错误信息,先根据错误分析一下。

    评论

报告相同问题?

悬赏问题

  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接