20230827_电子词典

Ser服务器

#include <head.h>
#include <sqlite3.h>

#define PORT 8888
#define IP "192.168.2.28"

void handler(int sig);
void handler(int sig)
{
	while(waitpid(-1,NULL,WNOHANG)>0);
}


void sign(char *name,char *pass,int newfd);//注册
void login(char *name,char *pass,int newfd);//登录
void quire(int newfd,struct sockaddr_in cin);//查询

int main()
{
	if(signal(17,handler) == SIG_ERR)
	{
		perror("signal->");
		return -1;
	}
	printf("signal success!\n");



	int fd = socket(AF_INET,SOCK_STREAM,0);
	if(fd < 0)
	{
		perror("socket->");
		return -1;
	}
	printf("sockte %d success\n",fd);
	
	int reuse = 1;
	int u1 = setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse));
	if(u1 < 0)
	{
		perror("setsockopt->");
		return -1;
	}
	printf("setsockopt success\n");

	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);
	bind(fd,(struct sockaddr *)&sin,sizeof(sin));

	int k2 = listen(fd,128);
	if(k2 < 0)
	{
		perror("listen->");
		return -1;
	}
	printf("listen success\n");

	struct sockaddr_in cin;
	socklen_t addrlen = sizeof(cin);
	int res = -1;
	int res1 = -1;
	char buf[128] = "";
	char buf1[128] = "";
	while(1)
	{
		int newfd = accept(fd,(struct sockaddr*)&cin,&addrlen);
		if(newfd < 0)
		{
			perror("accept->");
			return -1;
		}
		//printf("客户端 [%s : %d : %d] 已链接!\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);//父进程负责登录
		int cpid = fork();
		if(cpid == 0)//子进程负责消息登录,注册交互
		{
			close(fd);
			while(1)
			{
				res = recv(newfd,buf,sizeof(buf),0);
				if(res < 0)
				{
					perror("recv->");
					return -1;
				}
				else if(res == 0)
				{
					printf("客户端 [%s : %d ] 已下线!\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
					return -1;
				}
				//printf("接收到来自客户端 [%s : %d : %d] 的消息: %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);
				char*p = buf;//类型起始地址
				char*namestart = buf+2;
				char*nameend = buf+2;
				while(1)
				{
					if(*nameend == '-')
					{
						break;
					}
					nameend = nameend + 1;
				}
				char name[128] = "";//用户名-字符数组
				char pass[128] = "";//用户密码-字符数组
				int i = 0,j = 0;
				while(1)
				{
					if(namestart ==  nameend)
					{
						break;
					}
					 name[i]= *namestart;
					 i++;
					 namestart++;
				}
				//printf("用户名=>   %s\n",name);//获取账户名
				namestart = namestart+1;
				while(1)
				{
					if(*namestart == '\0')
					 {
						 break;
					 }
					 pass[j]= *namestart;
					 j++;
					 namestart++;
				}
				//printf("密码=> %s\n",pass);//获取密码
				while(1)
				{
					if(*p == '1')
					{
						//注册流程
						printf("<=====下面为注册流程=====>\n");
						sign(name,pass,newfd);
						break;
					}
					else if(*p == '2')
					{
						//登录流程
						printf("<=====下面为登录流程=====>\n");
						login(name,pass,newfd);
						quire(newfd,cin);//查询
						break;
					}
					else
					{
						//printf("类型错误,请重新输入!\n");
						strcpy(buf1,"输入类型错误");
						int res1 = send(newfd,buf1,sizeof(buf1),0);
						if(res1 < 0)
						{
							perror("send->");
							break;
						}
						printf("send [ %s ] success!\n",buf1);
						break;
					}
				}
			}
			close(newfd);
			exit(0);
		}
		close(newfd);
	}
	close(fd);

	return 0;
}
				
void sign(char *name,char *pass,int newfd)
{
	char buf1[128] = "";
	char buf2[128] = "";
	char s1[128] = "";
	//printf("NAME= %s\n",name);
	//printf("PASS= %s\n",pass);
	FILE *fp = fopen("./name_pass","a+");
	if(fp == NULL)
	{
		perror("open->\n");
		strcpy(buf1,"注册失败!");
		return ;
	}
	while(1)
	{
		if(fscanf(fp,"%s",s1) == EOF)
		{
			//printf("读取完毕无重复\n");
			bzero(buf2,sizeof(buf2));
			strcpy(buf2,"no");
			fclose(fp);
			break;
		}
		if(strcmp(name,s1) == 0)
		{
			strcpy(buf1,"namecf");
			bzero(buf2,sizeof(buf2));
			strcpy(buf2,"cf");
			fclose(fp);
			break;
		}

		if(fscanf(fp,"%s",s1) == EOF)
		{
			//printf("读取完毕无重复\n");
			bzero(buf2,sizeof(buf2));
			strcpy(buf2,"no");
			fclose(fp);
			break;
		}
		continue;
		if(strcmp(name,s1) == 0)
		{
			strcpy(buf1,"passcf");
		}
	}
	if(strcmp("cf",buf2) == 0)
	{
		strcpy(buf1,"namecf");
		//return ;
	}
	else
	{
		FILE *fp = fopen("./name_pass","a+");
		if(fp == NULL)
		{
			perror("open->\n");
		}
		fputs(name,fp);
		fputc(' ',fp);
		fputs(pass,fp);
		fputc('\n',fp);
		strcpy(buf1,"注册成功");
		fclose(fp);
	}
	int res1 = send(newfd,buf1,sizeof(buf1),0);
	if(res1 < 0)
	{
		perror("send->");
		return ;
	}
	printf("send [ %s ] success!\n",buf1);

return ;
}


void login(char *name,char *pass,int newfd)
{
	char buf1[128] = "";
	char buf2[128] = "";
	int nameequal = 0;
	int passequal = 0;
	char s1[128] = "";
	//printf("NAME= %s\n",name);
	//printf("PASS= %s\n",pass);
	FILE *fp = fopen("./name_pass","a+");
	if(fp == NULL)
	{
		perror("open->\n");
		strcpy(buf1,"注册失败!");
		return ;
	}
	while(1)
	{
		if(fscanf(fp,"%s",s1) == EOF)//获取用户名
		{
			printf("账户未注册\n");
			strcpy(buf1,"nameerr");
			fclose(fp);
			break;
		}
		if(strcmp(name,s1) == 0)//对比用户名
		{
			nameequal = 1;//
		}
		if(nameequal == 1)//用户名相等
		{
			if(fscanf(fp,"%s",s1) == EOF)//获取密码
			{
				printf("读取完毕,密码错误\n");
				fclose(fp);
				break;
			}
			if(strcmp(pass,s1) == 0)//对比密码
			{
				printf("登录成功\n");
				strcpy(buf1,"登录成功");
				break;
			}
			else
			{	
				printf("密码错误\n");
				strcpy(buf1,"passerr");
				break;
			}
		}
		strcpy(buf1,"nameerr");
	}
	int res1 = send(newfd,buf1,sizeof(buf1),0);
	if(res1 < 0)
	{
		perror("send->");
		return ;
	}
	//printf("send [ %s ] success!\n",buf1);



	return ;
}

void quire(int newfd,struct sockaddr_in cin)//查询
{
	char buf1[128] = "";
	char buf[128] = "";
	int res = recv(newfd,buf,sizeof(buf),0);
	if(res < 0)
	{
		perror("recv->");
		return ;
	}
	else if(res == 0)
	{
		printf("客户端 [%s : %d ] 已下线!\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
		return ;
	}
	printf("buf= %s\n",buf);
	/*printf("请输入数据库文件名字:");
	scanf("%s",stutxt1);
	getchar();*/
	//strcpy(stutxt1,"stutxt");
	char stutxt1[128] = "";
	strcpy(stutxt1,"stutxt");
	printf("表名为: %s\n",stutxt1);
	//创建数据库
	sqlite3 *db = NULL;
	if(sqlite3_open("./my.db",&db) != SQLITE_OK)
	{
		fprintf(stderr,"库文件打开错误=>[错误消息:%s 错误码:%d] _%d_\n",\
				sqlite3_errmsg(db),sqlite3_errcode(db),__LINE__);
		return ;
	}
	//printf("库文件<my.db>打开成功!\n");

	//创建表格
	char sql[128] = "";
	sprintf(sql,"create table if not exists %s (单词 char,解释 float);",stutxt1);
	char *errmsg = NULL;
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
	{
		fprintf(stderr,"sqlite3_exec:%s %d __%d__\n",errmsg,sqlite3_errcode(db),__LINE__);
		return ;
	}
	printf("库文件表格<%s>创建成功!\n",stutxt1);

	//查询单词
	char sql1[128] = "";
	//sprintf(sql1,"select * from %s",stutxt1);
	//sprintf(sql1,"select name,score from %s",stutxt1);
	//sprintf(sql1,"select *from %s where id=1",stutxt1);
	sprintf(sql1,"select *from %s where 单词 like '%s'",stutxt1,buf);
	//sprintf(sql1,"select *from %s where name like 'MC'",stutxt1);
	char *errmsg1 = NULL;
	char **pres = NULL;
	int row,column;
	if(sqlite3_get_table(db,sql1,&pres,&row,&column,&errmsg1) != SQLITE_OK)
	{
		fprintf(stderr,"sqlite3_exec:%s %d __%d__\n",errmsg1,sqlite3_errcode(db),__LINE__);
		return ;
	}
	printf("buf1=%s\n",buf1);

	for(int i = 0;i<(row+1)*column;i++)
	{
		//printf("[%d]: %s\n",i,pres[i]);
		printf("%s\t",pres[i]);
		if((i+1)%column == 0)
		{
			putchar(10);
		}
		strcpy(buf1,pres[i]);
		//printf("%s\t",buf1);

	}
	printf("数据查询表格<%s>成功!\n",stutxt1);

	int res1 = send(newfd,buf1,sizeof(buf1),0);
	if(res1 < 0)
	{
		perror("send->");
		return ;
	}
	printf("send [ %s ] success!\n",buf1);
	bzero(buf1,sizeof(buf1));
	return ;
}

Cli客户端

#include <head.h>

#define PORT 8888
#define TXT "./Hist"
#define IP "192.168.2.28"

int login(int cfd);//登录
int sign(int cfd);//注册
int inquire(int cfd);//查询
int quire(int cfd);//查询单词
int Historical();//历史记录

int main()
{
	int kp = 0;

	printf("<==================>\n");
	printf("<======1.注册======>\n");
	printf("<======2.登录======>\n");
	printf("<======3.退出======>\n");
	printf("<==================>\n");

	while(1)
	{	
		int cfd = socket(AF_INET,SOCK_STREAM,0);
		if(cfd < 0)
		{
			perror("socket->");
			return -1;
		}
	
		printf("请输入请求(1注册/2登录/3退出): ");
		scanf("%d",&kp);

		if(kp == 1)
		{
			sign(cfd);//注册
		}
		else if(kp == 2)
		{
			login(cfd);//登录
			inquire(cfd);//查询
		}
		else if(kp == 3)
		{
			return -1;//退出
		}
		else
		{
			printf("输入错误,请重新输入!\n");
		}
	}

	return 0;
}

int login(int cfd)//登录
{
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);
	socklen_t addrlen = sizeof(sin);
	if(connect(cfd,(struct sockaddr*)&sin,addrlen) < 0)
	{
		perror("connect->");
		return -1;
	}
	//printf("服务器[%s : %d]链接成功!\n",IP,PORT);
	int res = -1;
	int res1 = -1;
	char buf[128] = "";
	char buf1[128] = "";
	printf("请输入(类型-账户-密码): ");
	while(1)
	{
		scanf("%s",buf);
		getchar();
		res = send(cfd,buf,sizeof(buf),0);
		if(res < 0)
		{
			perror("send->");
			return -1;
		}
		//printf("send success => %s\n",buf);
		res1 = recv(cfd,buf1,sizeof(buf1),0);
		if(res1 < 0)
		{
			perror("recv");
			return -1;
		}
		else if(res1 == 0)
		{
			printf("服务器 [%s : %d] 已掉线!\n",IP,PORT);
			return -1;
		}
		//解析反馈包
		char *p = buf1;
		//printf("反馈值==>  %s\n",p);
		if(strcmp("nameerr",p) == 0)
		{
			printf("账户错误,请重新输入: ");//回包 nameerr
		}
		else if(strcmp("passerr",p) == 0)
		{
			printf("密码错误,请重新输入: ");//回包 passerr
		}

		else if(strcmp("输入类型错误",p) == 0)
		{
			printf("输入类型错误,请重新输入: ");
		}
		else if(strcmp("登录成功",p) == 0)
		{
			printf("登录成功!\n");
			return 0;
		}
		else
		{
			printf("操作错误,请重新输入: ");
		}
	}
}

int sign(int cfd)//注册
{
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);
	socklen_t addrlen = sizeof(sin);
	if(connect(cfd,(struct sockaddr*)&sin,addrlen) < 0)
	{
		perror("connect->");
		return -1;
	}
	//printf("服务器[%s : %d]链接成功!\n",IP,PORT);
	int res = -1;
	int res1 = -1;
	char buf[128] = "";
	char buf1[128] = "";
	printf("请输入(类型-账户-密码): ");
	while(1)
	{
		scanf("%s",buf);
		getchar();
		res = send(cfd,buf,sizeof(buf),0);
		if(res < 0)
		{
			perror("send->");
			return -1;
		}
		//printf("send success => %s\n",buf);
		res1 = recv(cfd,buf1,sizeof(buf1),0);
		if(res1 < 0)
		{
			perror("recv");
			return -1;
		}
		else if(res1 == 0)
		{
			printf("服务器 [%s : %d] 已掉线!\n",IP,PORT);
			return -1;
		}
		//解析反馈包
		char *p = buf1;
		//printf("反馈值==>  %s\n",p);
		if(strcmp("namecf",p) == 0)
		{
			printf("账户重复,请重新输入: ");//回包 namecf
		}
		if(strcmp("注册成功",p) == 0)
		{
			printf("注册成功!\n");
			return 0;
		}
		else
		{
			//printf("p+> %s",p);
			printf("注册失败!\n");
			return 0;
		}
	}
}


int inquire(int cfd)//查询
{
	int kp = 0;

	printf("<================>\n");
	printf("<===1.查询单词===>\n");
	printf("<===2.查询记录===>\n");
	printf("<===3.退出登录===>\n");
	printf("<================>\n");

	while(1)
	{	
	
		printf("请输入功能请求(1查询单词/2查询记录/3退出登录): ");
		scanf("%d",&kp);

		if(kp == 1)
		{
			//查询
			quire(cfd);//查询单词
		}
		else if(kp == 2)
		{
			printf("<----------------->\n");
			Historical();//历史记录
			printf("<----------------->\n");
		}
		else if(kp == 3)
		{
			//printf("退出登录\n");
			break;//退出登录
		}
		else
		{
			printf("输入错误,请重新输入!\n");
		}
	}
}


int quire(int cfd)//查询单词
{
	int res = -1;
	int res1 = -1;
	char buf[128] = "";
	char buf1[128] = "";

	FILE *fp = fopen(TXT,"a+");
	if(fp == NULL)
	{
		perror("open->");
		return -1;
	}
	//printf("open success\n");

	printf("请输入要查询的单词: ");
	while(1)
	{
		scanf("%s",buf);
		//scanf("%hhd",buf);
		getchar();
		res = send(cfd,buf,sizeof(buf),0);
		if(res < 0)
		{
			perror("send->");
			return -1;
		}
		//printf("send success => %s\n",buf);
		int num = fprintf(fp,"%s",buf);//buf -> fd文件
		if(num < 0)
		{
			perror("fprintf->");
			return -1;
		}
		//printf("fprintf success\n");
		fputc('\t',fp);

		res1 = recv(cfd,buf1,sizeof(buf1),0);
		if(res1 < 0)
		{
			perror("recv");
			return -1;
		}
		else if(res1 == 0)
		{
			printf("服务器 [%s : %d] 已掉线!\n",IP,PORT);
			return -1;
		}

		int num1 = fprintf(fp,"%s",buf1);
		if(num1 < 0)
		{
			perror("fprintf->");
			return -1;
		}
		//printf("fprintf success\n");
		fputc('\n',fp);

		char *p = buf1;
		if(strcmp("error",p) == 0)
		{
			printf("单词未找到,请重新输入: ");
		}
		else
		{
			printf("单词已找到 %s : %s\n",buf,p);
			fclose(fp);
			break;
		}
	}
}


int Historical()//历史记录
{
	char buf = 0;
	FILE *fp = fopen(TXT,"r");
	if(fp == NULL)
	{
		perror("open->");
		return -1;
	}
	//printf("open success\n");

	while(1)
	{
		int num = fscanf(fp,"%c",&buf);//文件 -> buf
		if(num < 0)
		{
			//perror("fscanf->");
			fclose(fp);
			return -1;
		}
		else if(num == EOF)
		{
			printf("读取完成!\n");
			fclose(fp);
			break;
		}
		printf("%c",buf);
	}
}

==========

ubuntu@ubuntu:项目$ ./xmlic
<==================>
<======1.注册======>
<======2.登录======>
<======3.退出======>
<==================>
请输入请求(1注册/2登录/3退出): 1
请输入(类型-账户-密码): 1-uuu-123
注册成功!
请输入请求(1注册/2登录/3退出): 2
请输入(类型-账户-密码): 2-uuu-123
登录成功!
<================>
<===1.查询单词===>
<===2.查询记录===>
<===3.退出登录===>
<================>
请输入功能请求(1查询单词/2查询记录/3退出登录): 1
请输入要查询的单词: able
单词已找到 able : adj.有能力的,能干的
请输入功能请求(1查询单词/2查询记录/3退出登录): 2
<----------------->
able	adj.有能力的,能干的
able	adj.有能力的,能干的
abide	v.遵守
able	adj.有能力的,能干的
able	adj.有能力的,能干的
able	adj.有能力的,能干的
able	adj.有能力的,能干的
<----------------->
请输入功能请求(1查询单词/2查询记录/3退出登录): 3
请输入请求(1注册/2登录/3退出): 3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值