SQLite实现在线电子词典

需求:

服务器:

1).提供英英方式的单词查询

2).同时记录用户的查询历史

2).客户机登陆需要密码

客户机:

1).登陆需要密码, 并且提供注册新用户功能, 注意在用户输入密码的时候不能显示密码,就像linux登陆时的那样

2).用户的查询即时给予回复

3).用户可以查询自己的查询历史

4).用户也可以清除自己的历史记录

下面是源码, 由于时间仓促,代码难免比较粗糙,希望谅解!

server.h:

/*************************************************************************
	> File Name: server.h
	> Author: Baniel Gao
	> Mail: createchance@163.com 
	> Created Time: Wed 15 Jan 2014 09:39:40 AM CST
 ************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include <sqlite3.h>
#include <errno.h>
#include <error.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <signal.h>

#define BUFF_SIZE		32
/*The len of sql*/
#define SQL_LEN			512

/*The max len of username*/
#define MAX_USERNAME	256
/*The max len of word and password*/
#define MAX_WORD		128
/*The max len of explaintion to word*/
#define EXP_LEN			1024

/*The flag of status
 *	R for register
 *	L for login
 *	Q for quary
 *	H for history
 *	S for client stop
 *	C for clear history
 * */
#define	R				1
#define L				2
#define Q				3
#define H				4
#define S				5
#define C				6

/*flags to show user status*/
#define ON_LINE			1
#define	OFF_LINE		0

/*network infomation*/
#define SERVER_IP		"0.0.0.0"
#define SERVER_PORT		50000

/*text dictionary path*/
#define DIC_PATH		"./dict/dic.txt"

/*system and user log path */
#define SYS_LOG			"./log/sys.log"
#define USER_LOG		"./log/user.log"

/*flags to set term display mode*/
#define ECHOFLAGS (ECHO | ECHOE | ECHOK | ECHONL)

/*error handler function*/
#define error_exit(_errmsg_)	error(EXIT_FAILURE, errno, _errmsg_)

/*client message struct*/
typedef struct _client_msg_ {
	char type;
	char name[MAX_USERNAME];
	char data[MAX_WORD];
} climsg_st;

/*server message struct*/
typedef struct _server_msg_ {
	char type;
	char text[EXP_LEN];
} sermsg_st;

/*functions to deal user quary*/
void menu(void);
void login_reg(void);
int show_history(sqlite3 *db, int connfd);
int clear_history(sqlite3 *db, int connfd);
int login(sqlite3 *db, int connfd);
int logout(sqlite3 *db, int connfd);
int regist(sqlite3 *db, int connfd);
int set_disp_mode(int fd,int option);
int quary(sqlite3 *db, int connfd);

server.c:

/*************************************************************************
	> File Name: server.c
	> Author: Baniel Gao
	> Mail: createchance@163.com 
	> Blog: blog.csdn.net/createchance 
	> Created Time: Wed 15 Jan 2014 11:52:12 AM CST
 ************************************************************************/
#include "server.h"

/*client and server message struct*/
climsg_st climsg;
sermsg_st sermsg;

int main(void)
{
	FILE *dictfp, *sysfp, *userfp;
	int sockfd, connfd;
	int ret;
	int lines = 0, alllines = 19661;
	pid_t pid;
	struct sockaddr_in server_addr, client_addr;
	socklen_t addrlen;
	sqlite3 *db;
	char explain[EXP_LEN];
	char *token;
	char sql[SQL_LEN];
	char *errmsg;

/*open local database my.db*/
	if (0 != sqlite3_open("my.db", &db)) {
		printf("error: %s \n", sqlite3_errmsg(db));
		return -1;
	}
/*open local text dictionary for read*/
	if (NULL == (dictfp = fopen(DIC_PATH, "r")))
		error_exit("fopen");
/*open system log for append*/
	if (NULL == (sysfp = fopen(SYS_LOG, "a")))
		error_exit("fopen");
/*open user log for append*/
	if (NULL == (userfp = fopen(USER_LOG, "a")))
		error_exit("fopen");

/*import the dictionary from local text dictionary*/
	while (1) {
		system("clear");
		printf("Importing data...\n");
		if (NULL == fgets(explain, EXP_LEN, dictfp))
			break;
		printf("\e[32m%.1f%% done!\e[0m \n", (lines++) * 100.0 / alllines);
		token = strtok(explain, " ");
		sprintf(sql, "insert into dictionary values(\"%s\", \"%s\");", token, explain + 17);
		if (0 !&#
  • 5
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值