自己写了个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;
}