主函数:
#include "server.h"
int main(int argc, char const *argv[])
{
int acceptfd;
pthread_t tid;
int listenfd, connectfd, nybts;
struct sockaddr_in server_addr;
struct sockaddr_in client_addr;
socklen_t socklen = sizeof(client_addr);
if (argc < 3)
{
printf("Usage : %s \n" , argv[0]);
exit(-1);
}
if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
perror("fail to socket");
exit(-1);
}
bzero(&server_addr, sizeof(server_addr));
server_addr.sin_family = PF_INET;
server_addr.sin_addr.s_addr = inet_addr(argv[1]);
server_addr.sin_port = htons(atoi(argv[2]));
if (bind(listenfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0)
{
perror("fail to bind");
exit(-1);
}
if (listen(listenfd, 5) < 0)
{
perror("fail to listen");
exit(-1);
}
while (1)
{
//阻塞等待接收客户端连接
if (-1 == (acceptfd = accept(listenfd, (struct sockaddr *)&client_addr, &socklen)))
PERROR_ERROR("accept error");
pthread_create(&tid, NULL, do_select, &acceptfd);
// pthread_join(tid,NULL); ----阻塞等待回收子线程的资源,会导致客户端同一时刻只能处理一个线程的信息
pthread_detach(tid);
}
//关闭
close(listenfd);
pthread_exit(NULL); //回收主线程资源
return 0;
}
线程执行函数:
#include "server.h"
//打开数据库,创建表
sqlite3 *proc_init1()
{
//打开数据库文件
sqlite3 *sql_db = NULL;
int ret = sqlite3_open(DATABASE, &sql_db);
if (ret != SQLITE_OK)
{
printf("errcode[%d] errmsg[%s]\n", ret, sqlite3_errmsg(sql_db));
exit(-1);
}
//建表
// IF NOT EXISTS 表不存在则创建 表存在则直接使用,而不是报错
//引号里面sql语句后面不用加分号
char *errstr = NULL;
char buff[128] = "CREATE TABLE IF NOT EXISTS employee(id INT PRIMARY KEY,passward CHAR, leve INT, name CHAR, salary CHAR, office CHAR,status INT)";
ret = sqlite3_exec(sql_db, buff, NULL, NULL, &errstr);
if (ret != SQLITE_OK)
{
printf("errcode[%d] errmsg[%s]\n", ret, errstr);
exit(-1);
}
//释放错误信息的空间 防止内存泄漏
sqlite3_free(errstr);
return sql_db;
}
//线程处理函数
void *do_select(void *arg)
{
int nybts = 0;
node_t emp;
int flag = 0;
sqlite3 *db1;
db1 = proc_init1();
memset(&emp, 0, sizeof(emp));
int acceptfd = *(int *)arg;
printf("客户端[%d]连接\n", acceptfd);
while (flag == 0)
{
if (-1 == (nybts = recv(acceptfd, &emp, sizeof(node_t), 0)))
PERROR_ERROR("recv error");
switch (emp.select)
{
case 1:
//注册
do_register(acceptfd, db1, &emp);
break;
case 2:
//登录
do_login(acceptfd, db1, &emp);
break;
case 3:
//退出
flag = 1;
printf("客户端[%d]断开连接\n", acceptfd);
close(acceptfd);
break;
}
}
pthread_exit(NULL);
}
//注册函数---管理员可以执行注册操作,普通员工不能进行注册
void do_register(int acceptfd, sqlite3 *db1, node_t *emp)
{
char lock[32] = "aabbcc";
//接收管理员密码,并比对
if (-1 == recv(acceptfd, emp, sizeof(node_t), 0))
PERROR_ERROR("recv error")
if (0 == strcmp(emp->text, lock))
{
printf("输入密码正确,进入注册界面\n");
strcat(emp->text, "--OK");
if (-1 == send(acceptfd, emp, sizeof(node_t), 0))
PERROR_ERROR("send error");
//接收注册员工的信息
if (-1 == recv(acceptfd, emp, sizeof(node_t), 0))
PERROR_ERROR("recv error")
//将员工信息插入数据库中
do_insert(emp, db1, acceptfd);
}
else
{
strcat(emp->text, "--FALSE");
if (-1 == send(acceptfd, emp, sizeof(node_t), 0))
PERROR_ERROR("send error");
return;
}
return;
}
//将注册的员工信息插入到数据库中
void do_insert(node_t *emp, sqlite3 *db1, int acceptfd)
{
char sq_buff[512];
printf("进入到插表函数\n");
emp->status = 0;
//因为id是主键,所以不用查询表中是否已经有了该id
sprintf(sq_buff, "INSERT INTO employee VALUES(%d, '%s', %d , '%s' , '%s' , '%s',%d)", emp->id, emp->passward, emp->leve, emp->name, emp->office, emp->salary, emp->status);
if (sqlite3_exec(db1, sq_buff, NULL, NULL, NULL) != SQLITE_OK)
{
strcpy(emp->text, "用户已经存在");
if (-1 == send(acceptfd, emp, sizeof(node_t), 0))
PERROR_ERROR("send error");
}
else
{
//插入成功后,按降序排列
strcpy(emp->text, "用户注册成功");
if (-1 == send(acceptfd, emp, sizeof(node_t), 0))
PERROR_ERROR("send error");
}
return;
}
//登录函数
void do_login(int acceptfd, sqlite3 *db1, node_t *emp)
{
char sq_buff[512];
char sql_buff[256] = {0};
char *errmsg, **result;
int nrow, ncolumn;
//接收账户和密码
if (-1 == recv(acceptfd, emp, sizeof(node_t), 0))
PERROR_ERROR("recv error")
//查询数据库中是否存在
sprintf(sql_buff, "SELECT * FROM employee WHERE id=%d", emp->id);
if (sqlite3_get_table(db1, sql_buff, &result, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
PERROR_ERROR("get table error");
if (nrow == 0)
{
//如果nrow==0,表示sqlite3_get_table函数执行后,没有查询到该id;
strcpy(emp->text, "用户不存在");
if (-1 == send(acceptfd, emp, sizeof(node_t), 0))
PERROR_ERROR("send error");
return;
}
//获取该员工所有信息
get_employee_info(db1, emp);
if (emp->status == 1)
{
strcpy(emp->text, "用户已经在线,不能登录");
if (-1 == send(acceptfd, emp, sizeof(node_t), 0))
PERROR_ERROR("send error");
}
else
{
emp->status = 1;
sprintf(sq_buff, "UPDATE employee SET status=%d WHERE id=%d", emp->status, emp->id);
if (sqlite3_exec(db1, sq_buff, NULL, NULL, NULL) != SQLITE_OK)
PERROR_ERROR("登录失败");
strcpy(emp->text, "用户登录成功");
//发送给客户端
if (-1 == send(acceptfd, emp, sizeof(node_t), 0))
PERROR_ERROR("send error");
//接收用户请求
do_login_select(acceptfd, emp, db1);
}
}
//通过id获取员工信息
void get_employee_info(sqlite3 *db1, node_t *emp)
{
char *errmsg, **result;
int nrow, ncolumn;
char sql_buff[256] = {0};
sprintf(sql_buff, "SELECT * FROM employee WHERE id=%d", emp->id);
if (sqlite3_get_table(db1, sql_buff, &result, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
{
perror("get table error");
return;
}
int i = 0;
for (i = 0; i < ncolumn; i++)
{
printf("%10s", result[i]);
}
printf("\n");
int index = i;
//打印字段的值
int j = 0;
//外层循环控制行数
for (i = 0; i < nrow; i++)
{
//内层循环控制列数
for (j = 0; j < ncolumn; j++)
{
printf("%10s", result[index++]);
}
printf("\n");
}
while (ncolumn > 0)
{
switch (ncolumn)
{
case 1:
emp->id = atoi(result[ncolumn + 6]);
break;
case 2:
strcpy(emp->passward, result[ncolumn + 6]);
break;
case 3:
emp->leve = atoi(result[ncolumn + 6]);
break;
case 4:
strcpy(emp->name, result[ncolumn + 6]);
break;
case 5:
strcpy(emp->salary, result[ncolumn + 6]);
break;
case 6:
strcpy(emp->office, result[ncolumn + 6]);
break;
case 7:
emp->status = atoi(result[ncolumn + 6]);
break;
}
ncolumn--;
}
}
//登录后执行增删改查操作
void do_login_select(int acceptfd, node_t *emp, sqlite3 *db1)
{
char sq_buff[512] = {0};
int flag = 0;
node_t emp1;
while (flag == 0)
{
if (emp->status == 1)
{
//接收客户端请求
if (-1 == recv(acceptfd, emp, sizeof(node_t), 0))
PERROR_ERROR("recv error");
if (emp->leve == 1)
{
switch (emp->select)
{
case 1:
//增加
do_add_emp(acceptfd, &emp1, db1);
break;
case 2:
//删除
if (-1 == recv(acceptfd, &emp1, sizeof(node_t), 0))
PERROR_ERROR("recv error");
sprintf(sq_buff, "DELETE FROM employee WHERE id=%d", emp1.id);
if (sqlite3_exec(db1, sq_buff, NULL, NULL, NULL) != SQLITE_OK)
PERROR_ERROR("删除失败");
break;
case 3:
//查找
if (-1 == recv(acceptfd, &emp1, sizeof(node_t), 0))
PERROR_ERROR("recv error");
printf("select===%d\n", emp1.select);
if (emp1.select == 1)
{
get_msg_sys2(acceptfd, db1);
}
if (emp1.select == 2)
{
if (-1 == recv(acceptfd, &emp1, sizeof(node_t), 0))
PERROR_ERROR("recv error");
get_employee_info(db1, &emp1);
if (-1 == send(acceptfd, &emp1, sizeof(node_t), 0))
PERROR_ERROR("recv error");
}
break;
case 4:
//修改
do_change_info(acceptfd, db1, &emp1);
break;
case 5:
//退出
flag = 1;
quit_emp(db1, emp);
break;
}
}
else
{
switch (emp->select)
{
case 1:
//查询
break;
case 2:
//修改
do_change_info(acceptfd, db1, emp);
break;
case 3:
flag = 1;
quit_emp(db1, emp);
break;
}
}
}
}
}
//---------------从数据表中获取数据,管理员身份查询表---------------------
void get_msg_sys2(int acceptfd, sqlite3 *db1)
{
table_t table;
char buff[1024] = {0};
char sql_buff[256] = {0};
char *errmsg, **result;
int nrow, ncolumn, index, i, j;
sprintf(sql_buff, "SELECT * FROM employee");
if (sqlite3_get_table(db1, sql_buff, &result, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
{
perror("get table error");
return;
}
index = ncolumn;
table.ncolumn = ncolumn;
table.nrow = nrow;
if (-1 == send(acceptfd, &table, sizeof(table_t), 0))
PERROR_ERROR("send error");
//外层循环控制行数
for (i = 0; i < nrow; i++)
{
//内层循环控制列数
for (j = 0; j < ncolumn; j++)
{
strcat(buff, result[index]);
if (-1 == (send(acceptfd, buff, sizeof(buff), 0)))
PERROR_ERROR("send error");
memset(buff, 0, sizeof(buff));
index++;
}
printf("\n");
}
}
//修改员工信息
void do_change_info(int acceptfd, sqlite3 *db1, node_t *emp)
{
char sq_buff[512] = {0};
if (-1 == recv(acceptfd, emp, sizeof(node_t), 0))
PERROR_ERROR("recv error");
if (emp->select == 1)
{
sprintf(sq_buff, "UPDATE employee SET name='%s' WHERE id=%d", emp->name, emp->id);
if (sqlite3_exec(db1, sq_buff, NULL, NULL, NULL) != SQLITE_OK)
PERROR_ERROR("更新姓名失败");
get_employee_info(db1, emp);
}
if (emp->select == 2)
{
sprintf(sq_buff, "UPDATE employee SET passward='%s' WHERE id=%d", emp->passward, emp->id);
if (sqlite3_exec(db1, sq_buff, NULL, NULL, NULL) != SQLITE_OK)
PERROR_ERROR("更新密码失败");
get_employee_info(db1, emp);
}
if (emp->select == 3)
{
sprintf(sq_buff, "UPDATE employee SET office='%s' WHERE id=%d", emp->office, emp->id);
if (sqlite3_exec(db1, sq_buff, NULL, NULL, NULL) != SQLITE_OK)
PERROR_ERROR("更新职位失败");
get_employee_info(db1, emp);
}
if (emp->select == 4)
{
sprintf(sq_buff, "UPDATE employee SET salary='%s' WHERE id=%d", emp->salary, emp->id);
if (sqlite3_exec(db1, sq_buff, NULL, NULL, NULL) != SQLITE_OK)
PERROR_ERROR("更新工资失败");
get_employee_info(db1, emp);
}
if (emp->select == 5)
{
sprintf(sq_buff, "UPDATE employee SET leve=%d WHERE id=%d", emp->leve, emp->id);
if (sqlite3_exec(db1, sq_buff, NULL, NULL, NULL) != SQLITE_OK)
PERROR_ERROR("更新权限失败");
get_employee_info(db1, emp);
}
}
//添加成员
void do_add_emp(int acceptfd, node_t *emp, sqlite3 *db1)
{
if (-1 == recv(acceptfd, emp, sizeof(node_t), 0))
PERROR_ERROR("recv error")
do_insert(emp, db1, acceptfd);
}
//客户端退出
void quit_emp(sqlite3 *db1, node_t *emp)
{
char sq_buff[512] = {0};
emp->status = 0;
sprintf(sq_buff, "UPDATE employee SET status=%d WHERE id=%d", emp->status, emp->id);
if (sqlite3_exec(db1, sq_buff, NULL, NULL, NULL) != SQLITE_OK)
{
PERROR_ERROR("更新密码失败");
}
else
{
get_employee_info(db1, emp);
}
}
.h文件:
#ifndef __SERVER2_H__
#define __SERVER2_H__
#include
#include
#include
#include
#include "sqlite3.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DATABASE "my.db"
#define PERROR_ERROR(str) \
{ \
perror(str); \
exit(-1); \
}
typedef struct __EMPL
{
int id; //员工号
char passward[128]; //密码
int leve; //级别(管理员=1,普通员工=0)
char name[32]; //姓名
char salary[32]; //薪资
char office[32]; //职务
char text[32]; //收发确认
int select; // 1:注册 2:登录 3:退出
int status; //状态,1--在线,0--下线
}node_t;
//数据库表的大小
typedef struct __TABLE
{
int nrow;
int ncolumn;
}table_t;
sqlite3 *proc_init1();
void *do_select(void *arg);
void do_register(int acceptfd, sqlite3 *db1, node_t *emp);
void do_insert(node_t *emp, sqlite3 *db1,int acceptfd);
void do_login(int acceptfd,sqlite3* db1,node_t* emp);
void do_login_select(int acceptfd, node_t *emp, sqlite3 *db1);
void get_employee_info(sqlite3 *db1, node_t *emp);
void do_change_info(int acceptfd, sqlite3 *db1, node_t *emp);
void do_add_emp(int acceptfd, node_t *emp, sqlite3 *db1);
void get_msg_sys2(int acceptfd, sqlite3 *db1);
void quit_emp(sqlite3 *db1, node_t *emp);
#endif
client.c文件
#include "client.h"
int main(int argc, const char *argv[])
{
int flag=0;
node_t emp;
memset(&emp, 0, sizeof(node_t));
if (3 != argc)
{
printf("Usage : %s \n" , argv[0]);
exit(-1);
}
// 1:创建套接字
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
// 2:填充网络信息结构体
struct sockaddr_in client_addr;
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(atoi(argv[2]));
client_addr.sin_addr.s_addr = inet_addr(argv[1]);
socklen_t socklen = sizeof(client_addr);
// 3:建立连接
if (-1 == (connect(sockfd, (struct sockaddr *)&client_addr, socklen)))
{
perror("connect error");
exit(-1);
}
while (flag==0)
{
printf("进入选择函数:1--注册,2--登录,3--退出\n");
scanf("%d", &emp.select);
if (-1 == send(sockfd, &emp, sizeof(emp), 0))
{
perror("send error");
exit(-1);
}
switch (emp.select)
{
case 1:
//注册
do_register(sockfd);
break;
case 2:
//登录
do_login(sockfd);
break;
case 3:
//退出
flag=1;
break;
}
}
// 5:关闭套接字
close(sockfd);
return 0;
}
client2.c:客户端执行的函数
#include "client.h"
void do_register(int sockfd)
{
node_t emp;
memset(&emp, 0, sizeof(node_t));
emp.select = 1;
printf("请输入管理员密码:");
scanf("%s", emp.text);
if (-1 == send(sockfd, &emp, sizeof(node_t), 0))
PERROR_ERROR("send error");
//接收返回的比对信息
if (-1 == recv(sockfd, &emp, sizeof(node_t), 0))
PERROR_ERROR("recv error");
if (0 == strcmp(emp.text, "aabbcc--OK"))
{
printf("管理员密码正确,请输入员工信息\n");
printf("姓名:");
scanf("%s", emp.name);
printf("账号:");
scanf("%d", &emp.id);
printf("密码:");
scanf("%s", emp.passward);
printf("职务:");
scanf("%s", emp.office);
printf("工资:");
scanf("%s", emp.salary);
printf("权限:");
scanf("%d", &emp.leve);
printf("姓名:%s,账号:%d,密码:%s,职位:%s,工资:%s,权限:%d\n", emp.name, emp.id, emp.passward, emp.office, emp.salary, emp.leve);
if (-1 == send(sockfd, &emp, sizeof(node_t), 0))
PERROR_ERROR("send error");
//接收返回信息
if (-1 == recv(sockfd, &emp, sizeof(node_t), 0))
PERROR_ERROR("recv error");
if (0 == strcmp(emp.text, "用户注册成功"))
{
printf("%s\n", emp.text);
}
else
{
printf("%s\n", emp.text);
return;
}
}
else
{
printf("密码错误\n");
}
return;
}
void do_login(int sockfd)
{
node_t emp;
memset(&emp, 0, sizeof(node_t));
emp.select = 2;
printf("账户:");
scanf("%d", &emp.id);
printf("密码:");
scanf("%s", emp.passward);
if (-1 == send(sockfd, &emp, sizeof(node_t), 0))
PERROR_ERROR("send error");
//接收返回信息
if (-1 == recv(sockfd, &emp, sizeof(node_t), 0))
PERROR_ERROR("recv error");
if (0 == strcmp(emp.text, "用户登录成功"))
{
printf("%s\n", emp.text);
printf("姓名:%s,账号:%d,密码:%s,职位:%s,工资:%s,权限:%d,状态:%d\n", emp.name, emp.id, emp.passward, emp.office, emp.salary, emp.leve, emp.status);
do_login_select(sockfd, &emp);
}
else
{
printf("%s\n", emp.text);
return;
}
return;
}
void do_login_select(int sockfd, node_t *emp)
{
int flag = 0;
node_t emp1;
memset(&emp1, 0, sizeof(node_t));
while (flag == 0)
{
if (emp->leve == 1)
{
printf("请选择要进行的操作:1-增加,2-删除,3-查询,4-修改,5-退出: ");
scanf("%d", &emp->select);
if (-1 == send(sockfd, emp, sizeof(node_t), 0))
PERROR_ERROR("recv error");
switch (emp->select)
{
case 1:
//增加
do_add_emp(sockfd, &emp1);
break;
case 2:
//删除
printf("请输入要删除的员工账号:");
scanf("%d", &emp1.id);
if (-1 == send(sockfd, &emp1, sizeof(node_t), 0))
PERROR_ERROR("recv error");
break;
case 3:
//查询
printf("请选择查找的方式:1--查看全部员工信息,2--根据账户查找: ");
scanf("%d", &emp1.select);
if (-1 == send(sockfd, &emp1, sizeof(node_t), 0))
PERROR_ERROR("send error");
if (emp1.select == 1)
{
sys_check_info(&emp1, sockfd);
}
if (emp1.select == 2)
{
printf("请输入要查找的员工账号:");
scanf("%d", &emp1.id);
if (-1 == send(sockfd, &emp1, sizeof(node_t), 0))
PERROR_ERROR("recv error");
if (-1 == recv(sockfd, &emp1, sizeof(node_t), 0))
PERROR_ERROR("recv error");
printf("姓名:%s,账号:%d,密码:%s,职位:%s,工资:%s,权限:%d,状态:%d\n", emp1.name, emp1.id, emp1.passward, emp1.office, emp1.salary, emp1.leve, emp1.status);
}
break;
case 4:
//修改
do_change_emp_info(&emp1, sockfd);
break;
case 5:
//退出
flag = 1;
break;
}
}
else
{
printf("请选择要进行的操作:1-查询,2-修改,3-退出: ");
scanf("%d", &emp->select);
if (-1 == send(sockfd, emp, sizeof(node_t), 0))
PERROR_ERROR("send error");
switch (emp->select)
{
case 1:
printf("姓名:%s,账号:%d,密码:%s,职位:%s,工资:%s,权限:%d,状态:%d\n", emp->name, emp->id, emp->passward, emp->office, emp->salary, emp->leve, emp->status);
break;
case 2:
//修改
printf("请选择要修改的信息:1--name,2--passward: ");
scanf("%d", &emp->select);
do_change_emp_info(emp, sockfd);
break;
case 3:
//退出
flag = 1;
break;
}
}
}
return;
}
void do_change_emp_info(node_t *emp, int sockfd)
{
if (emp->leve == 1)
{
printf("请输入要修改员工的账号:");
scanf("%d", &emp->id);
printf("请选择要修改的信息:1--name,2--passward,3--职位,4--工资,5--权限: ");
scanf("%d", &emp->select);
switch (emp->select)
{
case 1:
//姓名
printf("请输入修改后的姓名:");
scanf("%s", emp->name);
if (-1 == send(sockfd, emp, sizeof(node_t), 0))
PERROR_ERROR("recv error");
break;
case 2:
//密码
printf("请输入修改后的密码:");
scanf("%s", emp->passward);
if (-1 == send(sockfd, emp, sizeof(node_t), 0))
PERROR_ERROR("recv error");
break;
case 3:
//职位
printf("请输入修改后的职位:");
scanf("%s", emp->office);
if (-1 == send(sockfd, emp, sizeof(node_t), 0))
PERROR_ERROR("recv error");
break;
case 4:
//工资
printf("请输入修改后的工资:");
scanf("%s", emp->salary);
if (-1 == send(sockfd, emp, sizeof(node_t), 0))
PERROR_ERROR("recv error");
break;
case 5:
//权限
printf("请输入修改后的权限:");
scanf("%d", &emp->leve);
if (-1 == send(sockfd, emp, sizeof(node_t), 0))
PERROR_ERROR("recv error");
break;
}
}
if (emp->leve == 0)
{
switch (emp->select)
{
case 1:
//姓名
printf("请输入修改后的姓名:");
scanf("%s", emp->name);
if (-1 == send(sockfd, emp, sizeof(node_t), 0))
PERROR_ERROR("recv error");
break;
case 2:
//密码
printf("请输入修改后的密码:");
scanf("%s", emp->passward);
if (-1 == send(sockfd, emp, sizeof(node_t), 0))
PERROR_ERROR("recv error");
break;
}
}
}
//-------------管理员模式查询所有用户信息----------
void sys_check_info(node_t *emp, int sockfd)
{
int i = 0, j = 0;
table_t table;
char buff[1024] = {0};
char *p = buff;
if (-1 == (recv(sockfd, &table, sizeof(table_t), 0)))
{
perror("recv error");
exit(-1);
}
printf("********************************员工信息*************************\n");
printf("** 账号 密码 权限 姓名 职务 工资 状态**\n");
printf("*****************************************************************\n");
for (i = 0; i < table.nrow; i++)
{
memset(buff, 0, sizeof(buff));
for (j = 0; j < table.ncolumn; j++)
{
if (-1 == (recv(sockfd, buff, sizeof(buff), 0)))
{
perror("recv error");
exit(-1);
}
printf("%10s", buff);
}
printf("\n");
}
}
void do_add_emp(int sockfd, node_t *emp)
{
printf("管理员密码正确,请输入员工信息\n");
printf("姓名:");
scanf("%s", emp->name);
printf("账号:");
scanf("%d", &emp->id);
printf("密码:");
scanf("%s", emp->passward);
printf("职务:");
scanf("%s", emp->office);
printf("工资:");
scanf("%s", emp->salary);
printf("权限:");
scanf("%d", &emp->leve);
if (-1 == send(sockfd, emp, sizeof(node_t), 0))
PERROR_ERROR("send error");
//接收返回信息
if (-1 == recv(sockfd, emp, sizeof(node_t), 0))
PERROR_ERROR("recv error");
if (0 == strcmp(emp->text, "用户注册成功"))
{
printf("%s\n", emp->text);
}
else
{
printf("%s\n", emp->text);
return;
}
}
.h文件
#ifndef __CLIENT_H_
#define __CLIENT_H_
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PERROR_ERROR(str) \
{ \
perror(str); \
exit(-1); \
}
typedef struct __EMPL
{
int id; //员工号
char passward[128]; //密码
int leve; //级别(管理员=1,普通员工=0)
char name[32]; //姓名
char salary[32]; //薪资
char office[32]; //职务
char text[32]; //收发确认
int select; // 1:注册 2:登录 3:退出
int status; //状态,1--在线,0--下线
}node_t;
//数据库表的大小
typedef struct __TABLE
{
int nrow;
int ncolumn;
}table_t;
void do_register(int sockfd);
void do_login(int sockfd);
void do_login_select(int sockfd, node_t *emp);
void do_change_emp_info(node_t *emp, int sockfd);
void do_add_emp(int sockfd, node_t *emp);
void sys_check_info(node_t *emp, int sockfd);
#endif