#include "head.h"
int process_user_or_admin_login_request(int acceptfd,MSG *msg);
int process_user_modify_request(int acceptfd, MSG *msg);
int process_user_query_request(int acceptfd,MSG *msg);
int process_admin_modify_request(int acceptfd,MSG *msg);
int process_admin_adduser_request(int acceptfd,MSG *msg);
int history_callback(void *atg,int ncolumn,char **f_value,char **f_name);
int process_client_quit_request(int acceptfd,MSG *msg);
int process_admin_history_request(int acceptfd,MSG *msg);
int process_admin_query_request(int acceptfd,MSG *msg);
int process_admin_deluser_request(int acceptfd,MSG *msg);
int process_client_request(int acceptfd,MSG *msg);
void history_init(MSG *msg,char *buf);
sqlite3* db;
int main(int argc, const char *argv[])
{
//socket->填充->绑定->监听->等待连接->数据交互->关闭:w
int sockfd;
int acceptfd;
ssize_t recvbytes;
struct sockaddr_in serveraddr;
struct sockaddr_in clientaddr;
socklen_t addrlen = sizeof(serveraddr);
socklen_t cli_len = sizeof(clientaddr);
MSG msg;
//thread_data_t tid _data;
char *errmsg;
if(sqlite3_open("./staff.db",&db) != SQLITE_OK)
{
printf("sqlite3 open error");
}else {
printf("成功");
}
if(sqlite3_exec(db,"create table userinfo(staffno integer primary key,usertype integer,name text,passwd text,age integer,iphone text,addr text,work text,data text,level integer,salary REAL);",NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("%s\n",errmsg);
}
else
{
printf("成功\n");
}
if(sqlite3_exec(db,"create table historyinfo(time text,name text,words text);",NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("%s\n",errmsg);
}
else
{
printf("成功\n");
}
//创建网络通信的套接字
sockfd = socket(AF_INET,SOCK_STREAM,0);
if (sockfd == -1)
{
perror("socket failed.\n");
exit(-1);
}
printf("sockfd:%d.\n",sockfd);
//运行绑定地址快速套用
int b_reuse = 1;
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&b_reuse,sizeof(int));
printf("setsockopt success\n");
//填充网络结构体
memset(&serveraddr,0,sizeof(serveraddr));
memset(&clientaddr,0,sizeof(clientaddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(atoi(PORT));
serveraddr.sin_addr.s_addr = inet_addr(SER_ADDR);
//绑定网络套接字和网络结构体
if(bind(sockfd,(const struct sockaddr *)&serveraddr,addrlen) == -1)
{
printf("bind failed.\n");
exit(-1);
}
printf("bind success\n");
//监听套接字,将主动套接字转化位被动套接字
if(listen(sockfd,10) == -1)
{
printf("listen failed.\n");
exit(-1);
}
printf("listen success\n");
//定义一张表
fd_set readfds,tempfds;
printf("set success\n");
//清空表
FD_ZERO(&readfds);
FD_ZERO(&tempfds);
printf("清空成功\n");
//添加要监听的事件
FD_SET(sockfd,&readfds);
printf("set succcess\n");
int nfds = sockfd;
int retval;
int i = 0;
while(1)
{
printf("1\n");
tempfds = readfds;
retval = select(nfds + 1, &tempfds,NULL,NULL, NULL);
printf("2\n");
for(i=0;i<nfds + 1; i++)
{
if(FD_ISSET(i,&tempfds))
{
if(i==sockfd)
{
acceptfd = accept(sockfd,(struct sockaddr *)&clientaddr,&cli_len);
if(acceptfd == -1)
{
printf("acceptfd failed.\n");
exit(-1);
}
printf("ip : %s.\n",inet_ntoa(clientaddr.sin_addr));
FD_SET(acceptfd,&readfds);
nfds =nfds > acceptfd ? nfds:acceptfd;
}else
{
printf("success");
recvbytes =recv(i,&msg, sizeof(msg), 0);
printf("msg.type :%#x.\n", msg.msgtype);
if(recvbytes ==-1)
{
printf ("recv failed. \n");
continue;
}else if(recvbytes==0)
{
printf("peer shutdown.\n");
close(i);
FD_CLR(i,&readfds);
}else
{
process_client_request(i,&msg);
}
}
}
}
}
close(sockfd);
return 0;
}
void history_init(MSG *msg,char *buf){
char *errmsg;
time_t t;
char bbf[10];
char a[128];
struct tm *p;
time(&t);
p=gmtime(&t);
sprintf(bbf,"%d.%d.%d",p->tm_year+1900,p->tm_mon+1,p->tm_mday);
sprintf(a,"INSERT INTO historyinfo VALUES(%s,%s,%s)",bbf,msg->name,buf);
sqlite3_exec(db,a,NULL,NULL,&errmsg);
}
int process_user_or_admin_login_request(int acceptfd,MSG *msg)
{
printf("-----------------%s---------------------%d\n",__func__,__LINE__);
//封装sql命令,表中查询用户名和密码-存在-登录成功-发送响应-失败-发送失败响应
char sql[DATALEN] = {0};
char *errmsg;
char **resultp;
int nrow,ncolumn;
msg->info.usertype = msg->usertype;
strcpy (msg->info.name,msg->name);
strcpy (msg->info.passwd,msg->passwd);
printf("uesrtype:%#x-------username:%s--------passwd:%s\n",msg->info.usertype,msg->info.name,msg->info.passwd);
sprintf(sql,"select * from userinfo where usertype=%d and name = '%s' and passwd = '%s';",msg->info.usertype,msg->info.name,msg->info.passwd);
printf("%s\n",sql);
if (sqlite3_get_table(db,sql,&resultp,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
{
printf("---***---%s.\n",errmsg);
}
else
{
if(nrow == 0)
{
strcpy(msg->recvmsg,"用户名或者密码错误:\n");
send(acceptfd,msg,sizeof(MSG),0);
}
else
{
strcpy(msg->recvmsg,"OK");
send(acceptfd,msg,sizeof(MSG),0);
}
}
return 0;
}
//普通员工查询
int process_user_query_request(int acceptfd,MSG *msg)
{
printf("---------%s-------------%d\n",__func__,__LINE__);
//检查msg->flags----->封装sql命令-查找历史记录表-回调函数-发送查询结果-发送结束标志
int i = 0,j = 0;
char sql[DATALEN] = {0};
char **resultp;
int nrow,ncolumn;
char *errmsg;
sprintf(sql,"select * from userinfo where name='%s';",msg->name);//通过员工姓名查询员工信息
if(sqlite3_get_table(db,sql,&resultp,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
{
printf("%s.\n",errmsg);
}
else
{
printf("searching.....\n");
for(i = 0;i<ncolumn;i++)
{
printf("%-8s ",resultp[i]);
}
puts("");
puts("==========================================================");
int index =ncolumn;
for (i =0; i <nrow; i ++)
{
printf("%s %s %s %s %s %s %s %s %s %s %s \n", resultp[index+ncolumn-11], resultp[index+ncolumn-10],resultp[index+ncolumn-9],resultp[index+ncolumn-8],resultp[index+ncolumn-7],resultp[index+ncolumn-6],resultp[index+ncolumn-5],resultp[index+ncolumn-4],resultp[index+ncolumn-3],resultp[index+ncolumn-2],resultp[index+ncolumn-1]);
sprintf(msg->recvmsg, "%s, %s,%s, %s, %s, %s, %s, %s, %s, %s, %s:",resultp[index+ncolumn-11],resultp[index+ncolumn-10],resultp[index+ncolumn-9],resultp[index+ncolumn-8], resultp[index+ncolumn-7],resultp[index+ncolumn-6],resultp[index+ncolumn-5],resultp[index+ncolumn-4], resultp[index+ncolumn-3], resultp[index+ncolumn-2], resultp[index+ncolumn-1]);
send (acceptfd, msg, sizeof (MSG), 0);
usleep (1000);
puts("=================================");
index += ncolumn;
}
if(msg->flags !=1)
{ //全部查询的时候不知道何时结束,需要手动发送结束标志位,但是按人名查找不需要通知 方查询结束了
strcpy(msg->recvmsg, "over*");
send(acceptfd, msg, sizeof(MSG),0);
}
sqlite3_free_table(resultp);
printf("sqlite3_get_table successfully.\n");
}
}
//管理员查询员工信息
int process_admin_query_request(int acceptfd,MSG *msg)
{
printf("---------%s-------------%d\n",__func__,__LINE__);
//检查msg->flags----->封装sql命令-查找历史记录表-回调函数-发送查询结果-发送结束标志
int i = 0,j = 0;
char sql[DATALEN] = {0};
char **resultp;
int nrow,ncolumn;
char *errmsg;
if (msg->flags ==1)//按人名查询
{
sprintf(sql,"select * from userinfo where name='%s';",msg->info.name);//通过员工姓名查询员工信息
}
else
{
sprintf(sql,"select * from userinfo;");
}
if(sqlite3_get_table(db,sql,&resultp,&nrow,&ncolumn,&errmsg) != SQLITE_OK)
{
printf("%s.\n",errmsg);
strcpy(msg->recvmsg, "over*");
send(acceptfd, msg, sizeof(MSG),0);
}
else
{
printf("searching.....\n");
printf("ncolumn :
qq_41920615
- 粉丝: 0
- 资源: 1