1.考试系统用MySQL做数据库,在vs上实现各种功能。
配置环境后运行代码
VS2022/C++配置mysql_烧烤火锅炸鸡!的博客-CSDN博客_vs配置mysql
2.客户端代码
#include
//#include "stdafx.h"
#include
#include
#include
#pragma comment (lib, "WS2_32.lib")
using namespace std;
MYSQL mysql;
SOCKET connectsocket;
SOCKET serversocket;
SOCKET clientsocket;
DWORD WINAPI Fun_send(LPVOID);
DWORD WINAPI Fun_recv(LPVOID);
HANDLE hMUTEX;
MYSQL_RES* res;
MYSQL_ROW column;
MYSQL_FIELD* fd;
char sendBuff[255];
char recvBuff[255];
bool login();//登录用户
bool CreatUser(); //注册用户
bool CreatQue(); //创建试卷表
bool ModifyUser();//修改用户
bool SelectUser();//查询用户
bool DeleteUser();//删除用户
bool DeleteQue();//删除题目
bool ModifyQue();//修改题目
bool SelectQue();//查询题目
bool CreatText();//创建新试卷
int main()
{
mysql_init(&mysql);
if (!(mysql_real_connect(&mysql, "localhost", "root", "123456", "test", 3306, NULL, 0)))
{
printf("Error connect to database:%s\n", mysql_error(&mysql));
}
else
{
printf("connect sucessful\n");
}
//加载套接字
WSADATA wsadata;
int iRet = WSAStartup(MAKEWORD(2, 2), &wsadata);
if (iRet != 0)
{
cout << "套接字库加载失败" << endl;
return -1;
}
else
{
cout << "套接字库加载成功" << endl;
}
SOCKET serversocket = socket(AF_INET, SOCK_STREAM, 0);// 返回 INVALID_SOCKET 创建失败
if (SOCKET_ERROR == serversocket)
{
cout << "套接字创建失败" << endl;
return -1;
}
else
{
cout << "套接字创建成功" << endl;
}
SOCKADDR_IN addrSrv;
addrSrv.sin_family = AF_INET;
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //htonl(INADDR_ANY)
addrSrv.sin_port = htons(11111); //主机端口号
//绑定
iRet = bind(serversocket, (SOCKADDR*)&addrSrv, sizeof(addrSrv));//成功是0
if (SOCKET_ERROR == iRet)
{
cout << "套接字绑定失败" << endl;
return -1;
}
else
{
cout << "套接字绑定成功" << endl;
}
int i = listen(serversocket, 10);
if (SOCKET_ERROR == i)
{
cout << "监听失败" << endl;
return -1;
}
else
{
cout << "监听成功" << endl;
}
SOCKADDR_IN addrclient;
int len = sizeof(SOCKADDR);
connectsocket = accept(serversocket, (SOCKADDR*)&addrclient, &len);
if (INVALID_SOCKET == connectsocket)
{
cout << "接受链接失败" << endl;
return false;
}
else
{
cout << "接受链接成功" << endl;
}
cout << "wait client request..." << endl;
//HANDLE Send_Thread = CreateThread(NULL, 0, Fun_send, 0, 0, NULL);//发送
HANDLE Recv_Thread = CreateThread(NULL, 0, Fun_recv, 0, 0, NULL);//接受
//CloseHandle(Send_Thread);
CloseHandle(Recv_Thread);
hMUTEX = CreateMutex(NULL, FALSE, L"mutex");
while (1)
{
Sleep(100);
}
return 0;
}
DWORD WINAPI Fun_recv(LPVOID)
{
while (1)
{
recv(connectsocket, recvBuff, 255, 0);
if (!strcmp(recvBuff, "1"))
{
if (login() == true)
break;
else
continue;
}
else if (!strcmp(recvBuff, "2"))
{
if (CreatUser() == true)
break;
}
else if (!strcmp(recvBuff, "3"))
exit(0);
}
while (1)
{
recv(connectsocket, recvBuff, 255, 0);
if (!strcmp(recvBuff, "老师"))
{
while (1)
{
strcpy_s(sendBuff, "请输入您想要执行的功能:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "修改试卷请输入1:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "增加试题请输入2:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "删除试题请输入3:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "修改账户请输入4:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "删除账户请输入5:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "增加用户请输入6:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "查看用户请输入7:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "查看试题请输入8:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "退出请输入9:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
if (!strcmp(recvBuff, "1"))
{
ModifyQue();
break;
}
else if (!strcmp(recvBuff, "2"))
{
CreatQue();
break;
}
else if (!strcmp(recvBuff, "3"))
{
DeleteQue();
break;
}
else if (!strcmp(recvBuff, "4"))
{
ModifyUser();
break;
}
else if (!strcmp(recvBuff, "5"))
{
DeleteUser();
break;
}
else if (!strcmp(recvBuff, "6"))
{
CreatUser();
break;
}
else if (!strcmp(recvBuff, "7"))
{
SelectUser();
break;
}
else if (!strcmp(recvBuff, "8"))
{
SelectQue();
break;
}
else if (!strcmp(recvBuff, "9"))
{
exit(0);
}
}
}
if (!strcmp(recvBuff, "学生"))
{
while (1)
{
strcpy_s(sendBuff, "请输入您想要执行的功能:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "修改账户请输入1:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "答题请输入2:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "退出请输入3:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
if (!strcmp(recvBuff, "1"))
{
ModifyUser();
break;
}
else if (!strcmp(recvBuff, "2"))
{
CreatText();
break;
}
else if (!strcmp(recvBuff, "3"))
{
exit(0);
}
}
}
}
return 0;
}
bool SelectQue()
{
cout << "请输入查询条件" << endl;
cout << "全部查询,请输入0" << endl;
cout << "按ID查看,请输入1" << endl;
cout << "按题目查看,请输入2" << endl;
cout << "按分值查看,请输入3" << endl;
string s, sid, sname, sscore;
int flag;
recv(connectsocket, recvBuff, 255, 0);
flag = atoi(recvBuff);
switch (flag)
{
case 0:
s = "select *from que";
break;
case 1:
cout << "请输入查询的ID" << endl;
recv(connectsocket, recvBuff, 255, 0);
sid = recvBuff;
s = "select *from que where id='" + sid + "'";
break;
case 2:
cout << "请输入查询的题目" << endl;
recv(connectsocket, recvBuff, 255, 0);
sname = recvBuff;
s = "select *from que where name='" + sname + "'";
break;
case 3:
cout << "请输入查询的分值" << endl;
recv(connectsocket, recvBuff, 255, 0);
sscore = recvBuff;
s = "select *from que where score='" + sscore + "'";
break;
default:
break;
}
const char* ss = s.c_str();
mysql_query(&mysql, "set names gbk ");
mysql_query(&mysql, ss);
res = mysql_store_result(&mysql);
int r = mysql_num_rows(res);
itoa(r, sendBuff, 10);
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
char field[32][32];
for (int i = 0; fd = mysql_fetch_field(res); i++)
{
strcpy_s(field[i], fd->name);
}
int j = mysql_num_fields(res);
itoa(j, sendBuff, 10);
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
for (int i = 0; i < j; i++)
{
strcpy_s(sendBuff, field[i]);
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
}
cout << endl;
while (column = mysql_fetch_row(res))
{
for (int i = 0; i < j; i++)
{
strcpy_s(sendBuff, column[i]);
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
}
cout << endl;
}
return 0;
}//
bool SelectUser()
{
cout << "请输入查询条件" << endl;
cout << "全部查询,请输入0" << endl;
cout << "按ID查看,请输入1" << endl;
cout << "按角色查看,请输入2" << endl;
cout << "按账号查看,请输入3" << endl;
string s, sid, sname, srole;
int flag;
recv(connectsocket, recvBuff, 255, 0);
flag =atoi(recvBuff);
switch (flag)
{
case 0:
s = "select *from user";
break;
case 1:
cout << "请输入查询的ID" << endl;
recv(connectsocket, recvBuff, 255, 0);
sid=recvBuff;
s = "select *from user where id='" + sid+"'";
break;
case 2:
cout << "请输入查询的角色" << endl;
recv(connectsocket, recvBuff, 255, 0);
srole = recvBuff;
s = "select *from user where role='" + srole + "'";
break;
case 3:
cout << "请输入查询的账号" << endl;
recv(connectsocket, recvBuff, 255, 0);
sname = recvBuff;
s = "select *from user where name='" + sname + "'";
break;
default:
break;
}
const char* ss = s.c_str();
mysql_query(&mysql, "set names gbk ");
mysql_query(&mysql, ss);
res = mysql_store_result(&mysql);
int r = mysql_num_rows(res);
itoa(r, sendBuff, 10);
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
char field[32][32];
for (int i = 0; fd = mysql_fetch_field(res); i++)
{
strcpy_s(field[i], fd->name);
}
int j = mysql_num_fields(res);
itoa(j, sendBuff, 10);
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
for (int i = 0; i < j; i++)
{
strcpy_s(sendBuff, field[i]);
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
}
cout << endl;
while (column = mysql_fetch_row(res))
{
for (int i = 0; i < j; i++)
{
strcpy_s(sendBuff, column[i]);
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
}
cout << endl;
}
return 0;
}
bool DeleteUser()
{
string dname;
strcpy_s(sendBuff, "请输入需要删除账户:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
dname = recvBuff;
string Duser = "delete from user where name=\"" + dname + "\"";
const char* ss = Duser.c_str();
if (mysql_query(&mysql, ss))
{
strcpy_s(sendBuff, "删除用户出错!");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
printf("删除用户出错!");
cout << endl;
return false;
}
else if (mysql_affected_rows(&mysql) == 0)
{
strcpy_s(sendBuff, "删除的用户不存在!");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
cout << "删除的用户不存在!" << endl;
return false;
}
else if (mysql_affected_rows(&mysql) > 0)
{
strcpy_s(sendBuff, "删除用户成功!");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
cout << "删除用户成功!" << endl;
return true;
}
}
bool DeleteQue()
{
string dque;
strcpy_s(sendBuff, "请输入需要删除的题目:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
dque = recvBuff;
string Dque = "delete from que where id=\"" + dque + "\"";
const char* ss = Dque.c_str();
if (mysql_query(&mysql, ss))
{
strcpy_s(sendBuff, "删除题目出错!");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
printf("删除题目出错!");
cout << endl;
return false;
}
else if (mysql_affected_rows(&mysql) == 0)
{
strcpy_s(sendBuff, "删除的题目不存在!");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
cout << "删除的题目不存在!" << endl;
return false;
}
else if (mysql_affected_rows(&mysql) > 0)
{
strcpy_s(sendBuff, "删除题目成功!");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
cout << "删除题目成功!" << endl;
return true;
}
}
bool CreatQue()
{
string id, a, b, c, d, ans, score, name;
strcpy_s(sendBuff, "请输入题号:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
id = recvBuff;
strcpy_s(sendBuff, "请输入要创建题目:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
name = recvBuff;
strcpy_s(sendBuff, "请输入选项A:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
a = recvBuff;
strcpy_s(sendBuff, "请输入选项B:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
b = recvBuff;
strcpy_s(sendBuff, "请输入选项C:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
c = recvBuff;
strcpy_s(sendBuff, "请输入选项D:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
d = recvBuff;
strcpy_s(sendBuff, "请输入的新正确答案:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
ans = recvBuff;
strcpy_s(sendBuff, "请输入新的分值:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
score = recvBuff;
string NewUser = "insert into que values(\"" + id + "\",\"" + name + "\",\"" + a + "\",\"" + b + "\",\"" + c + "\",\"" + d + "\",\"" + ans + "\",\"" + score + "\");";
const char* ss = NewUser.c_str();
mysql_query(&mysql, "set names gbk");//可以用中文
if (mysql_query(&mysql, ss)) //mysql_query函数进行数据库查询 查询获取的ss
{
strcpy_s(sendBuff, "创建失败");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
cout << "创建失败\n" << endl; //mysql_error() 返回上一个创建信息的错误信息
return false;
}
else
{
strcpy_s(sendBuff, "创建成功");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
printf("创建成功\n");
return true;
}
}
bool ModifyQue()
{
string newname, newid, newa, newb, newc, newd, newanswer, newscore, oname;
strcpy_s(sendBuff, "请输入需要修改的题目:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
oname = recvBuff;
strcpy_s(sendBuff, "请输入新的题号:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
newid = recvBuff;
strcpy_s(sendBuff, "请输入要创建的新题目:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
newname = recvBuff;
strcpy_s(sendBuff, "请输入新的选项A:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
newa = recvBuff;
strcpy_s(sendBuff, "请输入新的选项B:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
newb = recvBuff;
strcpy_s(sendBuff, "请输入新的选项C:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
newc = recvBuff;
strcpy_s(sendBuff, "请输入新的选项D:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
newd = recvBuff;
strcpy_s(sendBuff, "请输入的新正确答案:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
newanswer = recvBuff;
strcpy_s(sendBuff, "请输入新的分值:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
newscore = recvBuff;
string Mque = "select *from que where name=\"" + newname + "\"";
const char* ss = Mque.c_str();
mysql_query(&mysql, ss);
mysql_query(&mysql, "set names gbk");//可以用中文
res = mysql_store_result(&mysql);
if (mysql_num_rows(res) > 0)//查询相同的数量
{
strcpy_s(sendBuff, "新题目和表中已存在的题目有重复");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
cout << "新题目和表中已存在的题目有重复" << endl;
return false;
}
string tt = " update que set id=\"" + newid + "\"," + "name=\"" + newname + "\"," + "a=\"" + newa + "\"," + "b=\"" + newb + "\"," + "c=\"" + newc + "\"," + "d=\"" + newd + "\"," + "answer=\"" + newanswer + "\"," + "score=\"" + newscore + "\"where name=\"" + oname + "\"";
ss = tt.c_str();
if (mysql_query(&mysql, ss)) //mysql_query函数进行数据库查询 查询获取的ss
{
strcpy_s(sendBuff, "修改失败");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
cout << "修改失败\n" << endl;
//return false;
}
else if (mysql_affected_rows(&mysql) == 0) //受影响的行 查询更新之后有影响的行
{
strcpy_s(sendBuff, "修改题目不存在");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
printf("修改题目不存在\n");
return false;
}
else if (mysql_affected_rows(&mysql) > 0) //受影响的行 查询更新之后有影响的行
{
strcpy_s(sendBuff, "修改成功");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
printf("修改成功\n");
return true;
}
return true;
}
bool ModifyUser()
{
string newname, newid, newpsw, newrole, oname;
strcpy_s(sendBuff, "请输入需要修改的账号:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
oname = recvBuff;
strcpy_s(sendBuff, "请输入要创建的新id:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
newid = recvBuff;
strcpy_s(sendBuff, "请输入你新的账号:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
newname = recvBuff;
cout << newname;
strcpy_s(sendBuff, "请输入你的新密码:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
newpsw = recvBuff;
strcpy_s(sendBuff, "请输入你的新身份:");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
recv(connectsocket, recvBuff, 255, 0);
newrole = recvBuff;
string Muser = "select *from user where name=\"" + newname + "\"";
const char* ss = Muser.c_str();
mysql_query(&mysql, ss);
// mysql_query(&mysql, "set names gbk");//可以用中文
res = mysql_store_result(&mysql);
if (mysql_num_rows(res) > 0)
{
strcpy_s(sendBuff, "新帐号的名字和表中已存在的用户名有重复");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
cout << "新帐号的名字和表中已存在的用户名有重复" << endl;
return false;
}
string qq = " update user set id=\"" + newid + "\"," + "name=\"" + newname + "\"," + "password=\"" + newpsw + "\"where name=\"" + oname + "\"";
ss = qq.c_str();
if (mysql_query(&mysql, ss)) //mysql_query函数进行数据库查询 查询获取的ss
{
strcpy_s(sendBuff, "修改失败\n");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
cout << "修改失败\n" << endl;
//return false;
}
else if (mysql_affected_rows(&mysql) == 0) //受影响的行 查询更新之后有影响的行
{
strcpy_s(sendBuff, "修改用户不存在\n");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
printf("修改用户不存在\n");
return false;
}
else if (mysql_affected_rows(&mysql) > 0) //受影响的行 查询更新之后有影响的行
{
strcpy_s(sendBuff, "修改成功\n");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
printf("修改成功\n");
return true;
}
return true;
}
bool CreatText()
{
int tscore = 0;
int number[100];
int n;
int score[100];
string rans[100];
string yans[100];
string qs;
string nums = "select *from que";
mysql_query(&mysql, nums.c_str());
mysql_query(&mysql, "set names gbk");
res = mysql_store_result(&mysql); //先查询是题库中共有多少题
int num = mysql_num_rows(res); //有多少行
strcpy_s(sendBuff, "请输入考试试题数量");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入考试试题数量" << endl;
recv(connectsocket, recvBuff, 255, 0);
n = atoi(recvBuff);
srand(time(NULL)); // time函数 为rand提供随机种子 保证每次获取的rand不同
if (n > num)
return false;
for (int i = 0; i < n; i++)
{
number[i] = rand() % num + 1; //随机函数 随即返回一个值0-10的数
for (int j = 0; j < i; j++)//查重
{
if (number[i] == number[j])
{
i--;
}
}
}
for (int i = 0; i < n; i++)
{
cout << "输出的第" << i + 1 << "题" << endl;
int q = number[i] - 1;
string str = to_string(q);
qs = "select *from que limit " + str + ",1";// 从k行到m行里查询
mysql_query(&mysql, qs.c_str()); // 查询
res = mysql_store_result(&mysql); //保存
column = mysql_fetch_row(res);
strcpy_s(sendBuff, column[1]);
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, column[2]);
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, column[3]);
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, column[4]);
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, column[5]);
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
rans[i] = column[6];
score[i] = atoi(column[7]);
cout << "请输入第" << i + 1 << "题答案" << endl;
recv(connectsocket, recvBuff, 255, 0);
if (recvBuff == rans[i])
{
tscore = tscore + score[i];
}
}
cout << "你的考试成绩是" << tscore << endl;
itoa(tscore, sendBuff, 10);
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
}
bool login()
{
string logid, logpwd;
recv(connectsocket, recvBuff, 255, 0);
logid = recvBuff;
cout << endl;
recv(connectsocket, recvBuff, 255, 0);
logpwd = recvBuff;
//执行查询mysql语句
string sql = "select *from user where id='" + logid + "'and password='" + logpwd + "'";
mysql_query(&mysql, "set names gbk");//可以使用中文
const char* ss = sql.c_str();
mysql_query(&mysql, ss);
res = mysql_store_result(&mysql);
if (mysql_num_rows(res) == 0)
{
strcpy_s(sendBuff, "mysql_query failed!");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
cout << "mysql_query failed!" << " " << sql.c_str() << "" << mysql_error(&mysql) << endl;
return false;
}
else
{
strcpy_s(sendBuff, "mysql_query success!");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
MYSQL_ROW row;
row = mysql_fetch_row(res);
cout << row[1] << " " << row[2] << " " << row[3] << " " << endl;
strcpy_s(sendBuff, row[3]);
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
return true;
}
}
bool CreatUser()
{
string cid, cname, cpwd, crol;
recv(connectsocket, recvBuff, 255, 0);
cid = recvBuff;
recv(connectsocket, recvBuff, 255, 0);
cname = recvBuff;
recv(connectsocket, recvBuff, 255, 0);
cpwd = recvBuff;
recv(connectsocket, recvBuff, 255, 0);
crol = recvBuff;
string NewUser = "insert into user values(\"" + cid + "\",\"" + cname + "\",\"" + cpwd + "\",\"" + crol + "\");";
const char* ss = NewUser.c_str();
mysql_query(&mysql, "set names gbk");//可以用中文
if (mysql_query(&mysql, ss)) //mysql_query函数进行数据库查询 查询获取的ss
{
strcpy_s(sendBuff, "创建失败");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
cout << "创建失败\n" << endl; //mysql_error() 返回上一个创建信息的错误信息
//printf("创建失败\n",mysql_error(&mysql));
return false;
}
else
{
strcpy_s(sendBuff, "创建成功");
send(connectsocket, sendBuff, sizeof(sendBuff), 0);
printf("创建成功\n");
return true;
}
// return 0;
}
3.用户端代码
#include
#include
#include
#include
#pragma comment (lib,"WS2_32.lib")
using namespace std;
MYSQL mysql;
SOCKET connectsocket;
SOCKET serversocket;
SOCKET clientsocket;
DWORD WINAPI Fun_send(LPVOID);
DWORD WINAPI Fun_recv(LPVOID);
HANDLE hMUTEX;
char sendBuff[255];
char recvBuff[255];
char role[255];
int main()
{
WSADATA wsadata;
int iRet = WSAStartup(MAKEWORD(2, 2), &wsadata);
if (iRet != 0)
{
cout << "套接字库加载失败" << endl;
return -1;
}
else
{
cout << "套接字库加载成功" << endl;
}
clientsocket = socket(AF_INET, SOCK_STREAM, 0);
if (SOCKET_ERROR == clientsocket)
{
cout << "套接字库创建失败" << endl;
return -1;
}
else
{
cout << "套接字库创建成功" << endl;
}
SOCKADDR_IN addrSrv;
addrSrv.sin_family = AF_INET;
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
addrSrv.sin_port = htons(11111);
int i = connect(clientsocket, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
if (SOCKET_ERROR == i)
{
cout << "链接失败" << endl;
}
else
{
cout << "链接成功" << endl;
}
HANDLE Send_Thread = CreateThread(NULL, 0, Fun_send, 0, 0, NULL);//发送
//HANDLE Recv_Thread = CreateThread(NULL, 0, Fun_recv, 0, 0, NULL);//接受
CloseHandle(Send_Thread);
//CloseHandle(Recv_Thread);
hMUTEX = CreateMutex(NULL, FALSE, L"mutex");
while (1)
{
Sleep(100);
}
return 0;
}
DWORD WINAPI Fun_send(LPVOID)
{
while (1)
{
cout << "<-----欢迎进入考试管理系统!------>" << endl;
cout << "<-----登录请按1------>" << endl;
cout << "<-----注册请按2------>" << endl;
cout << "<-----退出请按3------>" << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
if (!strcmp(sendBuff, "1"))
{
cout << "请输入账号=";
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入密码=";
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
if (!strcmp(recvBuff, "mysql_query success!"))
{
recv(clientsocket, recvBuff, 255, 0);
cout << "登陆的角色为" << recvBuff << endl;
strcpy_s(role, recvBuff);
break;
}
continue;
}
else if (!strcmp(sendBuff, "2"))
{
cout << "请输入你的学号:" << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入要创建的账号:" << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入你的密码:" << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入你的身份:" << endl;
cin >> sendBuff;
strcpy_s(role, sendBuff);
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
if (!strcmp(recvBuff, "创建成功"))
break;
else
continue;
}
else if (!strcmp(sendBuff, "3"))
{
return 0;
}
}
cout << role;
while (1)
{
strcpy_s(sendBuff, role);
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
if (!strcmp(role, "老师"))
{
while (1)
{
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
if (!strcmp(sendBuff, "1"))
{
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
break;
}
else if (!strcmp(sendBuff, "2"))
{
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
break;
}
else if (!strcmp(sendBuff, "3"))
{
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
break;
}
else if (!strcmp(sendBuff, "4"))
{
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
break;
}
else if (!strcmp(sendBuff, "5"))
{
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff;
break;
}
else if (!strcmp(sendBuff, "6"))
{
cout << "请输入你的学号:" << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入要创建的账号:" << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入你的密码:" << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入你的身份:" << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
break;
}
else if (!strcmp(sendBuff, "7"))
{
cout << "请输入查询条件" << endl;
cout << "全部查询,请输入0" << endl;
cout << "按ID查看,请输入1" << endl;
cout << "按角色查看,请输入2" << endl;
cout << "按账号查看,请输入3" << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
if (!strcmp(sendBuff, "1"))
{
cout << "请输入查询的ID" << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
}
if (!strcmp(sendBuff, "2"))
{
cout << "请输入查询的角色" << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
}
if (!strcmp(sendBuff, "3"))
{
cout << "请输入查询的账号" << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
}
recv(clientsocket, recvBuff, 255, 0);
int r = atoi(recvBuff);
recv(clientsocket, recvBuff, 255, 0);
int j = atoi(recvBuff);
for (int i = 0; i < j; i++)
{
recv(clientsocket, recvBuff, 255, 0);
printf("%10s\t", recvBuff);
}
cout << endl;
while (r!=0)
{
for (int i = 0; i < j; i++)
{
recv(clientsocket, recvBuff, 255, 0);
printf("%10s\t", recvBuff);
}
r--;
cout << endl;
}
break;
}
else if (!strcmp(sendBuff, "8"))
{
cout << "请输入查询条件" << endl;
cout << "全部查询,请输入0" << endl;
cout << "按ID查看,请输入1" << endl;
cout << "按题目查看,请输入2" << endl;
cout << "按分值查看,请输入3" << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
if (!strcmp(sendBuff, "1"))
{
cout << "请输入查询的ID" << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
}
if (!strcmp(sendBuff, "2"))
{
cout << "请输入查询的题目" << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
}
if (!strcmp(sendBuff, "3"))
{
cout << "请输入查询的分值" << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
}
recv(clientsocket, recvBuff, 255, 0);
int r = atoi(recvBuff);
recv(clientsocket, recvBuff, 255, 0);
int j = atoi(recvBuff);
for (int i = 0; i < j; i++)
{
recv(clientsocket, recvBuff, 255, 0);
printf("%8s\t", recvBuff);
}
cout << endl;
while (r != 0)
{
for (int i = 0; i < j; i++)
{
recv(clientsocket, recvBuff, 255, 0);
printf("%8s\t", recvBuff);
}
r--;
cout << endl;
}
break;
}
else if (!strcmp(sendBuff, "9"))
{
exit(0);
}
}
}
else if (!strcmp(role, "学生"))
{
while (1)
{
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
if (!strcmp(sendBuff, "1"))
{
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
break;
}
else if (!strcmp(sendBuff, "2"))
{
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
int n = atoi(sendBuff);
for (int i = 0; i < n; i++)
{
recv(clientsocket, recvBuff, 255, 0);
cout << i + 1 << ")" << recvBuff << endl;
recv(clientsocket, recvBuff, 255, 0);
cout << "(A):" << recvBuff << endl;
recv(clientsocket, recvBuff, 255, 0);
cout << "(B):" << recvBuff << endl;
recv(clientsocket, recvBuff, 255, 0);
cout << "(C):" << recvBuff << endl;
recv(clientsocket, recvBuff, 255, 0);
cout << "(D):" << recvBuff << endl;
cout << "请输入答案:" << endl;
cin >> sendBuff;
send(clientsocket, sendBuff, sizeof(sendBuff), 0);
}
cout << "你的考试成绩是:" << endl;
recv(clientsocket, recvBuff, 255, 0);
cout << recvBuff << endl;
break;
}
else if (!strcmp(sendBuff, "3"))
{
exit(0);
}
}
}
}
return 0;
}
4.数据库详细