在线词典项目实例

1. 在线词典功能分析

在线词典功能分析框图

数据库|SQLite编程实例---在线词典_sql

2. 服务器端和客户端流程图

服务器端流程示意图

数据库|SQLite编程实例---在线词典_sqlite3_02

客户端流程示意图

数据库|SQLite编程实例---在线词典_#define_03

3. 服务器端和客户端源码

服务器端源码

/*****dict_server.c*****/
//此处省略头文件...
#define N 32
#define R 1 //user - register
#define L 2 //user - login
#define Q 3 //user - query
#define H 4 //user - history
#define DATABASE "my.db"

typedef struct{ //定义通讯双方的信息结构体
int type;
char name[N];
char data[256];
}MSG;

int do_client(int acceptfd, sqlite3 *db);
void do_register(int acceptfd, MSG *msg, sqlite3 *db);
int do_login(int acceptfd, MSG *msg, sqlite3 *db);
int do_query(int acceptfd, MSG *msg, sqlite3 *db);
int do_history(int acceptfd, MSG *msg, sqlite3 *db);
int history_callback(void* arg,int f_num,char** f_value,char** f_name);
int do_searchword(int acceptfd, MSG *msg, char word[]);
int get_date(char *date);

int main(int argc, const char *argv[])
{
int sockfd;
struct sockaddr_in serveraddr;
int n;
MSG msg;
sqlite3 *db;
int acceptfd;
pid_t pid;

if(argc != 3){
printf("Usage:%s serverip port.\n", argv[0]);
return -1;
}

if(sqlite3_open(DATABASE, &db) != SQLITE_OK){ //打开数据库
printf("%s\n", sqlite3_errmsg(db));
return -1;
}
else{
printf("open DATABASE success.\n");
}

if((sockfd = socket(AF_INET, SOCK_STREAM,0)) < 0) {
perror("fail to socket.\n");
return -1;
}

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

if(bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0){
perror("fail to bind.\n");
return -1;
}

if(listen(sockfd, 5) < 0){ // 将套接字设为监听模式
printf("fail to listen.\n");
return -1;
}

//处理僵尸进程
signal(SIGCHLD, SIG_IGN);
while(1){
if((acceptfd = accept(sockfd, NULL, NULL)) < 0) {
perror("fail to accept");
return -1;
}

if((pid = fork()) < 0){
perror("fail to fork");
return -1;
}
else if(pid == 0){ // 儿子进程,处理客户端具体的消息
close(sockfd);
do_client(acceptfd, db);
}
else{ // 父亲进程,用来接受客户端的请求的
close(acceptfd);
}
}
return 0;
}

int do_client(int acceptfd, sqlite3 *db){
MSG msg;
while(recv(acceptfd, &msg, sizeof(msg), 0) > 0){
printf("type:%d\n", msg.type);
switch(msg.type){
case R:
do_register(acceptfd, &msg, db);
break;
case L:
do_login(acceptfd, &msg, db);
break;
case Q:
do_query(acceptfd, &msg, db);
break;
case H:
do_history(acceptfd, &msg, db);
break;
default:
printf("Invalid data msg.\n");
}
}
printf("client exit.\n");
close(acceptfd);
exit(0);
return 0;
}

void do_register(int acceptfd, MSG *msg, sqlite3 *db){
char * errmsg;
char sql[128];

sprintf(sql, "insert into usr values('%s', %s);", msg->name, msg->data);
printf("%s\n", sql);

if(sqlite3_exec(db,sql, NULL, NULL, &errmsg) != SQLITE_OK){
printf("%s\n", errmsg);
strcpy(msg->data, "usr name already exist.");
}
else{
printf("client register ok!\n");
strcpy(msg->data, "OK!");
}

if(send(acceptfd, msg, sizeof(MSG), 0) < 0){
perror("fail to send");
return ;
}

return ;
}

int do_login(int acceptfd, MSG *msg , sqlite3 *db){
char *errmsg, **resultp, sql[128] = {};
int nrow,ncloumn;

sprintf(sql, "select * from usr where name = '%s' and pass = '%s';", msg->name, msg->data);
printf("%s\n", sql);

if(sqlite3_get_table(db, sql, &resultp, &nrow, &ncloumn, &errmsg)!= SQLITE_OK){
printf("%s\n", errmsg);
return -1;
}
else{
printf("get_table ok!\n");
}

if(nrow == 1){ // 查询成功,数据库中拥有此用户
strcpy(msg->data, "OK");
send(acceptfd, msg, sizeof(MSG), 0);
return 1;
}

if(nrow == 0){ // 密码或者用户名错误
strcpy(msg->data,"usr/passwd wrong.");
send(acceptfd, msg, sizeof(MSG), 0);
}

return 0;
}

int do_searchword(int acceptfd, MSG *msg, char word[]){
FILE * fp;
int len = 0, result;
char *p, temp[512] = {};

if((fp = fopen("dict.txt", "r")) == NULL){ //打开文件,读取文件,进行比对
perror("fail to fopen.\n");
strcpy(msg->data, "Failed to open dict.txt");
send(acceptfd, msg, sizeof(MSG), 0);
return -1;
}

len = strlen(word); //打印出,客户端要查询的单词
printf("%s , len = %d\n", word, len);

while(fgets(temp, 512, fp) != NULL){ //读文件,来查询单词
result = strncmp(temp,word,len);
if(result < 0){
continue;
}
if(result > 0 || ((result == 0) && (temp[len]!=' '))){
break;
}


p = temp + len; // 表示找到了,查询的单词
while(*p == ' '){
p++;
}

strcpy(msg->data, p); // 找到了注释,跳跃过所有的空格
printf("found word:%s\n", msg->data);
fclose(fp); // 注释拷贝完毕之后,应该关闭文件
return 1;
}

fclose(fp);
return 0;
}

int get_date(char *date){
time_t t;
struct tm *tp;
time(&t);
tp = localtime(&t); //进行时间格式转换

sprintf(date, "%d-%d-%d %d:%d:%d", tp->tm_year + 1900, tp->tm_mon+1, tp->tm_mday,
tp->tm_hour, tp->tm_min , tp->tm_sec);
printf("get date:%s\n", date);

return 0;
}

int do_query(int acceptfd, MSG *msg , sqlite3 *db){
char word[64], *errmsg;
int found = 0;
char date[128] = {};
char sql[128] = {};

strcpy(word, msg->data); //拿出msg结构体中,要查询的单词
found = do_searchword(acceptfd, msg, word);
printf("查询一个单词完毕.\n");

if(found == 1){ // 表示找到了单词,那么此时应该将 用户名,时间,单词,插入到历史记录表中去
get_date(date); // 需要获取系统时间
sprintf(sql, "insert into record values('%s', '%s', '%s')", msg->name, date, word);

if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK) {
printf("%s\n", errmsg);
return -1;
}
else{
printf("Insert record done.\n");
}
}
else{ //表示没有找到
strcpy(msg->data, "Not found!");
}

send(acceptfd, msg, sizeof(MSG), 0); // 将查询的结果,发送给客户端
return 0;
}

// 得到查询结果,并且需要将历史记录发送给客户端
int history_callback(void* arg,int f_num,char** f_value,char** f_name){
int acceptfd;
MSG msg;
acceptfd = *((int *)arg);

sprintf(msg.data, "%s , %s", f_value[1], f_value[2]);
send(acceptfd, &msg, sizeof(MSG), 0);
return 0;
}


int do_history(int acceptfd, MSG *msg, sqlite3 *db){
char sql[128] = {};
char *errmsg;

sprintf(sql, "select * from record where name = '%s'", msg->name);
if(sqlite3_exec(db, sql, history_callback,(void *)&acceptfd, &errmsg)!= SQLITE_OK){ //查询数据库
printf("%s\n", errmsg);
}
else{
printf("Query record done.\n");
}

msg->data[0] = '\0'; // 所有的记录查询发送完毕之后,给客户端发出一个结束信息
send(acceptfd, msg, sizeof(MSG), 0);

return 0;
}

客户端源码

/*****dict_client.c*****/
//此处省略头文件...
#define N 32
#define R 1 //user - register
#define L 2 //user - login
#define Q 3 //user - query
#define H 4 //user - history

typedef struct{ //定义通讯双方的信息结构体
int type;
char name[N];
char data[256];
}MSG;

int do_register(int sockfd, MSG *msg){
msg->type = R;

printf("Input name:");
scanf("%s", msg->name);
getchar();

printf("Input passwd:");
scanf("%s", msg->data);

if(send(sockfd, msg, sizeof(MSG),0) < 0){
printf("fail to send.\n");
return -1;
}

if(recv(sockfd, msg, sizeof(MSG), 0) < 0){
printf("Fail to recv.\n");
return -1;
}

printf("%s\n", msg->data); // ok ! or usr alread exist.
return 0;
}

int do_login(int sockfd, MSG *msg){
msg->type = L;

printf("Input name:");
scanf("%s", msg->name);
getchar();

printf("Input passwd:");
scanf("%s", msg->data);

if(send(sockfd, msg, sizeof(MSG),0) < 0){
printf("fail to send.\n");
return -1;
}

if(recv(sockfd, msg, sizeof(MSG), 0) < 0){
printf("Fail to recv.\n");
return -1;
}

if(strncmp(msg->data, "OK", 3) == 0){
printf("Login ok!\n");
return 1;
}
else{
printf("%s\n", msg->data);
}

return 0;
}

int do_query(int sockfd, MSG *msg){
msg->type = Q;
puts("--------------");

while(1){
printf("Input word:");
scanf("%s", msg->data);
getchar();

if(strncmp(msg->data, "#", 1) == 0) //客户端,输入#号,返回到上一级菜单
break;

if(send(sockfd,msg, sizeof(MSG), 0) < 0){ //将要查询的单词发送给服务器
printf("Fail to send.\n");
return -1;
}

if(recv(sockfd, msg,sizeof(MSG), 0) < 0){ // 等待接受服务器,传递回来的单词的注释信息
printf("Fail to recv.\n");
return -1;
}
printf("%s\n", msg->data);
}

return 0;
}

int do_history(int sockfd, MSG *msg){

msg->type = H;

send(sockfd, msg, sizeof(MSG), 0);
while(1){ // 接受服务器,传递回来的历史记录信息
recv(sockfd, msg, sizeof(MSG), 0);
if(msg->data[0] == '\0')
break;
printf("%s\n", msg->data); //输出历史记录信息
}
return 0;
}

// ./server 192.168.3.196 10000
int main(int argc, const char *argv[]){

int sockfd, n;
struct sockaddr_in serveraddr;
MSG msg;

if(argc != 3){
printf("Usage:%s serverip port.\n", argv[0]);
return -1;
}

if((sockfd = socket(AF_INET, SOCK_STREAM,0)) < 0){
perror("fail to socket.\n");
return -1;
}

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

if(connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0){
perror("fail to connect");
return -1;
}

while(1){
printf("*****************************************************************\n");
printf("* 1.register 2.login 3.quit *\n");
printf("*****************************************************************\n");
printf("Please choose:");
scanf("%d", &n);
getchar();

switch(n){
case 1:
do_register(sockfd, &msg);
break;
case 2:
if(do_login(sockfd, &msg) == 1){
goto next;
}
break;
case 3:
close(sockfd);
exit(0);
break;
default:
printf("Invalid data cmd.\n");
}
}

next:
while(1){
printf("*****************************************************\n");
printf("* 1.query_word 2.history_record 3.quit *\n");
printf("*****************************************************\n");
printf("Please choose:");
scanf("%d", &n);
getchar();

switch(n){
case 1:
do_query(sockfd, &msg);
break;
case 2:
do_history(sockfd, &msg);
break;
case 3:
close(sockfd);
exit(0);
break;
default :
printf("Invalid data cmd.\n");
}
}
return 0;
}


关注我的公众号,共同交流学习嵌入式开发相关技术:
数据库|SQLite编程实例---在线词典_sql_04