cli_main.c
#include "./fun.h"
#include "./tcp_cli.h"
//printf("%s %d\n",__FILE__,__LINE__);//查找错误
int main(int argc, const char *argv[])
{
char c;
//1.连接服务器
int cfd = tcp_init();
//2.打印一级菜单
while(1)
{
user_print1();
c = chose_fun1();
if(0 == chose1(c, cfd))
{
break;
}
}
return 0;
}
fun.c
#include "./fun.h"
//printf("%s %d\n",__FILE__,__LINE__);//查找错误
//1.一级菜单打印
void user_print1(void)
{
//system("clear");
printf("===========电子词典==========\n");
printf("| 1.注册 |\n");
printf("| 2.登录 |\n");
printf("| 3.退出 |\n");
printf("=============================\n");
}
//2.功能选择
char chose_fun1()
{
printf("请输入需要选择的功能:");
char c = getchar();
while(getchar() != 10);
return c;
}
//3.一级菜单选择处理
int chose1(char c, int cfd)
{
switch(c)
{
case '1':
{
//注册
user_reg(cfd);
break;
}
case '2':
{
//选择登录函数
loin(cfd);
break;
}
case '3':
{
//退出
quit_sys(cfd);
return 0;
}
default:
{
printf("选择错误,请重新输入\n");
}
}
printf("输入任何字符返回主页面\n");
while(getchar() != 10);
}
//4.注册
int user_reg(int cfd)
{
ssize_t res;
char str[MAX] = "";
strcpy(str, "reg");
res = msg_send(cfd,str,sizeof(str)); //发送注册标志
printf("请输入用户名:");
scanf("%s", str);
res = msg_send(cfd,str,sizeof(str)); //发送用户名
printf("请输入密码:");
scanf("%s", str);
res = msg_send(cfd,str,sizeof(str)); //发送密码
msg_rec(cfd,str,sizeof(str)); //接收返回信息
if(0 == strcmp(str,"user is exit!")) //处理信息
{
printf("用户已存在,不可以重复注册\n");
return 0;
}
else if(0 == strcmp(str,"user reg success!"))
{
printf("用户注册成功\n");
return 0;
}
}
//5.登录
int user_loin(int cfd)
{
ssize_t res;
char str[MAX] = "";
strcpy(str,"loin");
res = msg_send(cfd,str,sizeof(str)); //
printf("请输入用户名:");
scanf("%s", str);
res = msg_send(cfd,str,sizeof(str));
printf("请输入密码:");
scanf("%s", str);
res = msg_send(cfd,str,sizeof(str));
msg_rec(cfd,str,sizeof(str));
if(0 == strcmp(str,"user is loin!"))
{
printf("用户已登录,不可以重复登录\n");
return 0;
}
else if(0 == strcmp(str,"user loin success!"))
{
printf("用户登录成功\n");
return 200;
}
else if(0 == strcmp(str,"user or passd error!"))
{
printf("用户名或密码错误或该用户不存在\n");
return 0;
}
}
//6.退出
int quit_sys(int cfd)
{
char *str = (char*)malloc(40);
strcpy(str,"quit");
msg_send(cfd,str,40);
//msg_rec(cfd,str,40);
free(str);
socket_close(cfd);
}
//7.二级菜单打印
void user_print2(void)
{
//system("clear");
printf("===========电子词典==========\n");
printf("| 1.查询单词 |\n");
printf("| 2.历史记录查询 |\n");
printf("| 3.返回上一级 |\n");
printf("=============================\n");
}
//8.二级菜单选择处理
int chose2(char c, int cfd)
{
switch(c)
{
case '1':
{
printf("选择查询单词功能\n");
danci_look(cfd);
break;
}
case '2':
{
printf("选择查询历史记录功能\n");
record_look(cfd);
break;
}
case '3':
{
printf("选择返回上一级的功能\n");
goto_print1(cfd);
return 100;
}
default:
{
printf("选择错误,请重新输入\n");
}
}
printf("输入任何字符返回菜单页面\n");
while(getchar() != 10);
}
//9.查询单词
int danci_look(int cfd)
{
ssize_t res;
char str[MAX] = "";
char chinese[MAX] = "";
strcpy(str,"danci");
res = msg_send(cfd,str,sizeof(str));
printf("请输入需要查询的单词:");
scanf("%s", str);
res = msg_send(cfd,str,sizeof(str));
while(1)
{
msg_rec(cfd, chinese, sizeof(chinese));
if(0 == strcmp(chinese,"123456"))
break;
else if(0 == strcmp(chinese,"123456789"))
{
printf("没有查找到该单词\n");
break;
}
printf("%s:%s\n", str, chinese);
}
}
//返回上一级
int goto_print1(int cfd)
{
ssize_t res;
char str[MAX] = "";
strcpy(str,"gotoprint1");
res = msg_send(cfd,str,sizeof(str));
}
//历史记录查询
int record_look(int cfd)
{
ssize_t res;
char str[MAX] = "";
strcpy(str,"recordlook");
res = msg_send(cfd,str,sizeof(str));
while(1)
{
msg_rec(cfd, str, sizeof(str));
if(0 == strcmp(str,"123456"))
break;
else if(0 == strcmp(str,"123456789"))
{
printf("没有查找到该用户的历史记录\n");
break;
}
printf("%s", str);
}
}
//选择登陆函数
int loin(int cfd)
{
if(user_loin(cfd))
{
char e;
while(getchar() != 10);
while(1)
{
//二级菜单打印
user_print2();
e = chose_fun1();
//功能选择
if(100 == chose2(e, cfd))
break;
}
}
}
tcp_cli.c
#include "./tcp_cli.h"
//printf("%s %d\n",__FILE__,__LINE__);//查找错误
//1.初始化
int tcp_init()
{
int cfd;
struct sockaddr_in client_addr,server_addr;
socklen_t server_addr_size;
//1.创建流式套接字
cfd = socket(AF_INET, SOCK_STREAM, 0);
if (cfd < 0)
{
ERROR("socket");
return -1;
}
memset(&server_addr, 0, sizeof(struct sockaddr_in));
//填充服务器地址信息结构体
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT); //端口的网络字节序
server_addr.sin_addr.s_addr = inet_addr(SERVER_IP); //IP的网络字节序
//3.连接服务器
server_addr_size = sizeof(struct sockaddr_in);
if(connect(cfd, (struct sockaddr*)&server_addr, server_addr_size) < 0)
{
ERROR("connect");
return -1;
}
printf("服务器连接成功\n");
return cfd;
}
//2.发送信息
ssize_t msg_send(int cfd, void* buf,int size)
{
ssize_t res = 0;
if((res = send(cfd, (struct msga*)buf, size, 0)) < 0)
{
ERROR("send");
return -1;
}
return res;
}
//3.接收信息
ssize_t msg_rec(int cfd, void* buf, int size)
{
ssize_t res = 0;
res = recv(cfd, buf, size, 0);
if(res < 0)
{
ERROR("recv");
return -1;
}
return res;
}
//4.关闭
void socket_close(int cfd)
{
close(cfd);
}
Makefile
all:ser
ser: fun.o cli_main.o tcp_cli.o
gcc $^ -o cli
%.o:%.c
gcc -c $< -o $@
clean:
rm *.o
tcp_cli.h
#ifndef __TCP_CLI_H__
#define __TCP_CLI_H__
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#define SERVER_IP "192.168.8.101" //服务器 IP
#define SERVER_PORT 8888 //端口号
#define MAX 128
#define ERROR(msg) do{\
fprintf(stderr, "__%d__", __LINE__);\
perror(msg);\
}while(0)
struct userinfo
{
char* user;
char* passd;
char* online;
};
struct msga
{
struct userinfo a;
char* str;
};
//1.初始化
int tcp_init();
//2.发送信息
ssize_t msg_send(int cfd, void* buf,int size);
//3.接受信息
ssize_t msg_rec(int cfd, void* buf, int size);
//4.关闭
void socket_close(int cfd);
#endif
fun.h
#ifndef __FUN_H__
#define __FUN_H__
#include <stdio.h>
#include <stdlib.h>
#include "./tcp_cli.h"
//printf("%s %d\n",__FILE__,__LINE__);//调试找错误代码
//1.一级菜单打印
void user_print1(void);
//2.功能选择
char chose_fun1(void);
//3.一级菜单处理
int chose1(char c, int cfd);
//4.注册
int user_reg(int cfd);
//5.登录
int user_loin(int cfd);
//6.退出
int quit_sys(int cfd);
//7.二级菜单打印
void user_print2(void);
//8.二级菜单选择处理
int chose2(char c, int cfd);
//9.查询单词
int danci_look(int cfd);
//10.返回一级菜单
int goto_print1(int cfd);
//11.查询历史记录功能
int record_look(int cfd);
//12.选择登陆函数
int loin(int cfd);
#endif