#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->tcpServer = NULL;
this->tcpSocket = NULL;
this->ui->label_IP->setText(this->GetLocalIPAddress());
this->initConnect();
}
MainWindow::~MainWindow()
{
delete ui;
if(this->tcpSocket != NULL) {
this->tcpSocket->close();
delete this->tcpSocket;
}
if(this->tcpServer != NULL) {
this->tcpServer->close();
delete this->tcpServer;
}
}
/**
* @brief MainWindow::initConnect
* @caption 初始化QObject::connect()函数
*/
void MainWindow::initConnect()
{
connect(this->ui->spinBox,SIGNAL(valueChanged(int)),this,SLOT(setPort(int)));
connect(this->ui->pushButton_Start,SIGNAL(clicked()),this,SLOT(startTcpServer()));
connect(this->ui->pushButton_Close,SIGNAL(clicked()),this,SLOT(closeTcpServer()));
}
/**
* @brief MainWindow::setPort
* @param port 端口号
*/
void MainWindow::setPort(int port)
{
this->ui->label_Port->setText(QString::number(port));
}
/**
* @brief MainWindow::startTcpServer 开启服务器
*/
void MainWindow::startTcpServer()
{
this->tcpServer = new QTcpServer(this);
this->tcpServer->listen(QHostAddress::Any,this->ui->spinBox->value());
connect(this->tcpServer,SIGNAL(newConnection()),this,SLOT(newConnect()));
this->ui->label_SS->setText("Running...");
}
/**
* @brief MainWindow::closeTcpServer 关闭服务器
*/
void MainWindow::closeTcpServer()
{
if(this->tcpSocket != NULL) {
this->tcpSocket->close();
}
if(this->tcpServer != NULL) {
this->tcpServer->close();
}
this->ui->label_SS->setText("Stop.");
}
/**
* @brief MainWindow::newConnect 新的Socket连接
*/
void MainWindow::newConnect()
{
this->tcpSocket = this->tcpServer->nextPendingConnection();
connect(this->tcpSocket,SIGNAL(readyRead()),this,SLOT(readMessages()));
}
/**
* @brief MainWindow::verify 执行登录验证的操作
* @param msg 欲验证的加密报文
* @return 验证成功,返回true;否则,返回false。
*/
bool MainWindow::verify(QString msg)
{
//数据库连接
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setDatabaseName("liyang");
db.setUserName("root");
db.setPassword("");
if(!db.open()){
QMessageBox::information(this, "信息提示", "数据库连接失败.",
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes);
db.close();
exit(EXIT_FAILURE);
}
QSqlQuery query;//获取数据库信息
query.exec("select * from ly_user");
//服务器端的数据同样采取加密操作,比较的是加密后的密文是否相同
QCryptographicHash md5(QCryptographicHash::Md5);
//遍历数据库信息,查询判断是否有该用户
while(query.next()) {
QString tmp = query.value(2).toString() + "#" + query.value(1).toString();
md5.addData(tmp.toUtf8());
tmp = "";
tmp.append(md5.result().toHex());
if(tmp == msg) {
return true;
}
}
db.close();
return false;
}
/**
* @brief MainWindow::readMessages 读取发送来的数据
*/
void MainWindow::readMessages()
{
QByteArray qba = this->tcpSocket->readAll();
QString msg = QVariant(qba).toString();
//this->setWindowTitle(msg);
if(this->verify(msg)) {
msg = "true";
} else {
msg = "false";
}
this->sendMessages(msg);
}
/**
* @brief MainWindow::sendMessages 发送回复消息,以说明登录验证是否成功。
* 如果回复"true",表示验证成功;否则,回复为"false",表示验证失败。
*/
void MainWindow::sendMessages(QString msg)
{
this->tcpSocket->write(msg.toStdString().c_str(),
strlen(msg.toStdString().c_str()));
}
/**
* @brief QT获取本机IP地址
*/
QString MainWindow::GetLocalIPAddress()
{
QString vAddress;
#ifdef _WIN32
QHostInfo vHostInfo = QHostInfo::fromName(QHostInfo::localHostName());
QList<QHostAddress> vAddressList = vHostInfo.addresses();
#else
QList<QHostAddress> vAddressList = QNetworkInterface::allAddresses();
#endif
for(int i = 0; i < vAddressList.size(); i++) {
if(!vAddressList.at(i).isNull() &&
vAddressList.at(i) != QHostAddress::LocalHost &&
vAddressList.at(i).protocol() == QAbstractSocket::IPv4Protocol)
{
vAddress = vAddressList.at(i).toString();
break;
}
}
return vAddress;
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
app.rar (13个子文件)
LiYang_LoginClient
mainwindow.ui 3KB
mainwindow.cpp 2KB
LiYang_LoginClient.pro 399B
main.cpp 214B
mainwindow.h 727B
dll
qsqlmysqld.dll 893KB
libmysql.dll 2.29MB
qsqlmysql.dll 79KB
LiYang_Server
mainwindow.ui 7KB
LiYang_Server.pro 398B
mainwindow.cpp 5KB
main.cpp 221B
mainwindow.h 937B
共 13 条
- 1
资源评论
- 楠枫D2015-04-23简单加密。。
- qq_383003402018-05-12为什么运行不成功呢
- 帆满2017-08-22下了之后,感觉太贵了点。天哪!我的十个积分!!!
- 完美废人862013-11-15可以实现MD5加密登录,功能简单了电
失的猛
- 粉丝: 84
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功