client.h
#ifndef __SERVER_H__
#define __SERVER_H__
#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>
#define SER_IP "192.168.250.100"
#define SER_PORT 8888
#define NAMELEN 20
#define phoneLEN 20
#define DATALEN 30
//打印错误信息
#define ERR_MSG(msg) do{
\
fprintf(stderr, "__%d__:", __LINE__);\
perror(msg);\
}while(0)
//员工信息结构体
typedef struct staff_info{
int no; //员工编号
int usertype; //ADMIN 0 USER 1
char name[NAMELEN]; //姓名
char passwd[8]; //密码
int age; // 年龄
char phone[phoneLEN];//电话
char addr[DATALEN]; // 地址
char work[DATALEN]; //职位
char date[DATALEN]; //入职年月
int level; // 等级
double salary ; // 工资
}staff_info_t;
//通信结构体
typedef struct {
int msgtype; //请求的消息类型
int usertype; //ADMIN 1 USER 2
char username[NAMELEN]; //姓名
char passwd[8]; //登陆密码
char recvmsg[DATALEN]; //通信的消息
int flags; //标志位
staff_info_t info; //员工信息
}MSG;
#endif
client.c
#include "client.h"
//全局变量
int sfd;
ssize_t res = 0;
MSG info;
int send_mesg(void)
{
if(send(sfd, &info, sizeof(info), 0) < 0)
{
ERR_MSG("sen");
return -1;
}
printf("send success\n");
}
//客户端连接初始化
int client_connect_init(void)
{
//创建流式套接字
int sfd = socket(AF_INET, SOCK_STREAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//绑定客户端自身的地址信息结构体,到对应的套接字上;----非必须绑定
//填充服务器的地址信息结构体,因为connect函数中要通过地址信息结构体连接服务器;
struct sockaddr_in sin;
sin.sin_family = AF_INET; //必须填AF_INET;
sin.sin_port = htons(SER_PORT); //服务器绑定的端口
sin.sin_addr.s_addr = inet_addr(SER_IP); //服务器绑定的IP
//连接服务器
if(connect(sfd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("connect");
return -1;
}
printf("connect success\n");
return sfd;
}
//管理员登录
int admin_login(void)
{
int code = 0;
bzero(&info, sizeof(info));
info.msgtype = 1;
info.usertype = 0;
//发送
printf("请输入用户名>>>");
scanf("%s",info.username);
while (getchar() != '\n');
printf("请输入密码>>>");
scanf("%s",info.passwd);
while (getchar() != '\n');
if(send(sfd, &info, sizeof(info), 0) < 0)
{
ERR_MSG("sen");
return -1;
}
printf("send success\n");
if(recv(sfd,&code,sizeof(code),0) < 0)
{
printf("%d:recv\n", __LINE__);
return -1;
}
if(code == 200)
{
printf("管理员登录成功\n");
//开启下一个菜单选项
return 200;
}
else
{
printf("管理员登录失败\n");
}
}
//用户登录
int user_login(void)
{
int code = 0;
bzero(&info, sizeof(info));
info.msgtype = 2;
info.usertype = 1;
//发送
printf("请输入用户名>>>");
scanf("%s",info.username);
while (getchar() != '\n');
printf("请输入密码>>>");
scanf("%s",info.passwd);
while (getchar() != '\n');
if(send(sfd, &info, sizeof(info), 0) < 0)
{
ERR_MSG("sen");
return -1;
}
printf("send success\n");
if(recv(sfd,&code,sizeof(code),0) < 0)
{
printf("%d:recv\n", __LINE__);
return -1;
}
if(code == 200)
{
printf("用户登录成功\n");
return 200;
}
else
{
printf("用户登录失败\n");
}
}
//查询函数
int search_func()
{
int ret,row;
int code = 0;
char str[512] = {
0};
ret = recv(sfd,&row,sizeof(row),0);
if (ret < 0)
{
printf("%d:recv\n", __LINE__);
return -1;
}
printf("************************************************查询结果*******************************************************\n");
printf("//员工编号 //类型 //姓名 //密码 // 年龄 //电话 // 地址 //职位 //入职年月 // 等级 // 工资\n");
int line, list;
int j = 1;
for(line=1; line<row+1; line++)
{
bzero(str, sizeof(str));
ret = recv(sfd,str,sizeof(str),0);
if (ret < 0)
{
printf("%d:recv\n", __LINE__);
return -1;
}
puts(str);
putchar(10);
}
printf("*************************************************************************************************************\n");
ret = recv(sfd,&code,sizeof(code),0);
if (ret < 0)
{
printf("%d:recv\n", __LINE__);
return -1;
}
if(code == 200)
{
printf("查找成功\n");
//开启下一个菜单选项
return 200;
}
else
{
printf("查找失败\n");
}
return 0;
}
//管理员通过名字查询
int admin_search_by_name(void)
{
info.msgtype = 3;
printf("请输入需要查询的的员工名字");
scanf("%s",info.info.name);
send_mesg();
search_func();
return 0;
}
//管理员查询所有
int admin_search_all(void)
{
info.msgtype = 4;
send_mesg();
search_func();
return 0;
}
int search_func_record()
{
int ret,row;
int code = 0;
char str[512] = {
0};
ret = recv(sfd,&row,sizeof(row),0);
if (ret < 0)
{
printf("%d:recv\n", __LINE__);
return -1;
}
printf("************************************************查询结果*******************************************************\n");
printf("//日期 //名字 //行为 \n");
int line, list;
int j = 1;
for(line=1; line<row+1; line++)
{
bzero(str, sizeof(str));
ret = recv(sfd,str,sizeof(str),0);
if (ret < 0)
{
printf("%d:recv\n", __LINE__);
return -1;
}
puts(str);
putchar(10);
}
printf("*************************************************************************************************************\n");
ret = recv(sfd,&code,sizeof(code),0);
if (ret < 0)
{
printf("%d:recv\n", __LINE__);
return -1;
}
if(code == 200)
{
printf("查找成功\n");
//开启下一个菜单选项
return 200;
}
else
{
printf("查找失败\n");
}
return 0;
}
//管理员查询历史记录
int admin_search_record(void)
{
info.msgtype = 5;
send_mesg();
search_func_record();
return 0;
}
//管理员添加用户
int admin_add_user(void)
{
int code = 0;
info.msgtype = 6;
printf("请输入相关信息:");
scanf("%d %d %s %s %d %s %s %s %s %d %lf",\
&info.info.no,&info.info.usertype,info.info.name,info.info.passwd,&info.info.age,\
info.info.phone,info.info.addr,info.info.work,info.info.date,&info.info.level,\
&info.info.salary);
send_mesg();
if(recv(sfd,&code,sizeof(code),0) < 0)
{
printf("%d:recv\n", __LINE__);
return -1;
}
if(code == 200)
printf("添加成功\n");
return 0;
}
//管理员删除用户
int admin_del_user(void)
{
int code = 0;
info.msgtype = 7;
printf("请输入需要删除的用户工号:");
scanf("%d",&info.info.no);
send_mesg();
if(recv(sfd,&code,sizeof(code),0) < 0)
{
printf("%d:recv\n", __LINE__);
return -1;
}
if(code == 200)
printf("删除成功\n");
return 0;
}
//管理员根据工号修改名字
int admin_update_user_name(void)
{
int code = 0;
info.msgtype = 8;
printf("请输入需要修改的员工的工号和名字:");
scanf("%d %s",&info.info.no,info.info.name);
send_mesg();
if(recv(sfd,&code,sizeof(code),0) < 0)
{
printf("%d:recv\n", __LINE__);
return -1;
}
if(code == 200)
printf("修改成功\n");
return 0;
}
//管理员根据工号修改年龄
int admin_update_user_age(void)
{
int code = 0;
info.msgtype = 9;
printf("请输入需要修改的员工的工号和年龄:");
scanf("%d %d",&info.info.no,&info.info.age);
send_mesg();
if(recv(sfd,&code,sizeof(code),0) < 0)
{
printf("%d:recv\n", __LINE__);
return -1;