一、项目分析
1、在线词典一般的饿运行过程:例如
— 服务器端将用户信息和历史记录保存在数据库中。客户端输入用户和密码,服务器端在数据库中查找、匹配,返回结果;
2、项目的流程
— 定义数据库中表的结构;
— 定义消息结构体;
— 分析服务器端和客户端 流程;
— 编码实现;
name text primary key : 表格中,name相同的 的字符串只能出现一次;
3、客户端设计流程图:
4、服务器 流程图:
— 总结: 在做任何的项目之前,都应该首先根据 项目需求 画出 流程图,根据流程图,编程时将更加有目的性;
二、项目编码
1、服务端源码
#include "onlin_dic.h"
#include <sys/types.h>
#include <sqlite3.h>
#define DATABACK "my.db"
void cli_data_handle(int sockefd,sqlite3* db);
int do_searchword(sqlite3* db,_MSG *msg ,char word[]);//在文件中直接查找dict.txt
int do_register(int newfd , sqlite3* db , _MSG* msg)
{
char *errmsg;
char sql[128];
sprintf(sql,"insert into usr values('%s','%s');",msg->name,msg->data);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
{
printf("insert faile !!");
strcpy(msg->data,"usr name already exist.");
}else
{
printf("client register ok !\n");
strcpy(msg->data,"ok");
}
if(send(newfd,msg,sizeof(_MSG),0) < 0)
{
printf("faile to faile");
return -1;
}
return 1;
}
int do_history(int newfd , sqlite3* db , _MSG* msg)
{
char sql[128];
char history_word[255]= {0};
int nrow;
int ncloumn;
char** resultp;
char* errmsg;
int index;
int i,j;
int index2;
sprintf(sql,"select * from record where name = '%s';",msg->name);
if(sqlite3_get_table(db,sql,&resultp,&nrow,&ncloumn,&errmsg) != SQLITE_OK )
{
printf("sqlite3_get_table faile !!");
}else printf("sqlite3_get_table ok\n");//根据用户名以及密码,查找返回查找过得相应的记录
index = 0;//列数
for(i = 0;i<=nrow;i++)//行数
{
for(j = 0;j<=ncloumn;j++)
{
index2 = index;
printf("%s ",resul