客户端
#include <iostream>
#include<Winsock2.h>
#include<string>
#pragma comment (lib,"WS2_32.lib")
using namespace std;
SOCKET coustomsocket;
SOCKET connectsocket;
SOCKET serversocket;
HANDLE hMUTEX;
DWORD WINAPI Fun_send(LPVOID);
DWORD WINAPI Fun_recv(LPVOID);
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;
}
coustomsocket = socket(AF_INET, SOCK_STREAM, 0);// 返回 INVALID_SOCKET 创建失败
if (coustomsocket == INVALID_SOCKET)
{
cout << "创建失败" << endl;
return -1;
}
else
cout << "创建成功" << endl;
//初始化
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //htonl(INADDR_ANY)
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(8000); //主机端口号
int i = connect(coustomsocket, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
if (i == SOCKET_ERROR)
{
cout << "套接字连接失败" << endl;
return -1;
}
else
cout << "套接字连接成功" << endl;
HANDLE send_Thread = CreateThread(NULL, 0, Fun_send, 0, 0, NULL);//发送
CloseHandle(send_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(coustomsocket, sendBuff, sizeof(sendBuff), 0);
if (!strcmp(sendBuff, "1"))
{
cout << "请输入登录账号" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入登录密码" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
/*recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;*/
if (!(strcmp(recvBuff, "登陆成功,你的角色是:")))
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << "登陆的角色为:" << recvBuff;
strcpy_s(role, recvBuff);
break;
}
else
continue;
}
else if (!(strcmp(sendBuff, "2")))
{
cout << "请输入你的新name:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入要创建的新id:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入你的新密码:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入你的新身份:" << endl;
cin >> sendBuff;
strcpy_s(role, sendBuff);
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
if (!(strcmp(recvBuff, "新用户创建成功")))
{
break;
}
else
continue;
}
else if ((!strcmp(sendBuff, "3")))
{
exit(0);
}
}
while (1)
{
strcpy_s(sendBuff, role);
// cout << "\n请输入你的角色:" << endl;
// cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << endl;
cout << "你的角色是" << role;
if (!strcmp(role, "老师"))
{
while (1)
{
cout << "请输入你想要执行的功能:";
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
cin >> sendBuff;//输入数字
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
if (!strcmp(sendBuff, "1"))
{
cout << "请输入需要修改的账号:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入要创建的新id:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入你新的账号:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入你的新密码:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入你的新身份:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
break;
}
if (!strcmp(sendBuff, "2"))
{
cout << "请输入题号:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入选项a:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入选项b:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入选项c:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入选项d:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入标准答案:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入分值:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入题目:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
break;
}
if (!strcmp(sendBuff, "3"))
{
cout << "请输入想要删除的题号:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
break;
}
if (!strcmp(sendBuff, "4"))
{
cout << "请输入需要修改的题号:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入修改的新题号:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入需要修改的题号A的内容:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入需要修改的题号B的内容:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入需要修改的题号C的内容:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入需要修改的题号D的内容:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入新的正确答案:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入新的分数:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入你的新内容:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
break;
}
if (!strcmp(sendBuff, "5"))
{
cout << "请输入想要删除的用户的name" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
break;
}
if (!strcmp(sendBuff, "6"))
{
cout << "请输入你的学号:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入要创建的账号:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入你的密码:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入你的身份:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
break;
}
if (!strcmp(sendBuff, "7"))
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
if (!strcmp(sendBuff, "0"))
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
int r = atoi(recvBuff); //转成整形
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
int ber = atoi(recvBuff); //转成整形
for (int i = 0; i < ber; i++)
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
printf("%-10s", recvBuff);
}
cout << endl;
while (r != 0)
{
for (int i = 0; i < ber; i++)
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
printf("%-10s", recvBuff);
}
r--;
cout << endl;
}
}
if (!strcmp(sendBuff, "1"))
{
cout << "请输入你想查询的id:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
int r = atoi(recvBuff); //转成整形
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
int ber = atoi(recvBuff); //转成整形
for (int i = 0; i < ber; i++)
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
printf("%-10s", recvBuff);
}
cout << endl;
while (r != 0)
{
for (int i = 0; i < ber; i++)
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
printf("%-10s", recvBuff);
}
r--;
cout << endl;
}
}
if (!strcmp(sendBuff, "2"))
{
cout << "请输入你想查询的角色:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
int r = atoi(recvBuff); //转成整形
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
int ber = atoi(recvBuff); //转成整形
for (int i = 0; i < ber; i++)
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
printf("%-10s", recvBuff);
}
cout << endl;
while (r != 0)
{
for (int i = 0; i < ber; i++)
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
printf("%-10s", recvBuff);
}
r--;
cout << endl;
}
}
if (!strcmp(sendBuff, "3"))
{
cout << "请输入你想查询name:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
int r = atoi(recvBuff); //转成整形
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
int ber = atoi(recvBuff); //转成整形
for (int i = 0; i < ber; i++)
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
printf("%-10s", recvBuff);
}
cout << endl;
while (r != 0)
{
for (int i = 0; i < ber; i++)
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
printf("%-10s", recvBuff);
}
r--;
cout << endl;
}
}
break;
}
if (!strcmp(sendBuff, "8"))
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
if (!strcmp(sendBuff, "0"))
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
int r = atoi(recvBuff);
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
int ber = atoi(recvBuff); //转成整形
for (int i = 0; i < ber; i++)
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
printf("%8s", recvBuff);
}
cout << endl;
while (r != 0)
{
for (int i = 0; i < ber; i++)
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
printf("%8s", recvBuff);
}
r--;
cout << endl;
}
}
if (!strcmp(sendBuff, "1"))
{
cout << "请输入id:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
int r = atoi(recvBuff);
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
int ber = atoi(recvBuff); //转成整形
for (int i = 0; i < ber; i++)
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
printf("%8s", recvBuff);
}
cout << endl;
while (r != 0)
{
for (int i = 0; i < ber; i++)
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
printf("%8s", recvBuff);
}
r--;
cout << endl;
}
}
if (!strcmp(sendBuff, "2"))
{
cout << "请输入题目分值:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
int r = atoi(recvBuff);
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
int ber = atoi(recvBuff); //转成整形
for (int i = 0; i < ber; i++)
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
printf("%8s", recvBuff);
}
cout << endl;
while (r != 0)
{
for (int i = 0; i < ber; i++)
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
printf("%8s", recvBuff);
}
r--;
cout << endl;
}
}
break;
}
if (!strcmp(sendBuff, "9"))
{
exit(0);
}
}
}
else if (!strcmp(role,"学生"))
{
while (1)
{
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
cout << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
if (!strcmp(sendBuff, "1"))
{
cout << "请输入需要修改的name:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入要创建的新id:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入你新的name:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入你的新密码:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
cout << "请输入你的新身份:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << recvBuff;
break;
}
if (!strcmp(sendBuff, "2"))
{
cout << "请输入题目个数:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
int y = atoi(sendBuff); //转成整形
for (int i = 0; i < y; i++)
{
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << i + 1 << ")" << recvBuff << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << "(A)" << recvBuff << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << "(B)" << recvBuff << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << "(C)" << recvBuff << endl;
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << "(D)" << recvBuff << endl;
//atoi函数是将字符串转化成常量数值
}
for (int i = 0; i < y; i++)
{
cout << "请输入第" << i+1 << "题答案:" << endl;
cin >> sendBuff;
send(coustomsocket, sendBuff, sizeof(sendBuff), 0);
}
recv(coustomsocket, recvBuff, sizeof(recvBuff), 0);
cout << "你的分数是 " << recvBuff;
break;
}
if (!strcmp(sendBuff, "3"))
{
exit(0);
}
}
}
}
return 0;
}
服务端:
// socked.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include<Winsock2.h>
#include<mysql.h>
#include<string>
#pragma comment(lib,"WS2_32.lib")
using namespace std;
MYSQL mysql;
SOCKET connectSocket;
SOCKET serversocket;
SOCKET coustomsocket;
DWORD WINAPI Fun_send(LPVOID);
DWORD WINAPI Fun_recv(LPVOID);
HANDLE hMUTEX; //句柄
char recvBuff[255];
char sendBuff[255];
bool login();//登录用户
bool ConnectDatabase();//连接数据库 //学生:修改密码 考试 //老师:修改密码 增加删除修改用户/试题
bool CreatUser(); //注册用户
bool CreatQue(); //创建试卷表
bool modifyuser(); //修改用户信息
bool SelectUser(); //选择输出方式
bool DeleteUser(); //删除用户信息
bool DeleteQue(); //删除题目表
bool modifyque(); //修改题目表
bool selectque(); //展示题库
bool creatText();//教师创建试题库
MYSQL_FIELD* fd;
MYSQL_RES* result;
MYSQL_RES* res;
MYSQL_ROW column;
int main()
{
mysql_init(&mysql);
if (!(mysql_real_connect(&mysql, "localhost", "root", "123456", "zz", 3306, NULL, 0)))
{
printf("Error connect to database:%s\n", mysql_error(&mysql));
}
else
{
printf("connect sucessful\n");
}
//加载套接字
WSADATA wasData;
int iret = WSAStartup(MAKEWORD(2, 2), &wasData); //VersionRequired 请求socket版本,wsaData结构体类型 传出参数
// VersionRequired = MAKEWORD(2,2);
if (iret != 0)
{
cout << "加载失败" << endl;
return -1;
}
else
cout << "套接字加载成功" << endl;
//创建套接字
//有三个参数 地址协议族 ipv ipv6 传输协议类型 SOCK_STREAM流式套接字 有连接/ SOCK_DGRA 数据报套接字 无连接 0()代表自动根据协议类型和族选择合适协议
SOCKET serversocket = socket(AF_INET, SOCK_STREAM, 0);// 返回 INVALID_SOCKET 创建失败
if (serversocket == INVALID_SOCKET)
{
cout << "套接字创建失败" << endl;
return -1;
}
else
cout << "套接字创建成功" << endl;
//初始化
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //htonl(INADDR_ANY)
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(8000); //主机端口号
//绑定
iret = bind(serversocket, (SOCKADDR*)&addrSrv, sizeof(addrSrv));//成功是0
if (iret == SOCKET_ERROR)
{
cout << "套接字绑定失败" << endl;
return -1;
}
else
cout << "套接字绑定成功" << endl;
//监听
iret = listen(serversocket, 10);
if (iret == SOCKET_ERROR)
{
cout << "监听失败" << endl;
return -1;
}
else
cout << "监听成功" << endl;
//连接
SOCKADDR_IN addrcoustom;
int len = sizeof(SOCKADDR);
connectSocket = accept(serversocket, (SOCKADDR*)&addrcoustom, &len);
if (INVALID_SOCKET == connectSocket)
{
cout << "接受连接失败" << endl;
return -1;
}
else
cout << "接受连接成功" << endl;
cout << "等待客户端的连接......" << endl;
HANDLE recv_Thread = CreateThread(NULL, 0, Fun_recv, 0, 0, NULL);//接收线程
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, sizeof(recvBuff), 0);
if (!strcmp(recvBuff, "1"))
{
if (login() == true)
break;
else
continue;
}
else if (!strcmp(recvBuff, "2"))
{
if (CreatUser() == true)
break;
else
continue;
}
else if (!strcmp(recvBuff, "3"))
{
exit(0);
}
}
while (1)
{
recv(connectSocket, recvBuff, sizeof(recvBuff), 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, sizeof(recvBuff), 0);
if (!strcmp(recvBuff, "1"))
{
modifyuser();
break;
}
else if (!strcmp(recvBuff, "2"))
{
CreatQue();
break;
}
else if (!strcmp(recvBuff, "3"))
{
DeleteQue();
break;
}
else if (!strcmp(recvBuff, "4"))
{
modifyque();
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"))
{
//return 0;
}
}
}
else if (!(strcmp(recvBuff, "学生")))
{
while (1)
{
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, sizeof(recvBuff), 0);
if (!strcmp(recvBuff, "1"))
{
modifyuser();
break;
}
if (!strcmp(recvBuff, "2"))
{
creatText();
break;
}
if (!strcmp(recvBuff, "3"))
{
return 0;
}
}
}
}
return 0;
}
bool login()
{
string logid, logpsd,role;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
logid = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
logpsd=recvBuff;
string sql = " select *from user where id= '" + logid + "'and pwd ='" + logpsd + "'";
mysql_query(&mysql, "set names gbk");
mysql_query(&mysql, sql.c_str());
MYSQL_RES* result;
result = mysql_store_result(&mysql);
int num = mysql_num_rows(result); //取结果集中的行数
if (num != 0)
{ MYSQL_ROW row;
row = mysql_fetch_row(result);
strcpy_s(sendBuff, "登陆成功,你的角色是:");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, row[3]);
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return true;
}
else
{
strcpy_s(sendBuff, "用户名或密码错误");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return false;
}
mysql_free_result(result);
return 0;
}
bool CreatUser()
{
string cid, cname, cpwd, crol;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
cname = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
cid = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
cpwd = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
crol = recvBuff;
string duser = "select *from user where name=\"" + cname + "\"";
mysql_query(&mysql, "set names gbk");//可以用中文
mysql_query(&mysql, duser.c_str());
res = mysql_store_result(&mysql);
if (mysql_num_rows(res) > 0)
{
strcpy_s(sendBuff, "用户已存在,无法创建");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return false;
}
string NewUser = "insert into user values(\"" + cid + "\",\"" + cname + "\",\"" + cpwd + "\",\"" + crol + "\");";
const char* ss = NewUser.c_str();
if (mysql_query(&mysql, ss))
{
strcpy_s(sendBuff, "创建失败");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return false;
}
else
{
strcpy_s(sendBuff, "新用户创建成功");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return true;
}
}
bool modifyuser()
{
string newname, newid, newpsw, newrole, oname;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
oname = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
newid = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
newname = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
newpsw = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
newrole = recvBuff;
string ModifyUser = "select *from user where name=\"" + newname + "\"";
const char* ss = ModifyUser.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);
return false;
}
string qq = " update user set id=\"" + newid + "\"," + "name=\"" + newname + "\"," + "pwd=\"" + newpsw + "\"," + "role=\"" + newrole + "\"where name=\"" + oname + "\"";
ss = qq.c_str();
if (mysql_query(&mysql, ss)) //mysql_query函数进行数据库查询 查询获取的ss
{
strcpy_s(sendBuff, "修改失败");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return false;
}
else if (mysql_affected_rows(&mysql) == 0) //受影响的行 查询更新之后有影响的行
{
strcpy_s(sendBuff, "修改用户不存在");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return false;
}
else if (mysql_affected_rows(&mysql) > 0) //受影响的行 查询更新之后有影响的行
{
strcpy_s(sendBuff, "修改成功");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return true;
}
return true;
}
bool modifyque()
{
string newid, newa, newb, newc, newd, newscore, newans, newcont, oid;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
oid = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
newid = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
newa = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
newb = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
newc = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
newd = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
newans= recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
newscore = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
newcont = recvBuff;
string ModifyUser = "select *from que where id=\"" + newid + "\"";
const char* ss = ModifyUser.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);
return false;
}
string qq = " update que set id=\"" + newid + "\"," + "a=\"" + newa + "\"," + "b=\"" + newb + "\"," + "c=\"" + newc + "\"," + "d=\"" + newd + "\"," + "ans=\"" + newans + "\"," + "score=\"" + newscore + "\"," + "cont=\"" + newcont + "\"where id=\"" + oid + "\"";
//string qq = " update user set id=\"" + newid + "\"," + "name=\"" + newname + "\"," + "pwd=\"" + newpsw + "\"," + "role=\"" + newrole + "\"where name=\"" + oname + "\"";
ss = qq.c_str();
if (mysql_query(&mysql, ss)) //mysql_query函数进行数据库查询 查询获取的ss
{
strcpy_s(sendBuff, "修改失败");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return false;
}
else if (mysql_affected_rows(&mysql) == 0) //受影响的行 查询更新之后有影响的行
{
strcpy_s(sendBuff, "修改用户不存在");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return false;
}
else if (mysql_affected_rows(&mysql) > 0) //受影响的行 查询更新之后有影响的行
{
strcpy_s(sendBuff, "修改成功");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return true;
}
return true;
}
bool SelectUser()
{
strcpy_s(sendBuff, "请输入查询条件");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "全部查询请输入0");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "按id查看请输入1");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "按角色查看请输入2");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "按name查看请输入3");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
string sid, srole, sname,s;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
if (!strcmp(recvBuff, "0"))
{
s = "select*from user";
mysql_query(&mysql, s.c_str());
res = mysql_store_result(&mysql);
int r = mysql_num_rows(res);
itoa(r, sendBuff, 10);
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
}
else if (!strcmp(recvBuff, "1"))
{
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
sid = recvBuff;
s = "select *from user where id='" + sid + "'";
mysql_query(&mysql, s.c_str());
res = mysql_store_result(&mysql);
int r = mysql_num_rows(res);
itoa(r, sendBuff, 10);
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
}
else if (!strcmp(recvBuff, "2"))
{
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
srole= recvBuff;
s = "select *from user where role='" + srole + "'";
mysql_query(&mysql, s.c_str());
mysql_query(&mysql, "set names gbk");
res = mysql_store_result(&mysql);
int r = mysql_num_rows(res);
itoa(r, sendBuff, 10);
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
}
else if (!strcmp(recvBuff, "3"))
{
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
sname = recvBuff;
s = "select *from user where name='" + sname + "'";
mysql_query(&mysql, s.c_str());
res = mysql_store_result(&mysql);
int r = mysql_num_rows(res);
itoa(r, sendBuff, 10);
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
}
const char* ss = s.c_str();
mysql_query(&mysql, "set names gbk");
mysql_query(&mysql, ss);
res = mysql_store_result(&mysql);
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);
}
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 true;
}
bool selectque()
{
strcpy_s(sendBuff, "请输入查询条件");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "全部查询请输入0");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "按id查看请输入1");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
strcpy_s(sendBuff, "按题目分值查看请输入2");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
string s;
string sid, schoose, sscore;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
if (!strcmp(recvBuff, "0"))
{
s = "select *from que"; //查看user表里全部查看
mysql_query(&mysql, s.c_str());
res = mysql_store_result(&mysql);
int r = mysql_num_rows(res);
itoa(r, sendBuff, 10);
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
}
if (!strcmp(recvBuff, "1"))
{
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
sid = recvBuff;
s = "select *from que where id=" + sid;
mysql_query(&mysql, s.c_str());
res = mysql_store_result(&mysql);
int r = mysql_num_rows(res);
itoa(r, sendBuff, 10);
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
}
else if (!strcmp(recvBuff, "2"))
{
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
sscore = recvBuff;
s = "select *from que where score=" + sscore;
mysql_query(&mysql, s.c_str());
res = mysql_store_result(&mysql);
int r = mysql_num_rows(res);
itoa(r, sendBuff, 10);
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
}
const char* ss = s.c_str(); //调用c_str()让 字符串s变成长的字符指针
mysql_query(&mysql, "set names gbk");
mysql_query(&mysql, ss);
res = mysql_store_result(&mysql); //把结果保存
char field[50][50];
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 true;
}
bool DeleteUser()
{
string dname;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
dname = recvBuff;
string duser = "delete from user where name=\"" + dname + "\"";
const char* ss = duser.c_str();
mysql_query(&mysql, "set names gbk");//可以用中文
if (mysql_query(&mysql, ss))
{
strcpy_s(sendBuff, "删除用户失败");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return false;
}
else if (mysql_affected_rows(&mysql) == 0) //能够删除 但是删除了0条 判断通过删除后是否影响行数 不影响就是什么也没删
{
strcpy_s(sendBuff, "删除用户不存在");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return false;
}
else if (mysql_affected_rows(&mysql) > 0)
{
strcpy_s(sendBuff, "删除用户成功");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return true;
}
}
bool DeleteQue()
{
string dque;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
dque = recvBuff;
string duser = "delete from que where id=\"" + dque + "\"";
const char* ss = duser.c_str();
if (mysql_query(&mysql, ss))
{
strcpy_s(sendBuff, "删除题目失败");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return false;
}
else if (mysql_affected_rows(&mysql) == 0) //能够删除 但是删除了0条 判断通过删除后是否影响行数 不影响就是什么也没删
{
strcpy_s(sendBuff, "删除的题目不存在");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return false;
}
else if (mysql_affected_rows(&mysql) > 0)
{
strcpy_s(sendBuff, "删除题目成功");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return true;
}
}
bool creatText()
{
int tscore = 0;
int n;
int number[100];
char mm[100];
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); //有多少行
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
int ber = atoi(recvBuff);
srand(time(NULL)); // time函数 为rand提供随机种子 保证每次获取的rand不同
if (ber > num)
return false;
for (int i = 0; i < ber; 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 < ber; i++)
{
int q = number[i] - 1;
string str = to_string(q); //为了将数字常量转化为字符串
qs = "select *from que limit " + str + ",1"; // select *from user a,b是从第a+1行到第b+1行所包含的数据
mysql_query(&mysql, qs.c_str()); // 查询
res = mysql_store_result(&mysql); //保存
column = mysql_fetch_row(res);
strcpy_s(sendBuff, column[7]);
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
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);
rans[i] = column[5];
score[i] = atoi(column[6]); //atoi函数是将字符串转化成常量数值
}
for (int i = 0; i < ber; i++)
{
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
yans[i] = recvBuff;
if (yans[i] == rans[i])
{
tscore = tscore + score[i];
}
}
itoa(tscore, sendBuff, 10);
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return 0;
}
bool CreatQue()
{
string id, a, b, c, d, ans, score, name;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
id = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
a = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
b= recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
c = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
d = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
ans = recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
score= recvBuff;
recv(connectSocket, recvBuff, sizeof(recvBuff), 0);
name = recvBuff;
string NewUser = "insert into que values(\"" + id + "\",\"" + a + "\",\"" + b + "\",\"" + c + "\",\"" + d + "\",\"" + ans + "\",\"" + score + "\",\"" + name + "\");";
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);
return false;
}
else
{
strcpy_s(sendBuff, "创建成功");
send(connectSocket, sendBuff, sizeof(sendBuff), 0);
return true;
}
}
服务端和客户端在线考试系统
最新推荐文章于 2024-08-19 22:23:29 发布