客户端
client_head_h
#ifndef __CLIENT_H__
#define __CLIENT_H__
#include <sys/types.h>
#include <sys/socket.h>
#include<stdio.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <strings.h>
#include <string.h>
#include<stdlib.h>
#include <signal.h>
#define PORT 2223
#define IP "192.168.250.100"
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
}while(0)
struct info
{
char account[20];
char password[20];
int workno;
int age;
char address[20];
int phone;
char posi[20];
char data[20];
int laver;
int salary;
};
typedef struct msg
{
char status;
int usertype;
int cmdtype;
char buf[32];
struct info st;
}Msg;
typedef struct History{
char type;
char name[20];
char date[200];
char data[20];
char time[20];
}His;
int do_manager_mode(int sfd,char*name);
int do_user_mode(int sfd, char*name);
int do_Add_User(int sfd);
int do_Del_User(int sfd);
int do_See_Manager(int sfd);
int Seek_Name(Msg sndbuf,int sfd);
int Seek_workno(Msg sndbuf,int sfd);
int do_See_History(int sfd);
int do_See_Modi(int sfd);
int Modi_Name(Msg sndbuf,int sfd);
int Modi_workno(Msg sndbuf,int sfd);
int do_See_User(int sfd,Msg name);
int do_Modi_User(int sfd,Msg name);
#endif
client.c
#include "client_head.h"
#include "client_test.c"
Msg name;
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket suess__%d__\n", __LINE__);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
if (-1 == connect(sfd, (struct sockaddr *)&sin, sizeof(sin)))
{
ERR_MSG("connet");
return -1;
}
printf("connect serve success\n");
int choose;
while (1)
{
BEGIN:
printf("**************************\n");
printf("**********1管理员模式******\n");
printf("**********2普通用户模式****\n");
printf("**********3退出***********\n");
printf("**************************\n");
printf("请输入>>>\n");
scanf("%d", &choose);
switch (choose)
{
case 1:
if (1 == do_manager_mode(sfd,name.st.account))
{
goto MANAGER;
}
else
{
goto BEGIN;
}
break;
case 2:
if (1 == do_user_mode(sfd,name.st.account))
{
printf("sssss46姓名:%s\n", name.st.account);
goto USER;
}
else
{
goto BEGIN;
}
break;
case 3:
goto END;
break;
default:
printf("输入错误,请重新选择__%s__ __%d__\n", __FILE__, __LINE__);
}
}
MANAGER:
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("请输入>>>\n");
scanf("%d", &choose);
switch (choose)
{
case 1:
if(1==do_See_Manager(sfd))
{
goto MANAGER;
}
break;
case 2:
if(1==do_See_Modi(sfd))
{
goto MANAGER;
}
break;
case 3:
do_Add_User(sfd);
break;
case 4:
do_Del_User(sfd);
break;
case 5:
do_See_History(sfd);
break;
case 6:
goto BEGIN;
break;
default:
printf("协议 错误 __%s__ __%d__\n", __FILE__, __LINE__);
}
}
USER:
while (1)
{
printf("**************************\n");
printf("**********1查询***********\n");
printf("**********2修改***********\n");
printf("**********3退出***********\n");
printf("*************************\n");
printf("请输入>>>\n");
scanf("%d", &choose);
switch (choose)
{
case 1:
printf("******************姓名:%s\n", name.st.account);
do_See_User(sfd,name);
break;
case 2:
do_Modi_User(sfd,name);
break;
case 3:
goto END;
break;
default:
printf("协议 错误 __%s__ __%d__\n", __FILE__, __LINE__);
}
}
END:
close(sfd);
return 0;
}
client_test_c
#include "client_head.h"
int do_manager_mode(int sfd, char*name)
{
struct msg sndbuf;
printf("请输入管理员账号:");
scanf("%s", sndbuf.st.account);
printf("请输入管理员密码:");
scanf("%s", sndbuf.st.password);
sndbuf.usertype = 1;
sndbuf.cmdtype = 6;
strcpy(name, sndbuf.st.account);
if (send(sfd, &sndbuf, sizeof(sndbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
int res = recv(sfd, &sndbuf, sizeof(sndbuf), 0);
if (res < 0)
{
ERR_MSG("recv");
return -1;
}
if (sndbuf.status == 'Y')
{
printf("登录成功\n");
return 1;
}
else if (sndbuf.status == 'Z')
{
printf("用户在线\n");
return 2;
}
else if (sndbuf.status == 'N')
{
printf("账号密码错误\n");
return 3;
}
printf("欢迎进入管理员模式\n");
return 0;
}
int do_user_mode(int sfd, char*name)
{
struct msg sndbuf;
printf("请输入用户账号:");
scanf("%s", sndbuf.st.account);
printf("请输入用户密码:");
scanf("%s", sndbuf.st.password);
sndbuf.usertype = 0;
sndbuf.cmdtype = 3;
strcpy(name,sndbuf.st.account);
if (send(sfd, &sndbuf, sizeof(sndbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
int res = recv(sfd, &sndbuf, sizeof(sndbuf), 0);
if (res < 0)
{
ERR_MSG("recv");
return -1;
}
if (sndbuf.status == 'Y')
{
printf("登录成功\n");
return 1;
}
else if (sndbuf.status == 'Z')
{
printf("用户在线\n");
return 2;
}
else if (sndbuf.status == 'N')
{
printf("账号密码错误\n");
return 3;
}
printf("欢迎进入普通用户模式\n");
return 0;
}
int do_Add_User(int sfd)
{
Msg sndbuf;
sndbuf.usertype = 1;
sndbuf.cmdtype = 3;
printf("请输入是否为管理员1/0:");
scanf("%s", sndbuf.buf);
printf("请输入账号:\n");
scanf("%s", sndbuf.st.account);
printf("请输入密码:\n");
scanf("%s", sndbuf.st.password);
printf("请输入工号:\n");
scanf("%d", &sndbuf.st.workno);
printf("请输入年龄:\n");
scanf("%d", &sndbuf.st.age);
printf("请输入地址:\n");
scanf("%s", sndbuf.st.address);
printf("请输入电话:\n");
scanf("%d", &sndbuf.st.phone);
printf("请输入职位:\n");
scanf("%s", sndbuf.st.posi);
printf("请输入时间:\n");
scanf("%s", sndbuf.st.data);
printf("请输入级别:\n");
scanf("%d", &sndbuf.st.laver);
printf("请输入工资:\n");
scanf("%d", &sndbuf.st.salary);
if (send(sfd, &sndbuf, sizeof(sndbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
return 0;
}
int do_Del_User(int sfd)
{
Msg sndbuf;
sndbuf.usertype = 1;
sndbuf.cmdtype = 4;
printf("请输入要删除的用户名\n");
scanf("%s", sndbuf.st.account);
if (send(sfd, &sndbuf, sizeof(sndbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
return 0;
}
int do_See_Manager(int sfd)
{
Msg sndbuf;
sndbuf.usertype = 1;
sndbuf.cmdtype = 1;
printf("**************************\n");
printf("**********1按名字查询******\n");
printf("**********2按工号查询******\n");
printf("**********3退出***********\n");
printf("请输入>>>>>>>>");
int choose;
scanf("%d", &choose);
switch (choose)
{
case 1:
Seek_Name(sndbuf, sfd);
break;
case 2:
Seek_workno(sndbuf, sfd);
break;
case 3:
return 1;
break;
}
return 0;
}
int Seek_Name(Msg sndbuf, int sfd)
{
sndbuf.status = 'M';
printf("请输入名字:");
scanf("%s", sndbuf.st.account);
if (send(sfd, &sndbuf, sizeof(sndbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
if (sndbuf.status == 'X')
{
printf("查询失败\n");
return -1;
}
int res = recv(sfd, &sndbuf, sizeof(sndbuf), 0);
if (res < 0)
{
ERR_MSG("recv");
return -1;
}
printf("姓名:%s\n", sndbuf.st.account);
printf("年龄:%d\n", sndbuf.st.age);
printf("地址:%s\n", sndbuf.st.address);
printf("电话:%d\n", sndbuf.st.phone);
printf("职位:%s\n", sndbuf.st.posi);
printf("日期:%s\n", sndbuf.st.data);
printf("级别:%d\n", sndbuf.st.laver);
printf("薪资:%d\n", sndbuf.st.salary);
return 0;
}
int Seek_workno(Msg sndbuf, int sfd)
{
sndbuf.status = 'W';
printf("请输入工号:");
scanf("%d", &sndbuf.st.workno);
if (send(sfd, &sndbuf, sizeof(sndbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
if (sndbuf.status == 'X')
{
printf("查询失败\n");
return -1;
}
int res = recv(sfd, &sndbuf, sizeof(sndbuf), 0);
if (res < 0)
{
ERR_MSG("recv");
return -1;
}
printf("姓名:%s\n", sndbuf.st.account);
printf("年龄:%d\n", sndbuf.st.age);
printf("地址:%s\n", sndbuf.st.address);
printf("电话:%d\n", sndbuf.st.phone);
printf("职位:%s\n", sndbuf.st.posi);
printf("日期:%s\n", sndbuf.st.data);
printf("级别:%d\n", sndbuf.st.laver);
printf("薪资:%d\n", sndbuf.st.salary);
return 0;
}
int do_See_History(int sfd)
{
Msg sndbuf;
sndbuf.usertype = 1;
sndbuf.cmdtype = 5;
if (send(sfd, &sndbuf, sizeof(sndbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
His Hisbuf;
Hisbuf.type = 'Y';
while (1)
{
if (Hisbuf.type == 'Y')
{
bzero(&Hisbuf, sizeof(Hisbuf));
int res = recv(sfd, &Hisbuf, sizeof(Hisbuf), 0);
if (res < 0)
{
ERR_MSG("recv");
return -1;
}
printf("%s\t%s\t%s\t%s\t\n", Hisbuf.name, Hisbuf.date, Hisbuf.data, Hisbuf.time);
}
else if (Hisbuf.type == 'N')
{
break;
}
}
return 0;
}
int do_See_Modi(int sfd)
{
Msg sndbuf;
sndbuf.usertype = 1;
sndbuf.cmdtype = 2;
printf("**************************\n");
printf("**********1按名字修改******\n");
printf("**********2按工号修改******\n");
printf("**********3退出***********\n");
printf("请输入>>>>>>>>");
int choose;
scanf("%d", &choose);
switch (choose)
{
case 1:
Modi_Name(sndbuf, sfd);
break;
case 2:
Modi_workno(sndbuf, sfd);
break;
case 3:
return 1;
break;
}
return 0;
}
int Modi_Name(Msg sndbuf, int sfd)
{
sndbuf.status = 'M';
printf("请输入名字:");
scanf("%s", sndbuf.st.account);
printf("修改工号:");
scanf("%d", &sndbuf.st.workno);
printf("修改年龄:");
scanf("%d", &sndbuf.st.age);
printf("修改地址:");
scanf("%s", sndbuf.st.address);
printf("修改phone:");
scanf("%d", &sndbuf.st.phone);
printf("修改职位:");
scanf("%s", sndbuf.st.posi);
printf("修改时间:");
scanf("%s", sndbuf.st.data);
printf("修改水平:");
scanf("%d", &sndbuf.st.laver);
printf("请输入薪资:");
scanf("%d", &sndbuf.st.salary);
if (send(sfd, &sndbuf, sizeof(sndbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
return 0;
}
int Modi_workno(Msg sndbuf, int sfd)
{
sndbuf.status = 'W';
printf("请输入工号:");
scanf("%d", &sndbuf.st.workno);
printf("修改名字:");
scanf("%s", sndbuf.st.account);
printf("修改年龄:");
scanf("%d", &sndbuf.st.age);
printf("修改地址:");
scanf("%s", sndbuf.st.address);
printf("修改phone:");
scanf("%d", &sndbuf.st.phone);
printf("修改职位:");
scanf("%s", sndbuf.st.posi);
printf("修改时间:");
scanf("%s", sndbuf.st.data);
printf("修改水平:");
scanf("%d", &sndbuf.st.laver);
printf("请输入薪资:");
scanf("%d", &sndbuf.st.salary);
if (send(sfd, &sndbuf, sizeof(sndbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
return 0;
}
int do_See_User(int sfd, Msg name)
{
Msg sndbuf;
sndbuf.usertype = 0;
sndbuf.cmdtype = 1;
strcpy(sndbuf.st.account, name.st.account);
if (send(sfd, &sndbuf, sizeof(sndbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
bzero(&sndbuf,sizeof(sndbuf));
int res = recv(sfd, &sndbuf, sizeof(sndbuf), 0);
if (res < 0)
{
ERR_MSG("recv");
return -1;
}
printf("姓名:%s\n", sndbuf.st.account);
printf("年龄:%d\n", sndbuf.st.age);
printf("地址:%s\n", sndbuf.st.address);
printf("电话:%d\n", sndbuf.st.phone);
printf("职位:%s\n", sndbuf.st.posi);
printf("日期:%s\n", sndbuf.st.data);
printf("级别:%d\n", sndbuf.st.laver);
printf("薪资:%d\n", sndbuf.st.salary);
return 0;
}
int do_Modi_User(int sfd,Msg name)
{
Msg sndbuf;
sndbuf.usertype = 0;
sndbuf.cmdtype = 2;
strcpy(sndbuf.st.account, name.st.account);
printf("修改年龄:");
scanf("%d", &sndbuf.st.age);
printf("修改地址:");
scanf("%s", sndbuf.st.address);
printf("修改phone:");
scanf("%d", &sndbuf.st.phone);
printf("修改职位:");
scanf("%s", sndbuf.st.posi);
printf("修改时间:");
scanf("%s", sndbuf.st.data);
printf("修改水平:");
scanf("%d", &sndbuf.st.laver);
printf("请输入薪资:");
scanf("%d", &sndbuf.st.salary);
if (send(sfd, &sndbuf, sizeof(sndbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
}
服务器
server_h
#ifndef __SERVER_H__
#define __SERVER_H__
#include <sys/types.h>
#include <sys/socket.h>
#include<stdio.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <strings.h>
#include <string.h>
#include<stdlib.h>
#include <signal.h>
#include <time.h>
#include<sqlite3.h>
#define PORT 2223
#define IP "192.168.250.100"
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
}while(0)
struct info
{
char account[20];
char password[20];
int workno;
int age;
char address[20];
int phone;
char posi[20];
char data[20];
int laver;
int salary;
};
typedef struct msg
{
char status;
int usertype;
int cmdtype;
char buf[32];
struct info st;
}Msg;
typedef struct History{
char type;
char name[20];
char date[200];
char data[20];
char time[20];
}His;
int CreateLogin();
int do_manager_mode(struct sockaddr_in cin,Msg rcvbuf,int newfd,sqlite3*db);
int do_user_mode(struct sockaddr_in cin,Msg rcvbuf,int newfd,sqlite3*db);
int ChangeUserStatus(Msg rcvbuf,sqlite3*db,int status);
int do_Add_User(struct sockaddr_in cin, Msg rcvbuf, int newfd, sqlite3 *db,char *name);
int do_Del_User(struct sockaddr_in cin, Msg rcvbuf, int newfd, sqlite3 *db,char *name);
int do_See_Manager(struct sockaddr_in cin, Msg rcvbuf, int newfd, sqlite3 *db, char *name);
int history(char *name,char *buf);
int do_See_History(struct sockaddr_in cin, Msg rcvbuf, int newfd, sqlite3 *db);
int do_See_Modi(struct sockaddr_in cin, Msg rcvbuf, int newfd, sqlite3 *db,char*name);
int do_See_User(struct sockaddr_in cin, Msg rcvbuf, int newfd, sqlite3 *db,char*name);
#endif
server_c
#include "server_head.h"
#include "server_test.c"
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if (sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("socket success __%d__\n", __LINE__);
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("bind success __%d__\n", __LINE__);
if (listen(sfd, 128) < 0)
{
ERR_MSG("listen");
return -1;
}
printf("listen success __%d__\n", __LINE__);
printf("正在创建身份密码表\n");
CreateLogin();
printf("创建身份密码表成功\n");
sqlite3 *db = NULL;
if (sqlite3_open("./dict.db", &db) != SQLITE_OK)
{
fprintf(stderr, "%d sqlite3_open fail %d|%s\n",
__LINE__, sqlite3_errcode(db), sqlite3_errmsg(db));
return -1;
}
printf("open success\n");
struct sockaddr_in cin;
socklen_t addrlen = sizeof(cin);
int newfd = -1;
while (1)
{
newfd = accept(sfd, (struct sockaddr *)&cin, &addrlen);
if (newfd < 0)
{
ERR_MSG("accept");
return -1;
}
printf("[%s : %d] newfd=%d,客户端连接成功\n",
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);
pid_t pid = fork();
if (pid == 0)
{
close(sfd);
int res;
struct msg rcvbuf;
Msg name;
while (1)
{
res = recv(newfd, &rcvbuf, sizeof(rcvbuf), 0);
if (res < 0)
{
ERR_MSG("recv");
return -1;
}
else if (res == 0)
{
fprintf(stderr, "[%s : %d] newfd=%d,客户端下线\n",
inet_ntoa(cin.sin_addr), ntohs(cin.sin_port), newfd);
ChangeUserStatus(name,db,0);
break;
}
if (rcvbuf.usertype == 0)
{
switch (rcvbuf.cmdtype)
{
case 1:
do_See_User(cin, rcvbuf, newfd, db,name.st.account);
break;
case 2:
do_Modi_User(cin, rcvbuf, newfd, db,name.st.account);
break;
case 3:
do_user_mode(cin,rcvbuf,newfd,db);
strcpy(name.st.account,rcvbuf.st.account);
break;
default:
printf("协议 %c 错误 __%s__ __%d__\n", rcvbuf.cmdtype, __FILE__, __LINE__);
}
}
else if (rcvbuf.usertype == 1)
{
switch (rcvbuf.cmdtype)
{
case 1:
do_See_Manager(cin, rcvbuf, newfd, db,name.st.account);
break;
case 2:
do_See_Modi(cin, rcvbuf, newfd, db,name.st.account);
break;
case 3:
do_Add_User(cin, rcvbuf, newfd, db,name.st.account);
break;
case 4:
do_Del_User(cin, rcvbuf, newfd, db,name.st.account);
break;
case 5:
do_See_History(cin, rcvbuf, newfd, db);
break;
case 6:
do_manager_mode(cin,rcvbuf,newfd,db);
strcpy(name.st.account,rcvbuf.st.account);
break;
default:
printf("协议 %c 错误 __%s__ __%d__\n", rcvbuf.cmdtype, __FILE__, __LINE__);
}
}
}
}
else if (pid > 0)
{
close(newfd);
}
else
{
ERR_MSG("fork");
return 0;
}
}
close(sfd);
close(newfd);
return 0;
}
test
#include "server_head.h"
int CreateLogin()
{
sqlite3 *db = NULL;
if (sqlite3_open("./dict.db", &db) != SQLITE_OK)
{
fprintf(stderr, "%d sqlite3_open fail %d|%s\n",
__LINE__, sqlite3_errcode(db), sqlite3_errmsg(db));
return -1;
}
printf("open success\n");
char sql[128] = "create table if not exists ID(name char,password int,status int,manager_user int);";
printf("sql=%s\n", sql);
char *errmsg = NULL;
if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg);
return -1;
}
char sql1[128] = "create table if not exists MSG(name char,workno int,age int,address char,phone int,posi char,data char,laver int,salary int);";
printf("sql1=%s\n", sql1);
char *errmsg1 = NULL;
if (SQLITE_OK != sqlite3_exec(db, sql1, NULL, NULL, &errmsg1) != SQLITE_OK)
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg1);
return -1;
}
char sql2[128] = "create table if not exists His(name char,msg char,data char,time char);";
printf("sql2=%s\n", sql2);
char *errmsg2 = NULL;
if (SQLITE_OK != sqlite3_exec(db, sql2, NULL, NULL, &errmsg2) != SQLITE_OK)
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg2);
return -1;
}
return 0;
}
int do_manager_mode(struct sockaddr_in cin, Msg rcvbuf, int newfd, sqlite3 *db)
{
char **pres = NULL;
int row, column;
char *errmsg = NULL;
char sql[128] = "";
sprintf(sql, "%s'%s' %s %s'%s' %s %s%d", "select *from ID where name=", rcvbuf.st.account, "and", "password=", rcvbuf.st.password, "and", "manager_user=", 1);
printf("sql=%s\n", sql);
if (SQLITE_OK != sqlite3_get_table(db, sql, &pres, &row, &column, &errmsg))
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg);
return -1;
}
if (pres[6] == NULL)
{
printf("**************\n");
rcvbuf.status = 'N';
if (send(newfd, &rcvbuf, sizeof(rcvbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
}
printf("status==%s\n", pres[6]);
if (strcmp(pres[6], "0") == 0)
{
rcvbuf.status = 'Y';
if (send(newfd, &rcvbuf, sizeof(rcvbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
ChangeUserStatus(rcvbuf, db, 1);
}
else if (strcmp(pres[6], "1") == 0)
{
rcvbuf.status = 'Z';
if (send(newfd, &rcvbuf, sizeof(rcvbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
}
return 0;
}
int do_user_mode(struct sockaddr_in cin, Msg rcvbuf, int newfd, sqlite3 *db)
{
char **pres = NULL;
int row, column;
char *errmsg = NULL;
char sql[128] = "";
sprintf(sql, "%s'%s' %s %s'%s' %s %s%d", "select *from ID where name=", rcvbuf.st.account, "and", "password=", rcvbuf.st.password, "and", "manager_user=", 0);
printf("sql=%s\n", sql);
if (SQLITE_OK != sqlite3_get_table(db, sql, &pres, &row, &column, &errmsg))
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg);
return -1;
}
if (pres[6] == NULL)
{
printf("**************\n");
rcvbuf.status = 'N';
if (send(newfd, &rcvbuf, sizeof(rcvbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
}
printf("status==%s\n", pres[6]);
if (strcmp(pres[6], "0") == 0)
{
rcvbuf.status = 'Y';
if (send(newfd, &rcvbuf, sizeof(rcvbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
ChangeUserStatus(rcvbuf, db, 1);
}
else if (strcmp(pres[6], "1") == 0)
{
rcvbuf.status = 'Z';
if (send(newfd, &rcvbuf, sizeof(rcvbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
}
return 0;
}
int ChangeUserStatus(Msg rcvbuf, sqlite3 *db, int status)
{
char sql1[128] = "";
sprintf(sql1, "%s%d %s'%s'", "update ID set status=", status, " where name=", rcvbuf.st.account);
char *errmsg = NULL;
printf("sql=%s\n", sql1);
if (SQLITE_OK != sqlite3_exec(db, sql1, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg);
}
return -1;
}
int do_Add_User(struct sockaddr_in cin, Msg rcvbuf, int newfd, sqlite3 *db, char *name)
{
char sql1[128] = "";
int status = 0;
sprintf(sql1, "%s ('%s','%s',%d,'%s')", "insert into ID values", rcvbuf.st.account, rcvbuf.st.password, status, rcvbuf.buf);
char *errmsg = NULL;
printf("sql=%s\n", sql1);
if (SQLITE_OK != sqlite3_exec(db, sql1, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg);
return -1;
}
char sql2[128] = "";
sprintf(sql2, "%s ('%s',%d,%d,'%s',%d,'%s','%s',%d,%d)", "insert into MSG values", rcvbuf.st.account, rcvbuf.st.workno,
rcvbuf.st.age, rcvbuf.st.address, rcvbuf.st.phone, rcvbuf.st.posi, rcvbuf.st.data, rcvbuf.st.laver, rcvbuf.st.salary);
char *errmsg1 = NULL;
printf("sql2=%s\n", sql2);
if (SQLITE_OK != sqlite3_exec(db, sql2, NULL, NULL, &errmsg1) != SQLITE_OK)
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg1);
return -1;
}
char buf[] = "添加用户信息";
history(name, buf);
return 0;
}
int do_Del_User(struct sockaddr_in cin, Msg rcvbuf, int newfd, sqlite3 *db, char *name)
{
char sql[128] = "";
sprintf(sql, "%s'%s'", "delete from ID where name=", rcvbuf.st.account);
printf("sql=%s\n", sql);
char *errmsg = NULL;
if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg);
return -1;
}
sprintf(sql, "%s'%s'", "delete from MSG where name=", rcvbuf.st.account);
printf("sql=%s\n", sql);
if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg);
return -1;
}
char buf[] = "进行名字删除用户账号及个人信息";
history(name, buf);
return 0;
}
int history(char *name, char *buf)
{
sqlite3 *db = NULL;
if (sqlite3_open("./dict.db", &db) != SQLITE_OK)
{
fprintf(stderr, "%d sqlite3_open fail %d|%s\n",
__LINE__, sqlite3_errcode(db), sqlite3_errmsg(db));
return -1;
}
printf("open success\n");
struct tm *info;
time_t t1 = time(NULL);
info = localtime(&t1);
char time[120];
char data[120];
sprintf(data, "%d-%d-%d", info->tm_year + 1900, info->tm_mon + 1, info->tm_mday);
sprintf(time, "%d:%d:%d", info->tm_hour, info->tm_min, info->tm_sec);
char sql1[128] = "";
sprintf(sql1, "%s ('%s','%s','%s','%s')", "insert into His values", name, buf, data, time);
char *errmsg = NULL;
printf("sql1=%s\n", sql1);
if (SQLITE_OK != sqlite3_exec(db, sql1, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg);
return -1;
}
return 0;
}
int do_See_Manager(struct sockaddr_in cin, Msg rcvbuf, int newfd, sqlite3 *db, char *name)
{
char **pres = NULL;
int row, column;
char *errmsg = NULL;
char sql[128] = "";
int flag;
if (rcvbuf.status == 'W')
{
sprintf(sql, "%s%d", "select *from MSG where workno=", rcvbuf.st.workno);
printf("%d:sql=%s\n", __LINE__, sql);
if (SQLITE_OK != sqlite3_get_table(db, sql, &pres, &row, &column, &errmsg))
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg);
return -1;
}
flag = 0;
}
else if (rcvbuf.status == 'M')
{
sprintf(sql, "%s'%s'", "select *from MSG where name=", rcvbuf.st.account);
printf("%d:sql=%s\n", __LINE__, sql);
if (SQLITE_OK != sqlite3_get_table(db, sql, &pres, &row, &column, &errmsg))
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg);
return -1;
}
flag = 1;
}
else
{
rcvbuf.status = 'X';
}
bzero(&rcvbuf, sizeof(rcvbuf));
strcpy(rcvbuf.st.account, pres[9]);
rcvbuf.st.workno = atoi(pres[10]);
rcvbuf.st.age = atoi(pres[11]);
strcpy(rcvbuf.st.address, pres[12]);
rcvbuf.st.phone = atoi(pres[13]);
strcpy(rcvbuf.st.posi, pres[14]);
strcpy(rcvbuf.st.data, pres[15]);
rcvbuf.st.laver = atoi(pres[16]);
rcvbuf.st.salary = atoi(pres[17]);
if (send(newfd, &rcvbuf, sizeof(rcvbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
if (flag == 0)
{
char buf[] = "按照工号查询个人信息";
history(name, buf);
}
else if (flag == 1)
{
char buf[] = "按照姓名查询个人信息";
history(name, buf);
}
return 0;
}
int do_See_History(struct sockaddr_in cin, Msg rcvbuf, int newfd, sqlite3 *db)
{
char **pres = NULL;
int row, column;
char *errmsg = NULL;
char sql[128] = "";
sprintf(sql, "%s", "select *from His");
printf("%d:sql=%s\n", __LINE__, sql);
if (SQLITE_OK != sqlite3_get_table(db, sql, &pres, &row, &column, &errmsg))
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg);
return -1;
}
printf("row=%d,column=%d\n", row, column);
His Hisbuf;
for (int i = column; i < (row + 1) * column; i += 4)
{
bzero(&Hisbuf, sizeof(Hisbuf));
Hisbuf.type = 'Y';
strcpy(Hisbuf.name, pres[i]);
strcpy(Hisbuf.date, pres[i + 1]);
strcpy(Hisbuf.data, pres[i + 2]);
strcpy(Hisbuf.time, pres[i + 3]);
if (send(newfd, &Hisbuf, sizeof(Hisbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
}
bzero(&Hisbuf, sizeof(Hisbuf));
Hisbuf.type = 'N';
if (send(newfd, &Hisbuf, sizeof(Hisbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
return 0;
}
int do_See_Modi(struct sockaddr_in cin, Msg rcvbuf, int newfd, sqlite3 *db, char *name)
{
char **pres = NULL;
int row, column;
char *errmsg = NULL;
int flag;
if (rcvbuf.status == 'W')
{
char sql[600] = "";
sprintf(sql, "%s'%s' %s%d %s'%s' %s%d %s'%s' %s'%s' %s%d %s%d %s%d", "update MSG set name=", rcvbuf.st.account, "age=", rcvbuf.st.age, "address=", rcvbuf.st.address, "phone=", rcvbuf.st.phone, "posi=", rcvbuf.st.posi, "data=", rcvbuf.st.data, "laver=", rcvbuf.st.laver, "salary=", rcvbuf.st.salary, "where workno=", rcvbuf.st.workno);
char *errmsg = NULL;
printf("sql=%s\n", sql);
if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg);
return -1;
}
flag = 0;
}
else if (rcvbuf.status == 'M')
{
char sql[600] = "";
sprintf(sql, "%s%d,%s%d,%s'%s',%s%d,%s'%s',%s'%s',%s%d,%s%d %s'%s'", "update MSG set workno=", rcvbuf.st.workno, "age=", rcvbuf.st.age, "address=", rcvbuf.st.address, "phone=", rcvbuf.st.phone, "posi=", rcvbuf.st.posi, "data=", rcvbuf.st.data, "laver=", rcvbuf.st.laver, "salary=", rcvbuf.st.salary, "where name=", rcvbuf.st.account);
char *errmsg = NULL;
printf("sql=%s\n", sql);
if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg);
return -1;
}
flag = 1;
}
if (flag == 0)
{
char buf[] = "按照工号修改个人信息";
history(name, buf);
}
else if (flag == 1)
{
char buf[] = "按照姓名修改个人信息";
history(name, buf);
}
return 0;
}
int do_See_User(struct sockaddr_in cin, Msg rcvbuf, int newfd, sqlite3 *db, char *name)
{
char **pres = NULL;
int row, column;
char *errmsg = NULL;
char sql[128] = "";
sprintf(sql, "%s'%s'", "select *from MSG where name=", rcvbuf.st.account);
printf("%d:sql=%s\n", __LINE__, sql);
if (SQLITE_OK != sqlite3_get_table(db, sql, &pres, &row, &column, &errmsg))
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg);
return -1;
}
printf("%s\n", pres[9]);
printf("%s\n", pres[10]);
printf("%s\n", pres[11]);
printf("%s\n", pres[12]);
printf("%s\n", pres[13]);
printf("%s\n", pres[14]);
printf("%s\n", pres[15]);
printf("%s\n", pres[16]);
printf("%s\n", pres[17]);
bzero(&rcvbuf, sizeof(rcvbuf));
strcpy(rcvbuf.st.account, pres[9]);
rcvbuf.st.workno = atoi(pres[10]);
rcvbuf.st.age = atoi(pres[11]);
strcpy(rcvbuf.st.address, pres[12]);
rcvbuf.st.phone = atoi(pres[13]);
strcpy(rcvbuf.st.posi, pres[14]);
strcpy(rcvbuf.st.data, pres[15]);
rcvbuf.st.laver = atoi(pres[16]);
rcvbuf.st.salary = atoi(pres[17]);
printf("姓名:%s\n", rcvbuf.st.account);
printf("年龄:%d\n", rcvbuf.st.age);
printf("地址:%s\n", rcvbuf.st.address);
printf("电话:%d\n", rcvbuf.st.phone);
printf("职位:%s\n", rcvbuf.st.posi);
printf("日期:%s\n", rcvbuf.st.data);
printf("级别:%d\n", rcvbuf.st.laver);
printf("薪资:%d\n", rcvbuf.st.salary);
if (send(newfd, &rcvbuf, sizeof(rcvbuf), 0) < 0)
{
ERR_MSG("send");
return -1;
}
char buf[] = "普通用户查询个人信息";
history(name, buf);
return 0;
}
int do_Modi_User(struct sockaddr_in cin, Msg rcvbuf, int newfd, sqlite3 *db, char *name)
{
char sql[600] = "";
sprintf(sql, "%s%d,%s%d,%s'%s',%s%d,%s'%s',%s'%s',%s%d,%s%d %s'%s'", "update MSG set workno=", rcvbuf.st.workno, "age=", rcvbuf.st.age, "address=", rcvbuf.st.address, "phone=", rcvbuf.st.phone, "posi=", rcvbuf.st.posi, "data=", rcvbuf.st.data, "laver=", rcvbuf.st.laver, "salary=", rcvbuf.st.salary, "where name=", rcvbuf.st.account);
char *errmsg = NULL;
printf("sql=%s\n", sql);
if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
fprintf(stderr, "%d sqlite3_exec fail %s\n",
__LINE__, errmsg);
return -1;
}
char buf[] = "用户修改个人信息";
history(name, buf);
return 0;
}