2021_9_2Qt-main.zip
大小:3.22MB
价格:30积分
下载量:0
评分:
5.0
上传者:m0_74810392
更新日期:2025-09-22

2021_9_2Qt-main.zip

资源文件列表(大概)

文件名
大小
2021_9_2Qt-main/
-
2021_9_2Qt-main/client/
-
2021_9_2Qt-main/client/.DS_Store
6KB
2021_9_2Qt-main/client/Client.qrc
1.73KB
2021_9_2Qt-main/client/QT/
-
2021_9_2Qt-main/client/QT/icon/
-
2021_9_2Qt-main/client/QT/icon/Add.png
8.04KB
2021_9_2Qt-main/client/QT/icon/AddAccount.png
6.28KB
2021_9_2Qt-main/client/QT/icon/Back.png
7.79KB
2021_9_2Qt-main/client/QT/icon/BackGround_4.jpg
678.33KB
2021_9_2Qt-main/client/QT/icon/Back_purple.png
6.45KB
2021_9_2Qt-main/client/QT/icon/Background_1.jpg
654.44KB
2021_9_2Qt-main/client/QT/icon/Background_2.jpg
362.79KB
2021_9_2Qt-main/client/QT/icon/Background_3.jpg
755.05KB
2021_9_2Qt-main/client/QT/icon/Background_5.jpg
313.03KB
2021_9_2Qt-main/client/QT/icon/Background_6.jpg
374.89KB
2021_9_2Qt-main/client/QT/icon/Chat.png
8.26KB
2021_9_2Qt-main/client/QT/icon/Chat_icon.png
4.06KB
2021_9_2Qt-main/client/QT/icon/Closed.png
9.62KB
2021_9_2Qt-main/client/QT/icon/Delete.png
5.21KB
2021_9_2Qt-main/client/QT/icon/File.png
3.42KB
2021_9_2Qt-main/client/QT/icon/Login.png
5.75KB
2021_9_2Qt-main/client/QT/icon/LoginOut_blue.png
4.67KB
2021_9_2Qt-main/client/QT/icon/Login_Blue.png
4.64KB
2021_9_2Qt-main/client/QT/icon/Login_Red.png
6.77KB
2021_9_2Qt-main/client/QT/icon/Login_lightBlue.png
3.15KB
2021_9_2Qt-main/client/QT/icon/Logout.png
5.51KB
2021_9_2Qt-main/client/QT/icon/Me.png
6.85KB
2021_9_2Qt-main/client/QT/icon/Message.png
6.73KB
2021_9_2Qt-main/client/QT/icon/More Circle.png
7.5KB
2021_9_2Qt-main/client/QT/icon/Password.png
6.62KB
2021_9_2Qt-main/client/QT/icon/Refresh.png
7.13KB
2021_9_2Qt-main/client/QT/icon/Register.png
4.71KB
2021_9_2Qt-main/client/QT/icon/Search.png
5.7KB
2021_9_2Qt-main/client/QT/icon/Send.png
6.29KB
2021_9_2Qt-main/client/QT/icon/SendFile.png
7.29KB
2021_9_2Qt-main/client/QT/icon/SendFile_blue.png
7.84KB
2021_9_2Qt-main/client/QT/icon/SlectFile.png
8.84KB
2021_9_2Qt-main/client/QT/icon/confirPwd_green.png
7.87KB
2021_9_2Qt-main/client/QT/icon/go.png
9.95KB
2021_9_2Qt-main/client/QT/icon/login_Account.png
6.72KB
2021_9_2Qt-main/client/QT/icon/login_密码.png
4.63KB
2021_9_2Qt-main/client/QT/icon/name_green.png
4.51KB
2021_9_2Qt-main/client/QT/icon/name_lightgreen.png
7.79KB
2021_9_2Qt-main/client/QT/icon/password_green.png
6.95KB
2021_9_2Qt-main/client/QT/icon/talk.png
12.27KB
2021_9_2Qt-main/client/QT/icon/toRight.png
8.2KB
2021_9_2Qt-main/client/VolumeConfiguration.plist
4.35KB
2021_9_2Qt-main/client/chatdialog.cpp
4.32KB
2021_9_2Qt-main/client/chatdialog.h
587B
2021_9_2Qt-main/client/chatdialog.ui
4.16KB
2021_9_2Qt-main/client/client.cpp
3.61KB
2021_9_2Qt-main/client/client.h
704B
2021_9_2Qt-main/client/client.pro
997B
2021_9_2Qt-main/client/client.pro.user
22.52KB
2021_9_2Qt-main/client/client.ui
11.29KB
2021_9_2Qt-main/client/clientserve.cpp
2.51KB
2021_9_2Qt-main/client/clientserve.h
415B
2021_9_2Qt-main/client/clientserve.ui
392B
2021_9_2Qt-main/client/home.cpp
17.33KB
2021_9_2Qt-main/client/home.h
782B
2021_9_2Qt-main/client/home.ui
17.18KB
2021_9_2Qt-main/client/main.cpp
163B
2021_9_2Qt-main/client/receivefiledialog.cpp
3.62KB
2021_9_2Qt-main/client/receivefiledialog.h
640B
2021_9_2Qt-main/client/receivefiledialog.ui
3.61KB
2021_9_2Qt-main/client/registerdialog.cpp
4.17KB
2021_9_2Qt-main/client/registerdialog.h
880B
2021_9_2Qt-main/client/registerdialog.ui
13.77KB
2021_9_2Qt-main/client/sendfiledialog.cpp
4KB
2021_9_2Qt-main/client/sendfiledialog.h
774B
2021_9_2Qt-main/client/sendfiledialog.ui
6.04KB
2021_9_2Qt-main/client/userinfo.cpp
147B
2021_9_2Qt-main/client/userinfo.h
246B
2021_9_2Qt-main/server/
-
2021_9_2Qt-main/server/.DS_Store
6KB
2021_9_2Qt-main/server/main.cpp
164B
2021_9_2Qt-main/server/server.cpp
22.38KB
2021_9_2Qt-main/server/server.h
542B
2021_9_2Qt-main/server/server.pro
602B
2021_9_2Qt-main/server/server.ui
1.18KB

资源内容介绍

2021_9_2Qt-main.zip
#include "server.h"#include "ui_server.h"#include <QTcpServer>#include <QTcpSocket>#include <QSqlDatabase>#include <QSqlQuery>#include <QSqlError>#include <QDateTime>int currentsize;server::server(QWidget *parent) : QMainWindow(parent) , ui(new Ui::server){ ui->setupUi(this); tcpServer = new QTcpServer(); ui->listWidget->clear(); ui->listWidget->insertItem(0,tr("当前无在线用户")); for(int i = 0; i < M; i++) { tcpSocket[i]=new QTcpSocket(); } tcpServer->listen(QHostAddress::Any,8888); //init db = QSqlDatabase::addDatabase("QSQLITE"); //判断是否建立了用户表 db.setDatabaseName("./people.db"); db.open(); QSqlQuery sqlquery; sqlquery.exec("CREATE TABLE if not exists people (id INTEGER NOT NULL UNIQUE,name TEXT NOT NULL UNIQUE,password TEXT NOT NULL,ip TEXT,islogin INTEGER NOT NULL,PRIMARY KEY(id AUTOINCREMENT))"); db.close(); connect(tcpServer,&QTcpServer::newConnection,[=](){ tcpSocket[0] = tcpServer->nextPendingConnection(); currentsize++; QString ip = tcpSocket[0]->peerAddress().toString().section(":",3,3); int port = tcpSocket[0]->peerPort(); //预留currentsize以用作多用户同时连接所用 QString str = QString("[%1:%2]").arg(ip).arg(port); qDebug() << str ; connect(tcpSocket[0],&QTcpSocket::readyRead,[=](){ //读取缓冲区数据 QByteArray buffer = tcpSocket[0]->readAll(); if("login" == QString(buffer).section("##",0,0)) {//登陆 db.setDatabaseName("./people.db"); db.open(); QSqlQuery sqlquery; sqlquery.prepare("select * from people where name = :name"); sqlquery.bindValue(":name",QString(buffer).section("##",1,1)); sqlquery.exec(); if(!sqlquery.next()) {//未查找到该用户 tcpSocket[0]->write(QString("login error##no_user").toUtf8()); tcpSocket[0]->flush(); db.close(); } else {//用户存在 int id = sqlquery.value(0).toInt(); QString pwd = sqlquery.value(2).toString(); if(pwd == QString(buffer).section("##",2,2)) {//登录成功 tcpSocket[0]->write(QString("login successed##%1").arg(id).toUtf8()); sqlquery.prepare("update people set ip=:ip, islogin=1 where name = :name"); sqlquery.bindValue(":ip",ip); sqlquery.bindValue(":name",QString(buffer).section("##",1,1)); sqlquery.exec(); tcpSocket[0]->flush(); //更新服务器界面 ui->listWidget->clear(); sqlquery.prepare("select * from people where islogin = 1"); sqlquery.exec(); if(sqlquery.next()) { QString userid = sqlquery.value(0).toString(); QString username = sqlquery.value(1).toString(); QString userip = sqlquery.value(3).toString(); //qDebug()<<userid; ui->listWidget->insertItem(0,"用户ID:"+userid+",用户昵称:"+username+",用户IP:"+userip); int rownum = 1; while (sqlquery.next()) { QString userid = sqlquery.value(0).toString(); QString username = sqlquery.value(1).toString(); QString userip = sqlquery.value(3).toString(); ui->listWidget->insertItem(rownum,"用户ID:"+userid+",用户昵称:"+username+",用户IP:"+userip); rownum++; } } else { ui->listWidget->clear(); ui->listWidget->insertItem(0,tr("当前无在线用户")); } } else {//密码错误 tcpSocket[0]->write(QString("login error##errpwd").toUtf8()); tcpSocket[0]->flush(); db.close(); } } } else if("register" == QString(buffer).section("##",0,0)) {//注册环节 db.setDatabaseName("./people.db"); db.open(); QSqlQuery sqlquery; //注册用户的时候需要进行判重 sqlquery.prepare("select * from people where name = :name"); sqlquery.bindValue(":name",QString(buffer).section("##",1,1)); sqlquery.exec(); if(!sqlquery.next()) {//可以新建 sqlquery.clear(); sqlquery.prepare("insert into people values (null,:name,:password,null,0)"); sqlquery.bindValue(":name",QString(buffer).section("##",1,1)); sqlquery.bindValue(":password",QString(buffer).section("##",2,2)); sqlquery.exec(); sqlquery.clear(); sqlquery.prepare("select * from people where name = :name"); sqlquery.bindValue(":name",QString(buffer).section("##",1,1)); sqlquery.exec();//获得新建的用户的id sqlquery.next(); int newid = sqlquery.value(0).toInt(); sqlquery.exec("create table if not exists friend__" + QString::number(newid) +"(id INTEGER unique, name TEXT,sendmassage INTEGER,sendfile INTEGER)"); tcpSocket[0]->write(QString("register successed").toUtf8()); tcpSocket[0]->flush(); db.close(); } else {//有重名 tcpSocket[0]->write(QString("register error##same_name").toUtf8()); tcpSocket[0]->flush(); db.close(); } } else if("wantsendmessage" == QString(buffer).section("##",0,0)) {//想发信息,校验有没有这个人 db.setDatabaseName("./people.db"); db.open(); QSqlQuery sqlquery; sqlquery.prepare("select * from people where name = :name"); sqlquery.bindValue(":name",QString(buffer).section("##",2,2)); sqlquery.exec(); if(sqlquery.next()) {//有这个人在,可以发消息 int otherid = sqlquery.value(0).toInt(); tcpSocket[0]->write(QString("wantsendmessage_ok##%1").arg(otherid).toUtf8()); tcpSocket[0]->flush(); //发消息前把数据库准备好 int thisid = QString(buffer).section("##",1,1).toInt(); if(thisid < otherid) { sqlquery.exec("CREATE TABLE if not exists chat__" + QString::number(thisid) + "__" + QString::number(otherid) + "(time datetime NOT NULL UNIQUE,id INTEGER,message TEXT, PRIMARY KEY(time))"); } else { sqlquery.exec("CREATE TABLE if not exists chat__" + QString::number(otherid) + "__" + QString::number(thisid) + "(time datetime NOT NULL UNIQUE,id INTEGER,message TEXT, PRIMARY KEY(time))"); } db.close(); }

用户评论 (0)

发表评论

captcha