服务器
#include<stdio.h>
#include<sqlite3.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <sys/select.h>
#include <time.h>
//打印错误新的宏函数
#define ERR_MSG(msg) do{\
fprintf(stderr, " __%d__ ", __LINE__);\
perror(msg);\
}while(0)
#define PORT 8888 //1024~49151
#define IP "192.168.250.100" //本机IP,用ifconfig查看
//打开数据库
sqlite3 *open_staff(sqlite3 *db);
//管理员的用户名操作
int root_in_id(sqlite3 *db,char *buf,int i);
//管理员的密码操作
int root_in_password(sqlite3 *db,char *buf,int i,char *root_name);
//全部查询操作
int root_search_all(sqlite3 *bd,int i,char *root_name);
//按名字查询
int root_search_name(sqlite3 *bd,char *buf,int i,char * root_name);
//添加成员
int root_add(sqlite3 *bd,char *buf,int i,char *root_name);
//删除成员
int root_del(sqlite3 *bd,char *buf,int i,char *root_name);
//修改成员
int root_change(sqlite3 *bd,char *buf,int i,char *root_name);
//历史操作记录
int root_his(sqlite3 *bd,int i,char *root_name);
//员工的用户名操作
int staff_in_id(sqlite3 *db,char *buf,int i);
//员工的密码操作
int staff_in_password(sqlite3 *db,char *buf,int i);
//员工自我信息
int staff_msg(sqlite3 *db,char *buf,int i);
//员工修改自身信息
int staff_change(sqlite3 *db,char *buf,int i);
//创建staff这个表格
int create_staff(sqlite3 *db);
//创建root_history这个表格
int create_root_history(sqlite3 *db,int newfd);
//删除rppt_history表格
int delete_root_history(sqlite3 *db,int newfd);
//关闭数据库
int close_staff(sqlite3 *db);
int main(int argc, const char *argv[])
{
//创建流式套接字
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;
}
//将套接字设置为被动监听状态,让内核去监听是否有客户端连接;
if(listen(sfd, 10) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success\n");
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
struct sockaddr_in cin_arr[1020];
//创建一个读集合
fd_set readfds,tempfds;
//清空集合
FD_ZERO(&readfds);
FD_ZERO(&tempfds);
//将0号文件描述符添加到集合
FD_SET(0,&readfds);
//将sfd文件描述符添加到集合
FD_SET(sfd,&readfds);
//最大文件描述符
int maxfd=sfd;
int select_res=0;
char buf[128] = "";
ssize_t res = 0;
int newfd=-1;
int i=0,j=0;
//打开数据库
sqlite3*db=open_staff(db);
//创建staff这个表格
create_staff(db);
while(1)
{
tempfds=readfds;
select_res=select(maxfd+1,&tempfds,NULL,NULL,NULL);
if(select_res<0)
{
ERR_MSG("select");
return -1;
}
else if(0==select_res)
{
fprintf(stderr,"select time out\n");
break;
}
for(i=0;i<=maxfd;i++)
{
//判断0~maxfd这些文件描述符在不在集合中
if(FD_ISSET(i,&tempfds)==0)
{
continue;
}
if(0==i)
{
}
//有客户端接入
else if(sfd==i)
{
newfd=accept(sfd,(struct sockaddr*)&cin,&addrlen);
if(newfd<0)
{
ERR_MSG("accept");
return -1;
}
printf("%d : %d\n",ntohs(cin.sin_port),newfd);
cin_arr[newfd-4]=cin;
FD_SET(newfd,&readfds);
maxfd=maxfd>newfd?maxfd:newfd;
}
else
{
bzero(buf,sizeof(buf));
res=recv(i,buf,sizeof(buf),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(0==res)
{
printf("%d :%d 退出\n",ntohs((cin_arr[i-4].sin_port)),i);
//每退出一个管理员,把他的history表格给关了
delete_root_history(db,i);
printf("%d的管理员表格删除了\n",i);
close(i);
FD_CLR(i,&readfds);
for(j=maxfd;j>0;j--)
{
if(FD_ISSET(j,&readfds)==1)
{
maxfd=j;
break;
}
}
}
else
{
static char root_name[20]="";
//管理员登录的工号操作
if(buf[0]=='1')
{
strcpy(root_name,buf+1);
root_in_id(db,buf,i);
printf("%s\n",root_name);
}
//管理员登陆的密码操作
else if(buf[0]=='2')
{
printf("%s\n",root_name);
if(root_in_password(db,buf,i,root_name)==0)
//每一个管理员登录,给他创建一个history表格
create_root_history(db,i);
}
//员工登录的工号操作
else if(buf[0]=='3')
{
staff_in_id(db,buf,i);
}
//员工登录密码操作
else if(buf[0]=='4')
{
staff_in_password(db,buf,i);
}
//查询所有
else if(buf[0]=='5')
{
root_search_all(db,i,root_name);
}
//按名字查询
else if(buf[0]=='6')
{
root_search_name(db,buf,i,root_name);
}
//管理员添加新员工
else if(buf[0]=='7'||buf[0]=='8')
{
root_add(db,buf,i,root_name);
}
//管理员删除成员
else if(buf[0]=='9')
{
root_del(db,buf,i,root_name);
}
//管理员修改成员
else if(buf[0]=='a')
{
root_change(db,buf,i,root_name);
}
//管理员历史记录
else if(buf[0]=='b')
{
root_his(db,i,root_name);
}
//成员自我信息
else if(buf[0]=='c')
{
staff_msg(db,buf,i);
}
//成员修改信息
else if(buf[0]=='d')
{
staff_change(db,buf,i);
}
}
}
}
}
close_staff(db);
return 0;
}
//打开数据库
sqlite3* open_staff(sqlite3 *db)
{
if(sqlite3_open("./staff.db",&db)!=SQLITE_OK)
{
printf("err_code:%d\n",sqlite3_errcode(db));
printf("errmsg:%s\n",sqlite3_errmsg(db));
fprintf(stderr,"sqlite3_open failed\n");
return NULL;
}
printf("数据库打开成功\n");
return db;
}
//管理员登录的工号操作
int root_in_id(sqlite3 *db,char *buf,int i)
{
char sql[128]="select staff_id,staff_root from staff";
char **pres=NULL;
int row,column;
char *errmsg=NULL;
ssize_t res=0;
char rcv[128]="";
if(sqlite3_get_table(db,sql,&pres,&row,&column,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"__%d__ sqlite3_get_table:%s\n",__LINE__,errmsg);
return -1;
}
int j=0;
int flag=0;
for(j=0;j<(row+1)*column;j++)
{
if(j%2==0)
{
if(strcmp(pres[j],buf+1)==0)
{
if(strcmp(pres[j+1],"N")==0)
{
bzero(sql,sizeof(sql));
strcpy(sql,"非管理员");
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
return -1;
}
else
{
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
flag=1;
break;
}
}
}
}
if(flag==0)
{
bzero(sql,sizeof(sql));
strcpy(sql,"非管理员");
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
}
}
//管理员登陆的密码操作
int root_in_password(sqlite3 *db,char *buf,int i,char *root_name)
{
char sql[128]="select staff_id,staff_pwd from staff";
char **pres=NULL;
int row,column;
char *errmsg=NULL;
ssize_t res=0;
char rcv[128]="";
char do_wh_at[50]="";
char rili[128]="";
if(sqlite3_get_table(db,sql,&pres,&row,&column,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"__%d__ sqlite3_get_table:%s\n",__LINE__,errmsg);
return -1;
}
int j=0;
int len=strlen(buf+1);
for(j=0;j<(row+1)*column;j++)
{
if(j%2==0)
{
if(strcmp(pres[j],buf+1)==0)
{
if(strcmp(buf+2+len,pres[j+1])==0)
{
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("%s登录成功\n",root_name);
return 0;
}
else
{
bzero(sql,sizeof(sql));
strcpy(sql,"密码不正确");
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
return -1;
}
}
}
}
}
//员工登录的工号操作
int staff_in_id(sqlite3 *db,char *buf,int i)
{
char sql[128]="select staff_id,staff_root from staff";
char **pres=NULL;
int row,column;
char *errmsg=NULL;
ssize_t res=0;
char rcv[128]="";
if(sqlite3_get_table(db,sql,&pres,&row,&column,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"__%d__ sqlite3_get_table:%s\n",__LINE__,errmsg);
return -1;
}
int j=0;
int flag=0;
for(j=0;j<(row+1)*column;j++)
{
if(j%2==0)
{
if(strcmp(pres[j],buf+1)==0)
{
if(strcmp(pres[j+1],"Y")==0)
{
bzero(sql,sizeof(sql));
strcpy(sql,"是管理员");
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
return -1;
}
else
{
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
flag=1;
break;
}
}
}
}
if(flag==0)
{
bzero(sql,sizeof(sql));
strcpy(sql,"工号不存在");
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
}
}
//员工登陆的密码操作
int staff_in_password(sqlite3 *db,char *buf,int i)
{
char sql[128]="select staff_id,staff_pwd from staff";
char **pres=NULL;
int row,column;
char *errmsg=NULL;
ssize_t res=0;
char rcv[128]="";
if(sqlite3_get_table(db,sql,&pres,&row,&column,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"__%d__ sqlite3_get_table:%s\n",__LINE__,errmsg);
return -1;
}
int j=0;
int len=strlen(buf+1);
for(j=0;j<(row+1)*column;j++)
{
if(j%2==0)
{
if(strcmp(pres[j],buf+1)==0)
{
if(strcmp(buf+2+len,pres[j+1])==0)
{
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("%s登录成功\n",pres[j]);
return 0;
}
else
{
bzero(sql,sizeof(sql));
strcpy(sql,"密码不正确");
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
return -1;
}
}
}
}
}
//管理员全部查询
int root_search_all(sqlite3 *db,int i,char *root_name)
{
char sql[128] = "";
sprintf(sql,"select * from staff");
char ** pres = NULL; //存储查询结果的首地址
int row, column; //查询结果的行列数
char* errmsg = NULL;
char do_wh_at[50]="";
char rili[128]="";
if(sqlite3_get_table(db, sql, &pres, &row, &column, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_get_table:%s\n", __LINE__, errmsg);
return -1;
}
//能运行到当前位置则说明,查询函数运行成功
int j = 0;
char snd[128]="";
for(j=0; j<(row+1)*column; j++)
{
bzero(snd,sizeof(snd));
strcpy(snd,pres[j]);
if(send(i,snd,sizeof(snd),0)<0)
{
ERR_MSG("send");
return -1;
}
}
bzero(snd,sizeof(snd));
strcpy(snd,"finish out");
if(send(i,snd,sizeof(snd),0)<0)
{
ERR_MSG("send");
return -1;
}
sprintf(do_wh_at,"全部查询");
time_t t;
struct tm*info=NULL;
t=time(NULL);
info=localtime(&t);
sprintf(rili,"%d-%02d-%02d:%02d:%02d:%02d",info->tm_year+1900,info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min,info->tm_sec);
sprintf(sql,"INSERT INTO root_history%d (root_id,do_what,time) values (\"%s\",\"%s\",'%s');",i,root_name,do_wh_at,rili);
printf("%s\n",root_name);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
printf("全部查询成功\n");
//释放获取到的空间
//sqlite3_free_table(pres);
//pres = NULL;
return 0;
}
//管理员按名字查询
int root_search_name(sqlite3 *db,char *buf,int i,char *root_name)
{
char sql[128]="select * from staff";
char **pres=NULL;
int row,column;
char *errmsg=NULL;
ssize_t res=0;
char snd[128]="";
char rili[128]="";
char do_wh_at[50]="";
int flag=0;
if(sqlite3_get_table(db,sql,&pres,&row,&column,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"__%d__ sqlite3_get_table:%s\n",__LINE__,errmsg);
return -1;
}
int j=0;
for(j=0;j<(row+1)*column;j++)
{
if(strcmp(pres[j],buf+1)==0)
{
bzero(snd,sizeof(snd));
for(int k=-3;k<7;k++)
{
strcpy(snd,pres[j+k]);
if(send(i,snd,sizeof(snd),0)<0)
{
ERR_MSG("send");
return -1;
}
}
printf("查询%s成功\n",buf+1);
sprintf(do_wh_at,"查询名字为'%s'的员工",buf+1);
time_t t;
struct tm*info=NULL;
t=time(NULL);
info=localtime(&t);
sprintf(rili,"%d-%02d-%02d:%02d:%02d:%02d",info->tm_year+1900,info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min,info->tm_sec);
sprintf(sql,"INSERT INTO root_history%d (root_id,do_what,time) values (\"%s\",\"%s\",'%s');",i,root_name,do_wh_at,rili);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
flag=1;
}
}
if(flag==0)
{
bzero(sql,sizeof(sql));
strcpy(sql,"没有这个名字");
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
sprintf(do_wh_at,"查询名字为'%s'的员工",buf+1);
time_t t;
struct tm*info=NULL;
t=time(NULL);
info=localtime(&t);
sprintf(rili,"%d-%02d-%02d %02d:%02d:%02d",info->tm_year+1900,info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min,info->tm_sec);
sprintf(sql,"INSERT INTO root_history%d values (\"%s\",\"%s\",\"%s\");",i,root_name,do_wh_at,rili);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
return -1;
}
bzero(snd,sizeof(snd));
strcpy(snd,"finish out");
if(send(i,snd,sizeof(snd),0)<0)
{
ERR_MSG("send");
return -1;
}
return 0;
}
//管理员添加新员工操作
int root_add(sqlite3 *db,char *buf,int i,char *root_name)
{
char sql[128]="select staff_id from staff";
char **pres=NULL;
int row,column;
char *errmsg=NULL;
ssize_t res=0;
char rcv[128]="";
char do_wh_at[128]="";
char rili[128]="";
time_t t;
struct tm*info=NULL;
if(buf[0]=='7')
{
if(sqlite3_get_table(db,sql,&pres,&row,&column,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"__%d__ sqlite3_get_table:%s\n",__LINE__,errmsg);
return -1;
}
int j=0;
int len=strlen(buf+1);
for(j=0;j<(row+1)*column;j++)
{
if(strcmp(pres[j],buf+1)==0)
{
bzero(sql,sizeof(sql));
strcpy(sql,"工号已存在");
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
sprintf(do_wh_at,"加入工号为%s的员工,工号重复,加入失败",buf+1);
t=time(NULL);
info=localtime(&t);
sprintf(rili,"%d-%02d-%02d:%02d:%02d:%02d",info->tm_year+1900,info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min,info->tm_sec);
sprintf(sql,"INSERT INTO root_history%d (root_id,do_what,time) values (\"%s\",'%s','%s');",i,root_name,do_wh_at,rili);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
return-1;
}
}
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
return 0;
}
if(buf[0]=='8')
{
bzero(sql,sizeof(sql));
int len=strlen(buf+1);
int len2=strlen(buf+3+len);
int lenth2=len+len2;
int len3=strlen(buf+4+lenth2);
int lenth3=lenth2+len3;
int len4=strlen(buf+5+lenth3);
int lenth4=lenth3+len4;
int len5=strlen(buf+6+lenth4);
int lenth5=lenth4+len5;
int len6=strlen(buf+7+lenth5);
int lenth6=lenth5+len6;
int len7=strlen(buf+8+lenth6);
int lenth7=lenth6+len7;
int len8=strlen(buf+9+lenth7);
int lenth8=lenth7+len8;
int len9=strlen(buf+10+lenth8);
sprintf(sql,"INSERT INTO staff values ('%s','%c','%s','%s','%s','%s','%s','%s','%s','%s');",buf+1,buf[2+len],buf+3+len,buf+4+lenth2,buf+5+lenth3,buf+6+lenth4,buf+7+lenth5,buf+8+lenth6,buf+9+lenth7,buf+10+lenth8);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
sprintf(do_wh_at,"加入工号为%s的员工%s,加入成功",buf+1,buf+4+lenth2);
t=time(NULL);
info=localtime(&t);
sprintf(rili,"%d-%02d-%02d:%02d:%02d:%02d",info->tm_year+1900,info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min,info->tm_sec);
sprintf(sql,"INSERT INTO root_history%d (root_id,do_what,time) values (\"%s\",'%s','%s');",i,root_name,do_wh_at,rili);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
printf("员工%s加入成功\n",buf+3+len);
return 0;
}
}
//管理员删除成员
int root_del(sqlite3 *db,char *buf,int i,char *root_name)
{
char sql[128]="select staff_id from staff";
char **pres=NULL;
int row,column;
char *errmsg=NULL;
ssize_t res=0;
char rcv[128]="";
char do_wh_at[128]="";
char rili[128]="";
time_t t;
struct tm*info=NULL;
if(sqlite3_get_table(db,sql,&pres,&row,&column,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"__%d__ sqlite3_get_table:%s\n",__LINE__,errmsg);
return -1;
}
int j=0;
int len=strlen(buf+1);
for(j=0;j<(row+1)*column;j++)
{
if(strcmp(pres[j],buf+1)==0)
{
bzero(sql,sizeof(sql));
sprintf(sql,"delete from staff where staff_id='%s'",buf+1);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
sprintf(do_wh_at,"%s删除工号为%s的员工,删除成功",root_name,buf+1);
t=time(NULL);
info=localtime(&t);
sprintf(rili,"%d-%02d-%02d:%02d:%02d:%02d",info->tm_year+1900,info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min,info->tm_sec);
sprintf(sql,"INSERT INTO root_history%d (root_id,do_what,time) values (\"%s\",'%s','%s');",i,root_name,do_wh_at,rili);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
printf("%s删除成员%s成功\n",root_name,buf+1);
return 0;
}
}
strcpy(sql,"工号不存在");
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
sprintf(do_wh_at,"%s删除工号为%s的员工,工号不存在,删除失败",root_name,buf+1);
t=time(NULL);
info=localtime(&t);
sprintf(rili,"%d-%02d-%02d:%02d:%02d:%02d",info->tm_year+1900,info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min,info->tm_sec);
sprintf(sql,"INSERT INTO root_history%d (root_id,do_what,time) values (\"%s\",'%s','%s');",i,root_name,do_wh_at,rili);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
printf("%s删除成员失败\n",root_name);
return 0;
}
//管理员修改成员
int root_change(sqlite3 *db,char *buf,int i,char *root_name)
{
char sql[128]="select staff_id from staff";
char **pres=NULL;
int row,column;
char *errmsg=NULL;
ssize_t res=0;
char rcv[128]="";
char do_wh_at[128]="";
char rili[128]="";
time_t t;
struct tm*info=NULL;
char change_what[20]="";
if(sqlite3_get_table(db,sql,&pres,&row,&column,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"__%d__ sqlite3_get_table:%s\n",__LINE__,errmsg);
return -1;
}
int j=0;
int len=strlen(buf+1);
for(j=0;j<(row+1)*column;j++)
{
if(strcmp(pres[j],buf+1)==0)
{
bzero(sql,sizeof(sql));
switch(buf[len+2])
{
case '1':
strcpy(change_what,"staff_root");
break;
case '2':
strcpy(change_what,"staff_pwd");
break;
case '3':
strcpy(change_what,"staff_name");
break;
case '4':
strcpy(change_what,"staff_sex");
break;
case '5':
strcpy(change_what,"staff_birth");
break;
case '6':
strcpy(change_what,"staff_age");
break;
case '7':
strcpy(change_what,"staff_phone");
break;
case '8':
strcpy(change_what,"staff_work");
break;
case '9':
strcpy(change_what,"staff_money");
break;
}
sprintf(sql,"update staff set '%s'='%s'where staff_id='%s'",change_what,buf+3+len,buf+1);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
sprintf(do_wh_at,"%s修改工号为%s的员工,将%s修改成%s",root_name,buf+1,change_what,buf+3+len);
t=time(NULL);
info=localtime(&t);
sprintf(rili,"%d-%02d-%02d:%02d:%02d:%02d",info->tm_year+1900,info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min,info->tm_sec);
sprintf(sql,"INSERT INTO root_history%d (root_id,do_what,time) values (\"%s\",'%s','%s');",i,root_name,do_wh_at,rili);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
printf("%s修改工号为%s的员工,将%s修改成%s\n",root_name,buf+1,change_what,buf+3+len);
return 0;
}
}
strcpy(sql,"工号不存在");
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
sprintf(do_wh_at,"%s修改工号为%s的员工,工号不存在,修改失败",root_name,buf+1);
t=time(NULL);
info=localtime(&t);
sprintf(rili,"%d-%02d-%02d:%02d:%02d:%02d",info->tm_year+1900,info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min,info->tm_sec);
sprintf(sql,"INSERT INTO root_history%d (root_id,do_what,time) values (\"%s\",'%s','%s');",i,root_name,do_wh_at,rili);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
printf("%s修改%s失败\n",root_name,change_what);
return 0;
}
//管理员历史记录
int root_his(sqlite3 *db,int i,char *root_name)
{
char sql[128] = "";
sprintf(sql,"select * from root_history%d",i);
char ** pres = NULL; //存储查询结果的首地址
int row, column; //查询结果的行列数
char* errmsg = NULL;
char do_wh_at[50]="";
char rili[128]="";
if(sqlite3_get_table(db, sql, &pres, &row, &column, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_get_table:%s\n", __LINE__, errmsg);
return -1;
}
//能运行到当前位置则说明,查询函数运行成功
sprintf(do_wh_at,"查询操作历史");
time_t t;
struct tm*info=NULL;
t=time(NULL);
info=localtime(&t);
sprintf(rili,"%d-%02d-%02d:%02d:%02d:%02d",info->tm_year+1900,info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min,info->tm_sec);
sprintf(sql,"INSERT INTO root_history%d (root_id,do_what,time) values (\"%s\",\"%s\",'%s');",i,root_name,do_wh_at,rili);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
int j = 0;
char snd[128]="";
for(j=0; j<(row+1)*column; j++)
{
bzero(snd,sizeof(snd));
strcpy(snd,pres[j]);
if(send(i,snd,sizeof(snd),0)<0)
{
ERR_MSG("send");
return -1;
}
}
bzero(snd,sizeof(snd));
strcpy(snd,"finish out");
if(send(i,snd,sizeof(snd),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("历史操作查询成功\n");
return 0;
}
//员工自我信息
int staff_msg(sqlite3 *db,char *buf,int i)
{
char sql[128]="";
sprintf(sql,"select * from staff where staff_id=\'%s\';",buf+1);
char **pres=NULL;
int row,column;
char *errmsg=NULL;
ssize_t res=0;
char snd[128]="";
if(sqlite3_get_table(db,sql,&pres,&row,&column,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"__%d__ sqlite3_get_table:%s\n",__LINE__,errmsg);
return -1;
}
int j=0;
int len=strlen(buf+1);
for(j=0;j<(row+1)*column;j++)
{
bzero(snd,sizeof(snd));
strcpy(snd,pres[j]);
if(send(i,snd,sizeof(snd),0)<0)
{
ERR_MSG("send");
return -1;
}
}
strcpy(sql,"finish out");
if(send(i,sql,sizeof(sql),0)<0)
{
ERR_MSG("send");
return -1;
}
return 0;
}
//员工修改自身信息
int staff_change(sqlite3 *db,char *buf,int i)
{
char sql[128]="";
char **pres=NULL;
int row,column;
char *errmsg=NULL;
ssize_t res=0;
char rcv[128]="";
char change_what[20]="";
int len=strlen(buf+1);
bzero(sql,sizeof(sql));
switch(buf[len+2])
{
case '1':
strcpy(change_what,"staff_pwd");
break;
case '2':
strcpy(change_what,"staff_name");
break;
case '3':
strcpy(change_what,"staff_sex");
break;
case '4':
strcpy(change_what,"staff_birth");
break;
case '5':
strcpy(change_what,"staff_age");
break;
case '6':
strcpy(change_what,"staff_phone");
break;
case '7':
strcpy(change_what,"staff_work");
break;
case '8':
strcpy(change_what,"staff_money");
break;
}
sprintf(sql,"update staff set '%s'='%s'where staff_id='%s'",change_what,buf+3+len,buf+1);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
return -1;
}
return 0;
}
//创建staff这个表格,工号为主键,唯一
int create_staff(sqlite3 *db)
{
char* sql = "create table staff (staff_id char primary key, staff_root char, staff_pwd char,staff_name char,staff_sex char,staff_birth char,staff_age char,staff_phone char,staff_work char,staff_money char);";
char *errmsg=NULL;
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
if(sqlite3_errcode(db)!=1)
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
}
printf("员工信息表格加载完成\n");
}
//创建create_root_history这个表格
int create_root_history(sqlite3 *db,int newfd)
{
char sql[128] = "";
sprintf(sql,"create table root_history%d (root_id char,do_what char,time int);",newfd);
char *errmsg=NULL;
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
if(sqlite3_errcode(db)!=1)
fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
}
printf("管理员历史记录表格加载完成\n");
}
//关闭管理员历史表格
int delete_root_history(sqlite3 *db,int i)
{
char sql[128]="";
sprintf(sql,"drop table root_history%d;",i);
char *errmsg=NULL;
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
ERR_MSG("drop");
return -1;
}
printf("root_history%d表格已经删除啦\n",i);
}
//关闭数据库
int close_staff(sqlite3 *db)
{
if(sqlite3_close(db)!=SQLITE_OK)
{
printf("err_code:%d\n",sqlite3_errcode(db));
printf("errmsg:%s\n",sqlite3_errmsg(db));
fprintf(stderr,"sqlite3_open failed\n");
return -1;
}
printf("关闭数据库成功\n");
}
客户端
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <stdlib.h>
//打印错误新的宏函数
#define ERR_MSG(msg) do{\
fprintf(stderr, " __%d__ ", __LINE__);\
perror(msg);\
}while(0)
#define PORT 8888 //1024~49151
#define IP "192.168.250.100" //本机IP,用ifconfig查看
int update_maxfd(int maxfd, fd_set readfds);
//主菜单
void menu();
//管理员菜单
void root_menu();
//管理员登录模块
int root_in(int sfd);
//管理员查询菜单
void root_find_menu();
//管理员全部查询模块
int root_find_all(int sfd);
//管理员按名字查询模块
int root_find_name(int sfd);
//管理员添加成员模块
int root_add(int sfd);
//管理员删除成员模块
int root_del(int sfd);
//管理员修改成员模块
int root_change(int sfd);
//管理员历史记录模块
int root_his(int sfd);
//普通员工的登录
int staff_in(int sfd,char *staff_owner_id);
//普通员工菜单
void staff_menu();
//员工自我信息模块
int staff_msg(int sfd,char *staff_owner_id);
//员工修改自身信息模块
int staff_change(int sfd,char *staff_owner_id);
int main(int argc, const char *argv[])
{
//创建流式套接字
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("create socket success\n");
//允许端口快速重用
int reuse = 1;
if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
{
ERR_MSG("setsockopt");
return -1;
}
//填充地址信息结构体,真实的地址信息结构体与协议族相关
//AF_INET,所以详情请看man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT); //网络字节序的端口号
sin.sin_addr.s_addr = inet_addr(IP); //网络字节序的IP地址
//连接到服务器
if(connect(sfd,(struct sockaddr *)&sin,sizeof(sin))<0)
{
ERR_MSG("connect");
return -1;
}
int select_res = 0;
char buf[128] = "";
ssize_t res = 0;
int newfd = -1;
int i = 0;
char staff_owner_id[10]="";
int staff_id=0;
while(1)
{
//主菜单
menu();
bzero(buf,sizeof(buf));
char flag;
scanf("%c",&flag);
while(getchar()!=10);
//登录注册或退出
if(flag=='1')
{
//管理员登录模块
if(root_in(sfd)!=0)
continue;
while(1)
{
root_menu();
scanf("%c",&flag);
while(getchar()!=10);
if(flag=='1')
{
root_find_menu();
scanf("%c",&flag);
while(getchar()!=10);
if(flag=='1')
{
//全部查询
root_find_all(sfd);
continue;
}
else if(flag=='2')
{
//按名字查询
root_find_name(sfd);
continue;
}
else if(flag=='0')
continue;
else
{
printf("命令不对,请重新输入\n");
sleep(1);
continue;
}
}
else if(flag=='2')
{
//添加模块
root_add(sfd);
}
else if(flag=='3')
{
//删除模块
root_del(sfd);
}
else if(flag=='4')
{
//修改模块
root_change(sfd);
}
else if(flag=='5')
{
//历史记录模块
root_his(sfd);
}
else if(flag=='0')
{
//退出
close(sfd);
exit(0);
}
else
{
printf("未知命令,请重新给输入\n");
sleep(1);
}
}
}
else if(flag=='2')
{
if(staff_in(sfd,staff_owner_id)!=0)
continue;
while(1)
{
staff_menu();
scanf("%c",&flag);
while(getchar()!=10);
if(flag=='1')
{
//员工自我信息模块
staff_msg(sfd,staff_owner_id);
}
else if(flag=='2')
{
//员工修改自身信息模块
staff_change(sfd,staff_owner_id);
}
else if(flag=='0')
{
//退出系统
close(sfd);
exit(0);
}
else
{
printf("未知命令,请重新给输入\n");
sleep(1);
}
}
}
else if(flag=='0')
{
close(sfd);
exit(0);
}
else
{
printf("未知命令,请重新输入\n");
sleep(1);
}
}
close(sfd);
}
//主菜单
void menu()
{
system("clear");
printf("********************************\n");
printf("* 员工管理系统 *\n");
printf("* *\n");
printf("* 1、管理员 *\n");
printf("* 2、员工 *\n");
printf("* 0、退出 *\n");
printf("* *\n");
printf("********************************\n");
}
//管理员登录
int root_in(int sfd)
{
system("clear");
char buf[128]="";
char rcv[128]="";
ssize_t res=0;
buf[0]='1';
while(1)
{
printf("请输入管理员工号,回主界面请输入esc!\n");
bzero(rcv,sizeof(rcv));
bzero(buf+1,sizeof(buf)-1);
fgets(buf+1,sizeof(buf)-1,stdin);
buf[strlen(buf)-1]=0;
if(strcmp(buf+1,"esc!")==0)
return -1;
if(send(sfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
res=recv(sfd,rcv,sizeof(rcv),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(res==0)
{
printf("服务器崩了\n");
}
else
{
if(strcmp(rcv,"非管理员")==0)
{
printf("%s,请重新输入\n",rcv);
}
else
break;
}
}
printf("请输入密码,退出输入esc!\n");
while(1)
{
buf[0]='2';
int len=strlen(buf);
bzero(buf+len+1,sizeof(buf)-1-len);
fgets(buf+len+1,sizeof(buf)-1-len,stdin);
int len_owner=strlen(buf+len+1);
buf[len+len_owner]=0;
if(strcmp(buf+2+len,"esc!")==0)
return -1;
if(send(sfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
res=recv(sfd,rcv,sizeof(rcv),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(res==0)
{
printf("服务器崩了\n");
}
else
{
if(strcmp(rcv,"密码不正确")==0)
{
system("clear");
printf("%s,请重新输入\n",rcv);
}
else
{
printf("登陆成功\n");
break;
}
}
}
return 0;
}
//管理员菜单
void root_menu()
{
system("clear");
printf("********************************\n");
printf("* 管理员 *\n");
printf("* *\n");
printf("* 1、查询 *\n");
printf("* 2、添加 *\n");
printf("* 3、删除 *\n");
printf("* 4、修改 *\n");
printf("* 5、历史记录 *\n");
printf("* 0、退出程序 *\n");
printf("* *\n");
printf("********************************\n");
}
//管理员查询菜单
void root_find_menu()
{
system("clear");
printf("********************************\n");
printf("* 查询 *\n");
printf("* *\n");
printf("* 1、查询所有 *\n");
printf("* 2、按名字查询 *\n");
printf("* 0、退出至上一层 *\n");
printf("* *\n");
printf("********************************\n");
}
//管理员全部查询模块
int root_find_all(int sfd)
{
system("clear");
char buf[128]="";
char rcv[128]="";
ssize_t res=0;
buf[0]='5';
if(send(sfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
int i=1;
printf("工号 权限 密码 姓名 性别 出生年月 年龄 手机号 岗位 薪资\n");
while(1)
{
bzero(rcv,sizeof(rcv));
res=recv(sfd,rcv,sizeof(rcv),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(res==0)
break;
else if(strcmp(rcv,"finish out")==0)
break;
else
{
//printf("%s\t\t\t",rcv);
if(i/11==0)
{
i++;
continue;
}
printf("%s\t",rcv);
if(i%10==0)
{
putchar(10);
}
}
i++;
}
printf("退出查询请输入esc!退回管理员查询!");
bzero(buf+1, sizeof(buf)-1);
fgets(buf+1,sizeof(buf)-1,stdin);
buf[strlen(buf)-1]=0;
if(strcmp(buf+1,"esc!")==0)
return -1;
return 0;
}
//管理员按名字查询模块
int root_find_name(int sfd)
{
system("clear");
char buf[128]="";
char rcv[128]="";
ssize_t res=0;
buf[0]='6';
printf("请输入要查询的员工的名字\n");
fgets(buf+1,sizeof(buf)-1,stdin);
buf[strlen(buf)-1]=0;
if(send(sfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
res=recv(sfd,rcv,sizeof(rcv),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(res==0)
printf("服务器没了\n");
else
{
if(strcmp(rcv,"没有这个名字")==0)
{
printf("%s,请重试\n",rcv);
sleep(1);
return -1;
}
else
{
int k=2;
printf("工号 权限 密码 姓名 性别 出生年月 年龄 手机号 岗位 薪资\n");
printf("%s\t",rcv);
while(1)
{
bzero(rcv,sizeof(rcv));
res=recv(sfd,rcv,sizeof(rcv),0);
if(strcmp(rcv,"finish out")==0)
break;
printf("%s\t",rcv);
if(k%10==0)
putchar(10);
k++;
}
}
printf("退出查询请输入esc!");
bzero(buf+1, sizeof(buf)-1);
fgets(buf+1,sizeof(buf)-1,stdin);
buf[strlen(buf)-1]=0;
if(strcmp(buf+1,"esc!")==0)
return 0;
}
return 0;
}
//管理员添加成员模块
int root_add(int sfd)
{
system("clear");
char buf[128]="";
char rcv[128]="";
ssize_t res=0;
buf[0]='7';
while(1)
{
printf("请输入您要添加的工号,工号为唯一,请不要重复输入,退出请输入esc!\n");
bzero(buf+1,sizeof(buf)-1);
fgets(buf+1,sizeof(buf)-1,stdin);
buf[strlen(buf)-1]=0;
if(strcmp(buf+1,"esc!")==0)
return -1;
if(send(sfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
bzero(rcv,sizeof(rcv));
res=recv(sfd,rcv,sizeof(rcv),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(res==0)
printf("服务器没了\n");
else
{
if(strcmp(rcv,"工号已存在")==0)
{
printf("%s已存在,请重新输入\n",rcv);
sleep(1);
system("clear");
}
else
{
printf("工号为唯一,请继续\n");
break;
}
}
}
buf[0]='8';
int lenth=0;
int len=strlen(buf+1);
printf("请输入新添加的员工是否有管理员权限,Y有N无\n");
bzero(buf+1+len, sizeof(buf)-1-len);
fgets(buf+2+len,sizeof(buf)-2-len,stdin);
printf("请输入新添加的员工的密码\n");
fgets(buf+3+len,sizeof(buf)-3-len,stdin);
int len2=strlen(buf+3+len);
buf[len+len2+2]=0;
lenth=len+len2;
printf("请输入新添加的员工的姓名\n");
fgets(buf+3+lenth,sizeof(buf)-3-lenth,stdin);
int len3=strlen(buf+3+lenth);
lenth+=len3;
buf[2+lenth]=0;
printf("请输入新添加的员工的性别\n");
fgets(buf+3+lenth,sizeof(buf)-3-lenth,stdin);
int len4=strlen(buf+3+lenth);
lenth+=len4;
buf[2+lenth]=0;
printf("请输入新添加的员工的出生日\n");
fgets(buf+3+lenth,sizeof(buf)-3-lenth,stdin);
int len5=strlen(buf+3+lenth);
lenth+=len5;
buf[2+lenth]=0;
printf("请输入新添加的员工的年龄\n");
fgets(buf+3+lenth,sizeof(buf)-3-lenth,stdin);
int len6=strlen(buf+3+lenth);
lenth+=len6;
buf[2+lenth]=0;
printf("请输入新添加的员工的手机号\n");
fgets(buf+3+lenth,sizeof(buf)-3-lenth,stdin);
int len7=strlen(buf+3+lenth);
lenth+=len7;
buf[2+lenth]=0;
printf("请输入新添加的员工的岗位\n");
fgets(buf+3+lenth,sizeof(buf)-3-lenth,stdin);
int len8=strlen(buf+3+lenth);
lenth+=len8;
buf[2+lenth]=0;
printf("请输入新添加的员工的薪资\n");
fgets(buf+3+lenth,sizeof(buf)-3-lenth,stdin);
int len9=strlen(buf+3+lenth);
lenth+=len9;
buf[2+lenth]=0;
if(send(sfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("新员工添加完成\n");
system("clear");
return 0;
}
//管理员删除成员模块
int root_del(int sfd)
{
system("clear");
char buf[128]="";
char rcv[128]="";
ssize_t res=0;
buf[0]='9';
printf("请输入您要删除的工号,回退请输入esc!\n");
fgets(buf+1,sizeof(buf)-1,stdin);
buf[strlen(buf)-1]=0;
if(strcmp(buf+1,"esc!")==0)
return -1;
if(send(sfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
res=recv(sfd,rcv,sizeof(rcv),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(res==0)
{
printf("服务器没了\n");
return -1;
}
else
{
if(strcmp(rcv,"工号不存在")==0)
printf("%s\n",rcv);
else
printf("删除成功\n");
sleep(1);
}
}
//管理员修改成员模块
int root_change(int sfd)
{
system("clear");
char buf[128]="";
char rcv[128]="";
ssize_t res=0;
buf[0]='a';
printf("请输入您要修改的工号,回退请输入esc!\n");
fgets(buf+1,sizeof(buf)-1,stdin);
buf[strlen(buf)-1]=0;
int len=strlen(buf+1);
printf("请输入您要修改什么,1、权限2、密码3、姓名4、性别5、出生日6、年龄7、手机号8、岗位9、薪资\n");
fgets(buf+2+len,sizeof(buf)-2-len,stdin);
printf("请输入您要改成什么\n");
fgets(buf+3+len,sizeof(buf)-3-len,stdin);
int len2=strlen(buf+3+len);
buf[2+len+len2]=0;
if(strcmp(buf+1,"esc!")==0)
return -1;
if(send(sfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
res=recv(sfd,rcv,sizeof(rcv),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(res==0)
{
printf("服务器没了\n");
return -1;
}
else
{
if(strcmp(rcv,"工号不存在")==0)
printf("%s\n",rcv);
else
printf("修改成功\n");
sleep(1);
}
}
//管理员历史记录模块
int root_his(int sfd)
{
system("clear");
char buf[128]="";
char rcv[128]="";
ssize_t res=0;
buf[0]='b';
if(send(sfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
int i=1;
printf("工号 历史操作 时间\n");
while(1)
{
bzero(rcv,sizeof(rcv));
res=recv(sfd,rcv,sizeof(rcv),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(res==0)
break;
else if(strcmp(rcv,"finish out")==0)
break;
else if(i<4)
{
i++;
continue;
}
else
{
printf("%s\t",rcv);
if(i%3==0)
{
putchar(10);
}
}
i++;
}
printf("退出记录请输入esc!");
bzero(buf+1, sizeof(buf)-1);
fgets(buf+1,sizeof(buf)-1,stdin);
buf[strlen(buf)-1]=0;
if(strcmp(buf+1,"esc!")==0)
return -1;
}
//普通员工的登录
int staff_in(int sfd,char *staff_owner_id)
{
system("clear");
char buf[128]="";
char rcv[128]="";
ssize_t res=0;
buf[0]='3';
while(1)
{
printf("请输入工号,回主界面请输入esc!\n");
bzero(rcv,sizeof(rcv));
bzero(buf+1, sizeof(buf)-1);
fgets(buf+1,sizeof(buf)-1,stdin);
buf[strlen(buf)-1]=0;
if(strcmp(buf+1,"esc!")==0)
return -1;
if(send(sfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
res=recv(sfd,rcv,sizeof(rcv),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(res==0)
{
printf("服务器崩了\n");
}
else
{
if(strcmp(rcv,"工号不存在")==0)
{
printf("%s,请重新输入\n",rcv);
}
else if(strcmp(rcv,"是管理员")==0)
{
printf("%s,请重新输入\n",rcv);
}
else
break;
}
}
printf("请输入密码,退出输入esc!\n");
while(1)
{
buf[0]='4';
int len=strlen(buf);
bzero(buf+len+1,sizeof(buf)-1-len);
fgets(buf+len+1,sizeof(buf)-1-len,stdin);
int len_owner=strlen(buf+len+1);
buf[len+len_owner]=0;
if(strcmp(buf+2+len,"esc!")==0)
return -1;
if(send(sfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
res=recv(sfd,rcv,sizeof(rcv),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(res==0)
{
printf("服务器崩了\n");
}
else
{
if(strcmp(rcv,"密码不正确")==0)
{
system("clear");
printf("%s,请重新输入\n",rcv);
}
else
{
printf("登陆成功\n");
strcpy(staff_owner_id,buf+1);
break;
}
}
}
return 0;
}
//普通员工菜单
void staff_menu()
{
system("clear");
printf("********************************\n");
printf("* 员工界面 *\n");
printf("* *\n");
printf("* 1、自我信息 *\n");
printf("* 2、修改信息 *\n");
printf("* 0、退出程序 *\n");
printf("* *\n");
printf("********************************\n");
}
//员工自我信息模块
int staff_msg(int sfd,char * staff_owner_id)
{
system("clear");
char buf[128]="";
char rcv[128]="";
ssize_t res=0;
buf[0]='c';
strcpy(buf+1,staff_owner_id);
if(send(sfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
int k=0;
printf("工号 权限 密码 姓名 性别 出生年月 年龄 手机号 岗位 薪资\n");
while(1)
{
res=recv(sfd,rcv,sizeof(rcv),0);
if(res<0)
{
ERR_MSG("recv");
return -1;
}
else if(res==0)
{
printf("服务器没了\n");
return -1;
}
else if(strcmp(rcv,"finish out")==0)
break;
else
{
if(k<10)
{
k++;
continue;
}
fflush(stdin);
printf("%s\t",rcv);
k++;
}
}
printf("\n退出员工自我信息查询请输入esc!\n");
bzero(buf+1, sizeof(buf)-1);
fgets(buf+1,sizeof(buf)-1,stdin);
buf[strlen(buf)-1]=0;
if(strcmp(buf+1,"esc!")==0)
return 0;
return 0;
}
//员工修改自身信息模块
int staff_change(int sfd,char * staff_owner_id)
{
system("clear");
char buf[128]="";
char rcv[128]="";
ssize_t res=0;
buf[0]='d';
strcpy(buf+1,staff_owner_id);
int len=strlen(buf+1);
printf("请输入您要修改什么,1、密码2、姓名3、性别4、出生日5、年龄6、手机号7、岗位8、薪资\n");
fgets(buf+2+len,sizeof(buf)-2-len,stdin);
printf("请输入您要改成什么\n");
fgets(buf+3+len,sizeof(buf)-3-len,stdin);
int len2=strlen(buf+3+len);
buf[2+len+len2]=0;
if(send(sfd,buf,sizeof(buf),0)<0)
{
ERR_MSG("send");
return -1;
}
printf("修改完成\n");
}
员工管理系统视屏演示