服务器代码:
#include <stdio.h>
#include <sqlite3.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdlib.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
}while(0);
#define IP "192.168.250.100"
#define PORT 6666
#define ROOT_REGION "root用户登录"
#define PUT_REGION "普通用户登录"
#define ROOT_REGIN_GO 1
#define PUT_REGIN_GO 0
#define NEW_ROOT_INIT 3
#define ROOT_DEL 4
#define ROOT_CHA 5
#define ROOT_GAI 6
#define ROOT_HIS 7
#define PUT_CHA 8
#define PUT_GAI 9
time_t currentTime;
int newfd;
sqlite3* db=NULL;
sqlite3_stmt *stmt;
int root_regin(int newfd);
int putong_regin(int newfd);
int new_root_init(int newfd);
int DO_DEL(int newfd);
int DO_ROOT_CHA(int newfd);
int DO_ROOT_GAI(int newfd);
int DO_HIS(int newfd);
int PU_CHA(int newfd);
int PU_GAI(int newfd);
int deal_cli_msg(int newfd, struct sockaddr_in cin);
//回收僵尸进程
void handler(int sig)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}
struct msg{
int usertype; //用户类型
int cmdtype; //通信指令类型
char buf[512]; //通信的消息
struct info{
char name[20]; //名字
char bumen[20]; //部门
int age; //年龄
char phonenumber[50]; //手机号
char sex[10]; //性别
char pd[20]; //密码
char quanxian[20]; //权限
int qian;
} st; //员工信息
}msg_t;//通信结构体
struct sockaddr_in cin; //存储连接成功的客户端地址信息
int main(int argc,const char * argv[])
{
__sighandler_t s = signal(17, handler);
if(SIG_ERR == s)
{
ERR_MSG("signal");
return -1;
}
//如果存在数据库则直接打开
if(sqlite3_open("./bg.db",&db) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_open:%s errcode:%d\n",sqlite3_errmsg(db),sqlite3_errcode(db));
return -1;
}
//创建表格
char sql[128]="create table if not exists name (name char,password char,quanxian char);";
char* errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
char sql_his[128]="create table if not exists his (name char,sj char,time char);";
errmsg=NULL;
if(sqlite3_exec(db,sql_his,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
//创建流式套接字
int sfd = socket(AF_INET,SOCK_STREAM,0);
if(sfd < 0 )
{
ERR_MSG("socket");
return -1;
}
int reuse = 1;
if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
//填充服务器的地址信息结构体
struct sockaddr_in sin;
sin.sin_family =AF_INET;
sin.sin_port =htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
//绑定套接字
if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) <0)
{
ERR_MSG("bind");
return -1;
}
printf("绑定完成\n");
//将套接字设置为被动监听状态
if(listen(sfd,128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("监听设置完成\n");
socklen_t addrlen = sizeof(cin);
int newfd = -1;
pid_t cpid = 0;
while(1)
{
//父进程只负责连接
//阻塞函数,从已完成连接的队列头中获取一个客户端信息,生成一个新的文件描述符,
//该文件描述符才是与客户端通信的文件描述符!!!
newfd = accept(sfd, (struct sockaddr*)&cin, &addrlen);
if(newfd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s:%d] newfd=%d 连接成功__%d__\n", \
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd, __LINE__);
//能运行到当前啊位置,则代表有客户端连接成功,则需要创建一个子进程用于与客户端交互
cpid = fork();
if(cpid > 0)
{
close(newfd);
}
else if(0 == cpid)
{
close(sfd);
deal_cli_msg(newfd, cin);
exit(0); //退出子进程,子进程只负责交互,对应客户端退出后,结束子进程
}
else
{
perror("fork");
return -1;
}
}
close(sfd);
return 0;
}
int deal_cli_msg(int newfd, struct sockaddr_in cin)
{
ssize_t res=0;
while(1)
{
//接受
res= recv(newfd,&msg_t,sizeof(msg_t),0);
printf("%d\n",msg_t.cmdtype);
if(res<0)
{
ERR_MSG("接收");
return -1;
}
else if(res==0)
{
printf("%d下线\n",newfd);
break;
}
//判断root用户登录
if(msg_t.cmdtype==ROOT_REGIN_GO)
{
printf("root用户开始登录\n");
root_regin(newfd);
}
//判断普通用户登录
if(msg_t.cmdtype==PUT_REGIN_GO)
{
printf("普通用户开始登录\n");
putong_regin(newfd);
}
//新成员添加
if(msg_t.cmdtype == NEW_ROOT_INIT)
{
printf("ROOT用户添加新用户\n");
new_root_init(newfd);
}
//删除成员
if(msg_t.cmdtype == ROOT_DEL)
{
printf("开始删除用户\n");
DO_DEL(newfd);
}
//查找信息
if(msg_t.cmdtype == ROOT_CHA)
{
printf("ROOT用户查询信息\n");
DO_ROOT_CHA(newfd);
}
//修改信息
if(msg_t.cmdtype == ROOT_GAI)
{
printf("ROOT用户修改信息\n");
DO_ROOT_GAI(newfd);
}
//查询历史记录
if(msg_t.cmdtype == ROOT_HIS)
{
printf("查询记录\n");
DO_HIS(newfd);
}
//普通用户查询自己信息
if(msg_t.cmdtype == PUT_CHA )
{
printf("普通用户查询自己信息\n");
recv(newfd,&msg_t,sizeof(msg_t),0);
PU_CHA(newfd);
}
//普通用户修改自己信息
if(msg_t.cmdtype == PUT_GAI)
{
printf("普通用户修改自己信息\n");
recv(newfd,&msg_t,sizeof(msg_t),0);
PU_GAI(newfd);
}
bzero(msg_t.buf,sizeof(msg_t.buf));
}
close(newfd);
return 0;
}
int PU_CHA(int newfd)
{
printf("开始\n");
bzero(msg_t.buf,sizeof(msg_t.buf));
char sql[512] = "";
sprintf(sql,"select * from msg where name=\"%s\";",msg_t.st.name);
char* errmsg = NULL;
char** results = NULL;
int rows, columns;
if(sqlite3_get_table(db, sql, &results, &rows, &columns, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "line:%d\n", __LINE__);
return -1;
}
int i;
// 将查询结果拼接到msg_t.buf字符串中
for (int i = 0; i < rows + 1; ++i)
{
for (int j = 0; j < columns; ++j)
{
strcat(msg_t.buf, results[i * columns + j]);
strcat(msg_t.buf, " ");
}
strcat(msg_t.buf, "\n");
}
send(newfd,&msg_t,sizeof(msg_t),0);
// 释放结果集内存
sqlite3_free_table(results);
printf("查询发送\n");
time(¤tTime);
struct tm* timeinfo=localtime(¤tTime);
char sql_his[512]="";
sprintf(sql_his,"insert into his values(\"%s\",\"%s\",\"%s\");",msg_t.st.name,__func__,asctime(timeinfo));
errmsg=NULL;
if(sqlite3_exec(db,sql_his,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
return 0;
}
int PU_GAI(int newfd)
{
printf("%d\n",__LINE__);
printf("%d\n",__LINE__);
printf("%s\n",msg_t.st.name);
char sql[1024]="";
sprintf(sql,"update msg set name=\"%s\",age=%d,phonenum=\"%s\",password=\"%s\" where name=\"%s\";",\
msg_t.st.name,msg_t.st.age,msg_t.st.phonenumber,\
msg_t.st.pd,msg_t.st.name);
printf("%s\n",sql);
char* errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr, "SQL error: %s\n", errmsg);
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
char sql_name[1024]="";
sprintf(sql_name,"update name set name=\"%s\",password=\"%s\" where name=\"%s\";",\
msg_t.st.name,msg_t.st.pd,msg_t.st.name);
errmsg=NULL;
if(sqlite3_exec(db,sql_name,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr, "SQL error: %s\n", errmsg);
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
time(¤tTime);
struct tm* timeinfo=localtime(¤tTime);
char sql_his[512]="";
sprintf(sql_his,"insert into his values(\"%s\",\"%s\",\"%s\");",msg_t.st.name,__func__,asctime(timeinfo));
errmsg=NULL;
if(sqlite3_exec(db,sql_his,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
send(newfd,&msg_t,sizeof(msg_t),0);
return 0;
}
int root_regin(int newfd)
{
while(1)
{
bzero(msg_t.buf,sizeof(msg_t.buf));
// 打开数据库连接
int rc = sqlite3_open("./bg.db", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "无法打开数据库: %s\n", sqlite3_errmsg(db));
return rc;
}
char name[50]="";
char password[50]="";
int res=recv(newfd,&msg_t,sizeof(msg_t),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
strcpy(name, msg_t.st.name);
strcpy(password, msg_t.st.pd);
// 查询账号和权限
char sql[128];
sprintf(sql, "SELECT quanxian FROM name WHERE name='%s' AND password='%s'", name, password);
// 准备查询语句
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "无法准备查询语句: %s\n", sqlite3_errmsg(db));
return rc;
}
// 执行查询
rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW) {
// 提取查询结果
const unsigned char *quanxian = sqlite3_column_text(stmt, 0);
if (strcmp(quanxian, "root") == 0) {
// 执行root权限下的操作...
printf("root用户上线\n");
msg_t.usertype=1;
send(newfd,&msg_t,sizeof(msg_t),0);
break;
} else {
// 执行非root权限下的操作...
printf("不是root用户\n");
send(newfd,&msg_t,sizeof(msg_t),0);
}
} else {
printf("账号或密码错误,登录失败\n");
send(newfd,&msg_t,sizeof(msg_t),0);
}
}
time(¤tTime);
struct tm* timeinfo=localtime(¤tTime);
char sql_his[512]="";
sprintf(sql_his,"insert into his values(\"%s\",\"%s\",\"%s\");",msg_t.st.name,__func__,asctime(timeinfo));
char* errmsg=NULL;
if(sqlite3_exec(db,sql_his,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
send(newfd,&msg_t,sizeof(msg_t),0);
return 0;
}
int putong_regin(int newfd)
{
while(1)
{
bzero(msg_t.buf,sizeof(msg_t.buf));
// 打开数据库连接
int rc = sqlite3_open("./bg.db", &db);
if (rc != SQLITE_OK) {
fprintf(stderr, "无法打开数据库: %s\n", sqlite3_errmsg(db));
return rc;
}
char name[50]="";
char password[50]="";
int res=recv(newfd,&msg_t,sizeof(msg_t),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
strcpy(name, msg_t.st.name);
strcpy(password, msg_t.st.pd);
// 查询账号和权限
char sql[128];
sprintf(sql, "SELECT quanxian FROM name WHERE name='%s' AND password='%s'", name, password);
// 准备查询语句
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "无法准备查询语句: %s\n", sqlite3_errmsg(db));
return rc;
}
// 执行查询
rc = sqlite3_step(stmt);
if (rc == SQLITE_ROW) {
// 提取查询结果
const unsigned char *quanxian = sqlite3_column_text(stmt, 0);
if (strcmp(quanxian, "root") == 0) {
// 执行root权限下的操作...
printf("登录错误\n");
send(newfd,&msg_t,sizeof(msg_t),0);
} else {
// 执行非root权限下的操作...
printf("普通用户上线\n");
msg_t.usertype=0;
send(newfd,&msg_t,sizeof(msg_t),0);
break;
}
} else {
printf("账号或密码错误,登录失败\n");
send(newfd,&msg_t,sizeof(msg_t),0);
}
}
time(¤tTime);
struct tm* timeinfo=localtime(¤tTime);
char sql_his[512]="";
sprintf(sql_his,"insert into his values(\"%s\",\"%s\",\"%s\");",msg_t.st.name,__func__,asctime(timeinfo));
char* errmsg=NULL;
if(sqlite3_exec(db,sql_his,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
send(newfd,&msg_t,sizeof(msg_t),0);
return 0;
}
int new_root_init(int newfd)
{
recv(newfd,&msg_t,sizeof(msg_t),0);
printf("获取新用户信息成功\n");
//创建信息表
char sql[128]="create table if not exists msg (name char,password char,quanxian char,bumen char,age int,phonenum char,sex char);";
char* errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
//插入信息表
char insert_msg_sql[512]="";
sprintf(insert_msg_sql,"insert into msg values(\"%s\",\"%s\",\"%s\",\"%s\",%d,\"%s\",\"%s\");",\
msg_t.st.name,msg_t.st.pd,msg_t.st.quanxian,\
msg_t.st.bumen,msg_t.st.age,msg_t.st.phonenumber,msg_t.st.sex);
if(sqlite3_exec(db,insert_msg_sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
//插入账号表
char insert_name_sql[512]="";
sprintf(insert_name_sql,"insert into name values(\"%s\",\"%s\",\"%s\");",\
msg_t.st.name,msg_t.st.pd,msg_t.st.quanxian);
if(sqlite3_exec(db,insert_name_sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
printf("添加新账号%s成功\n",msg_t.st.name);
time(¤tTime);
struct tm* timeinfo=localtime(¤tTime);
char sql_his[512]="";
sprintf(sql_his,"insert into his values(\"%s\",\"%s\",\"%s\");",msg_t.st.name,__func__,asctime(timeinfo));
errmsg=NULL;
if(sqlite3_exec(db,sql_his,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
send(newfd,&msg_t,sizeof(msg_t),0);
return 0;
}
int DO_DEL(int newfd)
{
recv(newfd,&msg_t,sizeof(msg_t),0);
printf("将要删除的员工姓名为:%s\n",msg_t.st.name);
//删除对应的员工
char sql_del_to_name[128]="";
sprintf(sql_del_to_name,"delete from name where name=\"%s\";",msg_t.st.name);
char* errmsg=NULL;
if(sqlite3_exec(db,sql_del_to_name,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
char sql_del_to_msg[128]="";
sprintf(sql_del_to_msg,"delete from msg where name=\"%s\";",msg_t.st.name);
if(sqlite3_exec(db,sql_del_to_msg,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
printf("删除员工%s完成\n",msg_t.st.name);
time(¤tTime);
struct tm* timeinfo=localtime(¤tTime);
char sql_his[512]="";
sprintf(sql_his,"insert into his values(\"%s\",\"%s\",\"%s\");",msg_t.st.name,__func__,asctime(timeinfo));
errmsg=NULL;
if(sqlite3_exec(db,sql_his,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
send(newfd,&msg_t,sizeof(msg_t),0);
return 0;
}
int DO_ROOT_CHA(int newfd)
{
recv(newfd,&msg_t,sizeof(msg_t),0);
printf("进入\n");
//判断是查询全部还是特定
if(strcmp(msg_t.buf, "全部信息") == 0)
{
bzero(msg_t.buf,sizeof(msg_t.buf));
char sql[512] = "select * from msg;";
char* errmsg = NULL;
char** results = NULL;
int rows, columns;
if(sqlite3_get_table(db, sql, &results, &rows, &columns, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "line:%d\n", __LINE__);
return -1;
}
int i;
// 将查询结果拼接到msg_t.buf字符串中
for (int i = 1; i < rows + 1; ++i)
{
for (int j = 0; j < columns; ++j)
{
strcat(msg_t.buf, results[i * columns + j]);
strcat(msg_t.buf, " ");
}
strcat(msg_t.buf, "\n");
}
send(newfd,&msg_t,sizeof(msg_t),0);
// 释放结果集内存
sqlite3_free_table(results);
}
else if(strcmp(msg_t.buf, "特定员工信息") == 0)
{
bzero(msg_t.buf,sizeof(msg_t.buf));
char sql[512] = "";
sprintf(sql,"select * from msg where name=\"%s\";",msg_t.st.name);
char* errmsg = NULL;
char** results = NULL;
int rows, columns;
if(sqlite3_get_table(db, sql, &results, &rows, &columns, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "line:%d\n", __LINE__);
return -1;
}
int i;
// 将查询结果拼接到msg_t.buf字符串中
for (int i = 0; i < rows + 1; ++i)
{
for (int j = 0; j < columns; ++j)
{
strcat(msg_t.buf, results[i * columns + j]);
strcat(msg_t.buf, " ");
}
strcat(msg_t.buf, "\n");
}
send(newfd,&msg_t,sizeof(msg_t),0);
// 释放结果集内存
sqlite3_free_table(results);
}
time(¤tTime);
struct tm* timeinfo=localtime(¤tTime);
char sql_his[512]="";
sprintf(sql_his,"insert into his values(\"%s\",\"%s\",\"%s\");",msg_t.st.name,__func__,asctime(timeinfo));
char* errmsg=NULL;
if(sqlite3_exec(db,sql_his,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
return 0;
}
int DO_ROOT_GAI(int newfd)
{
recv(newfd,&msg_t,sizeof(msg_t),0);
printf("%s信息要被修改\n",msg_t.buf);
char sql[1024]="";
sprintf(sql,"update msg set name=\"%s\",bumen=\"%s\",age=%d,phonenum=\"%s\",sex=\"%s\",password=\"%s\" where name=\"%s\";",\
msg_t.st.name,msg_t.st.bumen,msg_t.st.age,msg_t.st.phonenumber,\
msg_t.st.sex,msg_t.st.pd,msg_t.buf);
char* errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr, "SQL error: %s\n", errmsg);
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
char sql_name[1024]="";
sprintf(sql_name,"update name set name=\"%s\",password=\"%s\",quanxian=\"%s\" where name=\"%s\";",\
msg_t.st.name,msg_t.st.pd,msg_t.st.quanxian,msg_t.buf);
errmsg=NULL;
if(sqlite3_exec(db,sql_name,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr, "SQL error: %s\n", errmsg);
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
time(¤tTime);
struct tm* timeinfo=localtime(¤tTime);
char sql_his[512]="";
sprintf(sql_his,"insert into his values(\"%s\",\"%s\",\"%s\");",msg_t.st.name,__func__,asctime(timeinfo));
errmsg=NULL;
if(sqlite3_exec(db,sql_his,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
send(newfd,&msg_t,sizeof(msg_t),0);
return 0;
}
int DO_HIS(int newfd)
{
char sql[512] = "select * from his;";
char* errmsg = NULL;
char** results = NULL;
int rows, columns;
if(sqlite3_get_table(db, sql, &results, &rows, &columns, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "line:%d\n", __LINE__);
return -1;
}
int i;
// 将查询结果拼接到msg_t.buf字符串中
for (int i = 0; i < rows + 1; ++i)
{
for (int j = 0; j < columns; ++j)
{
strcat(msg_t.buf, results[i * columns + j]);
strcat(msg_t.buf, " ");
}
strcat(msg_t.buf, "\n");
}
send(newfd,&msg_t,sizeof(msg_t),0);
// 释放结果集内存
sqlite3_free_table(results);
time(¤tTime);
struct tm* timeinfo=localtime(¤tTime);
char sql_his[512]="";
sprintf(sql_his,"insert into his values(\"%s\",\"%s\",\"%s\");",msg_t.st.name,__func__,asctime(timeinfo));
errmsg=NULL;
if(sqlite3_exec(db,sql_his,NULL,NULL,&errmsg) != SQLITE_OK)
{
fprintf(stderr,"line:%d\n",__LINE__);
return -1;
}
return 0;
}
客户端
#include <stdio.h>
#include <sqlite3.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d\n",__LINE__);\
perror(msg);\
}while(0);
struct msg{
int usertype; //用户类型
int cmdtype; //通信指令类型
char buf[512]; //通信的消息
struct info{
char name[20]; //名字
char bumen[20]; //部门
int age; //年龄
char phonenumber[50]; //手机号
char sex[10]; //性别
char pd[20]; //密码
char quanxian[20]; //权限
int qian;
} st; //员工信息
}msg_t;//通信结构体
#define IP "192.168.250.100"
#define PORT 6666
char buf[128]="";
sqlite3* db=NULL;
int cfd;
int LOGIN_GO();
int GO_ROOT_SET();
int NEW_ROOT_INIT();
int ROOT_DEL();
int DO_ROOT_CHA();
int ROOT_GAI();
int DO_HIS();
int GO_PUTONG_SET();
int PUTONG_LOGIN_GO();
int PUT_CHA();
int PUT_GAI();
int main(int argc, char const *argv[])
{
cfd = socket(AF_INET,SOCK_STREAM,0);
if(cfd<0)
{
ERR_MSG("socket");
return -1;
}
printf("cfd=%d\n",cfd);
int reuse = 1;
if(setsockopt(cfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
struct sockaddr_in sin;
sin.sin_family =AF_INET;
sin.sin_port =htons(PORT);
sin.sin_addr.s_addr=inet_addr(IP);
//连接服务器
if(connect(cfd,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("connect");
return -1;
}
printf("连接成功\n");
int a;
while(1)
{
printf("---------------------\n");
printf("----1.root用户登录----\n");
printf("----2.普通用户登录----\n");
printf("----3.退出-----------\n");
printf("---------------------\n");
printf("请输入:");
scanf("%d",&a);
switch(a)
{
case 1:
msg_t.cmdtype=1;
send(cfd,&msg_t,sizeof(msg_t),0);
if(!LOGIN_GO())
{
GO_ROOT_SET();
}
break;
case 2:
msg_t.cmdtype=0;
send(cfd,&msg_t,sizeof(msg_t),0);
if(!PUTONG_LOGIN_GO())
{
GO_PUTONG_SET();
}
break;
case 3:
goto END1;
break;
default:
break;
}
}
END1:
close(cfd);
return 0;
}
int LOGIN_GO()
{
while(1)
{
// 输入账号和密码
char name[50] = "";
char password[50] = "";
printf("输入账号:");
scanf("%s", name);
printf("输入密码:");
scanf("%s", password);
strcpy(msg_t.st.name, name);
strcpy(msg_t.st.pd, password);
send(cfd,&msg_t,sizeof(msg_t),0);
recv(cfd,&msg_t,sizeof(msg_t),0);
if(msg_t.usertype==1)
{
break;
}
else
printf("重新输入\n");
}
return 0;
}
int PUTONG_LOGIN_GO()
{
while(1)
{
printf("登录界面\n");
// 输入账号和密码
char name[50] = "";
char password[50] = "";
printf("输入账号:");
scanf("%s", name);
printf("输入密码:");
scanf("%s", password);
strcpy(msg_t.st.name, name);
strcpy(msg_t.st.pd, password);
send(cfd,&msg_t,sizeof(msg_t),0);
recv(cfd,&msg_t,sizeof(msg_t),0);
if(msg_t.usertype==0)
{
printf("登录成功\n");
break;
}
else
printf("重新输入\n");
}
return 0;
}
int GO_PUTONG_SET()
{
int a;
while(1)
{
printf("---------------------------\n");
printf("---------1.查询信息---------\n");
printf("---------2.修改信息---------\n");
printf("---------3.返回-------------\n");
printf("---------------------------\n");
printf("输入:");
scanf("%d",&a);
switch(a)
{
case 1:
msg_t.cmdtype=8;
send(cfd,&msg_t,sizeof(msg_t),0);
PUT_CHA();
break;
case 2:
msg_t.cmdtype=9;
send(cfd,&msg_t,sizeof(msg_t),0);
PUT_GAI();
break;
case 3:
goto ENDP;
break;
}
}
ENDP:
return 0;
}
int PUT_CHA()
{
send(cfd,&msg_t,sizeof(msg_t),0);
recv(cfd,&msg_t,sizeof(msg_t),0);
recv(cfd,&msg_t,sizeof(msg_t),0);
printf("%s\n",msg_t.buf);
return 0;
}
int PUT_GAI()
{
char name[20]="";
printf("员工姓名修改为:");
scanf("%s",name);
strcpy(msg_t.st.name,name);
char pwd[20]="";
printf("员工密码修改为:");
scanf("%s",pwd);
strcpy(msg_t.st.pd,pwd);
int age;
printf("员工年龄修改为:");
scanf("%d",&age);
msg_t.st.age=age;
char phn[20]="";
printf("员工手机号修改为:");
scanf("%s",phn);
strcpy(msg_t.st.phonenumber,phn);
send(cfd,&msg_t,sizeof(msg_t),0);
recv(cfd,&msg_t,sizeof(msg_t),0);
return 0;
}
int GO_ROOT_SET()
{
int a;
while(1)
{
printf("---------------------------\n");
printf("---------1.查询信息---------\n");
printf("---------2.查询历史---------\n");
printf("---------3.添加成员---------\n");
printf("---------4.删除成员---------\n");
printf("---------5.修改成员---------\n");
printf("---------6.返回-------------\n");
printf("---------------------------\n");
printf("输入:");
scanf("%d",&a);
switch(a)
{
case 1:
msg_t.cmdtype=5;
send(cfd,&msg_t,sizeof(msg_t),0);
DO_ROOT_CHA();
break;
case 2:
msg_t.cmdtype=7;
send(cfd,&msg_t,sizeof(msg_t),0);
DO_HIS();
break;
case 3:
msg_t.cmdtype=3;
send(cfd,&msg_t,sizeof(msg_t),0);
NEW_ROOT_INIT();
break;
case 4:
msg_t.cmdtype=4;
send(cfd,&msg_t,sizeof(msg_t),0);
ROOT_DEL();
break;
case 5:
msg_t.cmdtype=6;
send(cfd,&msg_t,sizeof(msg_t),0);
ROOT_GAI();
break;
case 6:
goto END;
break;
}
}
END:
return 0;
}
int NEW_ROOT_INIT()
{
printf("输入新的员工姓名:");
char name[20]="";
scanf("%s",name);
strcpy(msg_t.st.name,name);
printf("输入新的员工密码:");
char pd[20]="";
scanf("%s",pd);
strcpy(msg_t.st.pd,pd);
printf("输入新的员工权限:");
char qx[20]="";
scanf("%s",qx);
strcpy(msg_t.st.quanxian,qx);
printf("输入新的员工年龄:");
int age;
scanf("%d",&age);
msg_t.st.age=age;
printf("输入新的员工部门:");
char bumen[20]="";
scanf("%s",bumen);
strcpy(msg_t.st.bumen,bumen);
printf("输入新的员工手机号:");
char pnum[20]="";
scanf("%s",pnum);
strcpy(msg_t.st.phonenumber,pnum);
printf("输入新的员工性别:");
char sex[10];
scanf("%s",sex);
strcpy(msg_t.st.sex,sex);
send(cfd,&msg_t,sizeof(msg_t),0);
return 0;
}
int ROOT_DEL()
{
char name[20]="";
printf("请输入删除员工的姓名:");
scanf("%s",name);
strcpy(msg_t.st.name,name);
send(cfd,&msg_t,sizeof(msg_t),0);
return 0;
}
int DO_ROOT_CHA()
{
bzero(msg_t.buf,sizeof(msg_t.buf));
int a;
printf("如果查询全部员工信息为1,特定员工为2\n");
printf("请输入:");
scanf("%d",&a);
//判断
if(1 == a)
{
strcpy(msg_t.buf,"全部信息");
printf("%s\n",msg_t.buf);
send(cfd,&msg_t,sizeof(msg_t),0);
}
else if(2 == a)
{
strcpy(msg_t.buf,"特定员工信息");
printf("输入员工的姓名:");
char name[20]="";
scanf("%s",name);
strcpy(msg_t.st.name,name);
send(cfd,&msg_t,sizeof(msg_t),0);
}
recv(cfd,&msg_t,sizeof(msg_t),0);
recv(cfd,&msg_t,sizeof(msg_t),0);
printf("%s\n",msg_t.buf);
return 0;
}
int ROOT_GAI()
{
bzero(msg_t.buf,sizeof(msg_t.buf));
char name[20]="";
printf("输入要修改的成员:");
scanf("%s",name);
strcpy(msg_t.buf,name);
printf("员工姓名修改为:");
scanf("%s",name);
strcpy(msg_t.st.name,name);
char pwd[20]="";
printf("员工密码修改为:");
scanf("%s",pwd);
strcpy(msg_t.st.pd,pwd);
char bumen[20]="";
printf("员工部门修改为:");
scanf("%s",bumen);
strcpy(msg_t.st.bumen,bumen);
int age;
printf("员工年龄修改为:");
scanf("%d",&age);
msg_t.st.age=age;
char phn[20]="";
printf("员工手机号修改为:");
scanf("%s",phn);
strcpy(msg_t.st.phonenumber,phn);
char sex[10]="";
printf("员工性别修改为:");
scanf("%s",sex);
strcpy(msg_t.st.sex,sex);
char quanxian[20]="";
printf("员工权限修改为:(1为普通用户,root为root用户)");
scanf("%s",quanxian);
strcpy(msg_t.st.quanxian,quanxian);
send(cfd,&msg_t,sizeof(msg_t),0);
return 0;
}
int DO_HIS()
{
recv(cfd,&msg_t,sizeof(msg_t),0);
recv(cfd,&msg_t,sizeof(msg_t),0);
printf("%s\n",msg_t.buf);
printf("%d\n",__LINE__);
return 0;
}