在线词典查询

在网上看了很多关于词典查询的代码,然后自己也下了来测试,发现了很多不够完善的地方,自己调试了很久。很多东西不自己动手来写一写就永远不知道哪里该注意。
能实现几个简单的功能:注册、登录、查询、查看历史记录

//服务器端

#include <stdio.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <sqlite3.h>
#define __USE_GNU1
#include <fcntl.h>
#include <stdint.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>

#define  N  16
#define  R  1   //  user register
#define  L  2   //  user login
#define  Q  3   //  query word
#define  H  4   //  history record

#define DATABASE "my.db"

typedef struct sockaddr SA;

typedef struct 
{
	int type;
	char name[N];
	char data[512];   // password or word or remark
} MSG;

void do_menu(int rws, sqlite3 *db);
void do_registe(int rws, MSG *pbuf, sqlite3 *db);
void do_login(int rws, MSG *pbuf, sqlite3 *db);
void do_query(int rws, MSG *pbuf, sqlite3 *db);
char *do_searchword(int rws, MSG *pbuf, char *word);
int do_history(int rws, MSG *pbuf, sqlite3 *db);
int do_time(char date[]);
#define SIZE 512

/******************************server_init***************************************/
int server_init(char *ipaddr,unsigned short port, int backlog)
{
	int sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if(-1 == sockfd)
	{
		perror("socket");
		return -1;
	}
	int on = 1;
	setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on,sizeof(on));

	struct sockaddr_in saddr;
	memset(&saddr,0,sizeof(saddr));
	saddr.sin_family = AF_INET;
	saddr.sin_port = htons(port);
	saddr.sin_addr.s_addr = ((ipaddr == NULL)?htonl(INADDR_ANY):inet_addr(ipaddr));

	socklen_t slen = sizeof(saddr);

	int ret = bind(sockfd,(struct sockaddr *)&saddr,slen);
	if(-1 == ret)
	{
		perror("bind");
		close(sockfd);
		return -1;
	}

	ret = listen(sockfd, backlog);
	if(-1 == ret)
	{
		perror("listen");
		close(sockfd);
		return -1;
	}


	return sockfd;
}
/*********************************siganl*************************************/
void fun(int signal)
{
	if(SIGCHLD == signal)
		while(waitpid(-1, NULL, WNOHANG) > 0);
}

/**********************************do_menu**********************************/
void do_menu(int rws, sqlite3 *db)
{
	char buf[SIZE];
	MSG msg;
	memset(buf, 0, sizeof(buf));
	int ret = 0;
	while((ret = read(rws, &msg, sizeof(msg)))>0)
	{
		switch(msg.type)
		{
			case R :
				do_registe(rws, &msg, db);
				break;
			case L : 
				do_login(rws, &msg, db);
				break;
			case Q : 
				do_query(rws, &msg, db);
				printf("*********************\n");
				break;
			case H : 
				do_history(rws, &msg, db);
				break;
		}
	}
}

/***********************************do_registe*************************************/
void do_registe(int rws, MSG *pbuf, sqlite3 *db)
{
	char sqlstr[128];
	char *errmsg;
	int ret;
	int nrow, ncolumn;
	char **resultp = NULL;
	ret = sqlite3_open(DATABASE, &db);
	if(ret == -1)
	{
		perror("sqlite3_open");
		return ;
	}
	sprintf(sqlstr, "select * from user where name = '%s';", pbuf->name);
	ret = sqlite3_get_table(db, sqlstr, &resultp, &nrow, &ncolumn, &errmsg);	
	if(ret == -1)
	{
		printf("error:%s\n", errmsg);
		sqlite3_close(db);
		return ;
	}
	if(nrow == 0)
	{
		sprintf(sqlstr, "insert into user values('%s', '%s');", pbuf->name, pbuf->data);
		if(sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg) != SQLITE_OK)
		{
			perror("sqlite3_exec");
			return ;
		}
		else
		{
			strcpy(pbuf->data, "OK");
		}
	}
	else 
		sprintf(pbuf->data, "%s already exsit!", pbuf->name);
	write(rws, pbuf, sizeof(MSG));
	return ;

}

/**********************************do_login**************************************/
void do_login(int rws, MSG *pbuf, sqlite3 *db)
{
	char sqlstr[128];
	char *errmsg;
	int ret;
	int nrow, ncolumn;
	char **resultp = NULL;
	ret = sqlite3_open(DATABASE, &db);
	if(ret == -1)
	{
		perror("sqlite3_open");
		return ;
	}
	sprintf(sqlstr, "select * from user where name = '%s' and data = '%s';", pbuf->name, pbuf->data);
	ret = sqlite3_get_table(db, sqlstr, &resultp, &nrow, &ncolumn, &errmsg);
	if(ret == -1)
	{
		printf("error:%s\n", errmsg);
		sqlite3_close(db);
		return ;
	}
	if(nrow == 0)
	{
		strcpy(pbuf->data, "username and userpassword exsit");
	}
	else
	strcpy(pbuf->data, "OK");
	write(rws, pbuf, sizeof(MSG));
	return ;
}

/***************************************do_query****************************************/
void do_query(int rws, MSG *pbuf, sqlite3 *db)
{
	char word[SIZE];
	char date[SIZE];
	char sqlstr[512];
	char *errmsg;
	char *p;
	strcpy(word, pbuf->data);
	p = do_searchword(rws, pbuf, word);
	get_time(date);
	int ret = sqlite3_open(DATABASE, &db);
	if(ret == -1)
	{
		perror("sqlite3_open");
		return ;
	}
	sprintf(sqlstr, "insert into notes values('%s','%s',\"%s\");", date, pbuf->name, p);
	if(sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg) !=  SQLITE_OK)
	{
		perror("sqlite3_exec");
		return ;
	}
	return ;
}

/************************************do_searchword**************************************/
char *do_searchword(int rws, MSG *pbuf, char word[])
{
	FILE *fp;
	int len;
	int m;
	char buf[SIZE];
	if((fp = fopen("dict.txt", "r")) == NULL)
	{
		perror("fopen");
		strcpy(pbuf->data, "fopen faile");
		write(rws, pbuf->data, sizeof(MSG));
		return NULL;
	}
	len = strlen(word);
	while(fgets(buf, sizeof(buf)-1, fp) != NULL) 
	{
		m = strncmp(word, buf, len);
		if(m > 0)
		{
			continue;
		}
		if(m < 0 || buf[len] != ' ')
		{
			continue;
		}
		strcpy(pbuf->data, buf);
		int n = write(rws, pbuf, sizeof(MSG));
		if(n == -1)
		{
			perror("write");
			return NULL;
		}
		fclose(fp);
		return pbuf->data;
	}
	strcpy(pbuf->data, "the word non_existent");
	write(rws, pbuf, sizeof(MSG));

	fclose(fp);
	return pbuf->data;
}

/*****************************get_time*********************************/
int get_time(char date[])
{
	time_t now;
	time(&now);
	memset(date, 0, sizeof(date));
	strncat(date, ctime(&now), 24);
	return 0;
}

/****************************do_history******************************/
int do_history(int rws, MSG *pbuf, sqlite3 *db)
{
	char sqlstr[128];
	char buf[512];
	int ret;
	int nrow, ncolumn;
	char **resultp = NULL;
	char *errmsg;
	ret = sqlite3_open(DATABASE, &db);
	if(ret == -1)
	{
		perror("sqlite3_open");
		return ;
	}
	memset(sqlstr, 0, sizeof(sqlstr));
	sprintf(sqlstr, "select * from notes where name='%s';", pbuf->name);
	ret = sqlite3_get_table(db, sqlstr, &resultp, &nrow, &ncolumn, &errmsg);
	if(ret == -1)
	{
		printf("error:%s\n", errmsg);
		sqlite3_close(db);
		return -1;
	}
	int i, j, count = ncolumn;
	for(i = 0; i < nrow; i++)
	{
		for(j = 0; j < ncolumn; j++)
		{
			memset(pbuf->data, 0, sizeof(pbuf->data));
			memset(buf, 0, sizeof(buf));
			strcat(buf, resultp[count++]);
			sprintf(pbuf->data, "%s ", buf);
			ret = write(rws, pbuf, sizeof(MSG));
			if(ret == -1)
			{
				perror("write");
				return -1;
			}
		}	
	}
	pbuf->data[0] = '\0';
	write(rws, pbuf, sizeof(MSG));
	return 0;
}
/********************************main***********************************/
int main(int argc, char *argv[])
{
	if(argc < 3)
	{
		printf("Usage:%s <ipaddr> <port>\n",argv[0]);
		return -1;
	}
	signal(SIGCHLD, fun);
	int sockfd = server_init(NULL,atoi(argv[2]),1024);
	if(-1 == sockfd)
	{
		printf("server_init error\n");
		return -1;
	}
	printf("listen....\n");
	int ret;
	sqlite3 *db;
	
	char buf[20] = {0};

	while(1)
	{
		int rws = 0;
		if((rws = accept(sockfd,NULL,NULL))<0)
		{
			perror("accept");
			close(sockfd);
			return -1;
		}
		pid_t pid; 
		pid = fork();
		if(pid == -1)
		{
			perror("fork");
			break;
		} 
		if(pid == 0)
		{
			do_menu(rws, db);
			close(rws);
			exit(0);
		}
	
		else
		{
			close(rws);
		}
	}
	return 0;
}


//客户端
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
//#include <sqlite3.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define  N  16
#define  R  1   //  user register
#define  L  2   //  user login
#define  Q  3   //  query word
#define  H  4   //  history record

#define DATABASE "my.db"

typedef struct sockaddr SA;

typedef struct 
{
	int type;
	char name[N];
	char data[512];   // password or word or remark
} MSG;

void do_register(int sockfd, MSG *pbuf)
{
	pbuf->type = R;
	printf("input name : ");
	scanf("%s", pbuf->name);
	printf("input password : ");
	scanf("%s", pbuf->data);
	send(sockfd, pbuf, sizeof(MSG), 0);
	recv(sockfd, pbuf, sizeof(MSG), 0);
	printf("register : %s\n", pbuf->data);

	return;
}

int do_login(int sockfd, MSG *pbuf)
{
	pbuf->type = L;
	printf("input name : ");
	scanf("%s", pbuf->name);
	printf("input password : ");
	scanf("%s", pbuf->data);
	send(sockfd, pbuf, sizeof(MSG), 0);
	recv(sockfd, pbuf, sizeof(MSG), 0);
	if (strncmp(pbuf->data, "OK", 3) == 0) 
	{
		printf("login : OK\n");
		return 1;
	}
	printf("login : %s\n", pbuf->data);

	return 0;
}

void do_query(int sockfd, MSG *pbuf)
{
	pbuf->type = Q;
	while ( 1 )
	{
		printf("input word (# means quit): ");
		scanf("%s", pbuf->data);
		pbuf->data[15] = '\0';
		if (strcmp(pbuf->data, "#") == 0) break;
		send(sockfd, pbuf, sizeof(MSG), 0);
		recv(sockfd, pbuf, sizeof(MSG), 0);
		//print(pbuf->data);
		printf("%s", pbuf->data);
		printf("\n");
	}

	return;
}

void do_history(int sockfd, MSG *pbuf)
{
	pbuf->type = H;
	send(sockfd, pbuf, sizeof(MSG), 0);
	while ( 1 )
	{
		//memset(pbuf->data, 0, sizeof(pbuf->data));
		recv(sockfd, pbuf, sizeof(MSG), 0);
		if (pbuf->data[0] == '\0') break;
		printf("%s\n", pbuf->data);
	}

	return;
}

int main(int argc, char *argv[])
{
	int sockfd;
	struct sockaddr_in servaddr;
	MSG buf;
	char clean[64];

	if (argc < 3)
	{
		printf("Usage : %s <serv_ip> <serv_port>\n", argv[0]);
		exit(-1);
	}

	// XXX int socket(int domain, int type, int protocol);
	if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
	{
		perror("fail to socket");
		exit(-1);
	}

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = PF_INET;
	servaddr.sin_port = htons(atoi(argv[2]));
	servaddr.sin_addr.s_addr = inet_addr(argv[1]);

	// XXX int connect(int sockfd, const struct sockaddr *addr,
	//                 socklen_t addrlen);
	if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) < 0)
	{
		perror("fail to connect");
		exit(-1);
	}

	int n;
	while ( 1 )
	{
		printf("************************************\n");
		printf("* 1: register   2: login   3: quit *\n");
		printf("************************************\n");
		printf("please choose : ");
		if (scanf("%d", &n) == 0)
		{
			fgets(clean, 64, stdin);
			printf("\n");
			continue;
		}
		switch ( n )
		{
		case 1 :
			printf("\n");
			do_register(sockfd, &buf);
			printf("\n");
			break;
		case 2 :
			printf("\n");
			if (do_login(sockfd, &buf) == 1)
			{
				printf("\n");
				goto next;
			}
			printf("\n");
			break;
		case 3 :
			close(sockfd);
			exit(0);
		}
	}

next:

	while ( 1 )
	{
		printf("***********************************************\n");
		printf("* 1: query_word   2: history_record   3: quit *\n");
		printf("***********************************************\n");
		printf("please choose : ");
		
		if (scanf("%d", &n) == 0)
		{
			fgets(clean, 64, stdin);
			printf("\n");
			continue;
		}
		switch ( n )
		{
		case 1 :
			printf("\n");
			do_query(sockfd, &buf);
			printf("\n");
			break;
		case 2 :
			printf("\n");
			do_history(sockfd, &buf);
			printf("\n");
			break;
		case 3 :
			close(sockfd);
			exit(0);
		}
	}

	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值