项目描述:
实现一个可以通过客户端,进行服务器交互,实现注册账号,登陆账号,查询单词,查询历史的功能。
项目实用技术:
1.使用sqlite3进行创建数据库,实现单词的查询记录和账户记录;
2.使用TCP网络通信,实现两台机器之间的联系;
3.使用并发,实现多个客户端登陆的功能;
项目图示分析:
功能实现
服务器
1.注册账号
static int do_register(int fd,sqlite3 *db, MSG *msg)
{
printf("register ...\n");
char sql[1024]={0};
int ret;
char *errmsg;
sprintf(sql,"insert into usr values('%s',%s);",msg->name,msg->data);
printf("%s\n",sql);
ret = sqlite3_exec(db, sql,NULL,NULL,&errmsg);
if(ret != 0)
{
printf("insert %s\n",errmsg);
return -1;
}else{
printf("insert success\n");
strcpy(msg->data,"OK");
do{
ret = send(fd, msg, sizeof(MSG), 0);
}while(ret < 0);
return 1;
}
return -1;
}
2.登录账号
static int do_login(int fd,sqlite3 *db,MSG *msg)
{
printf("login ...\n");
char sql[1024]={0};
char **result;
int row;
int ret;
int column;
char *pzErrmsg;
sprintf(sql,"select * from usr where name='%s' and pass=%s;",msg->name,msg->data);
ret = sqlite3_get_table(db, sql, &result, &row, &column, &pzErrmsg);
if(ret != 0)
{
printf("get_table %s\n",pzErrmsg);
return -1;
}
if(row == 1)
{
printf("login success...\n");
strcpy(msg->data,"OK");
do{
ret = send(fd, msg, sizeof(MSG),0);
}while(ret < 0);
}else{
strcpy(msg->data,"NOT OK");
do{
ret = send(fd, msg, sizeof(MSG),0);
}while(ret < 0);
printf("login failed...\n");
}
return -1;
}
3.查询单词
static int do_searchword(char *word,int fd, sqlite3 *db, MSG *msg)
{
FILE *fp;
char temp[1024]={0};
int len,ret;
char *p;
fp = fopen("dict.txt","r");
if(fp == NULL)
{
perror("fopen");
return -1;
}
int word_len = strlen(word);
while(fgets(temp, 1024, fp) != NULL)
{
len = strncmp(temp, word, word_len);
if(len < 0)
continue;
if((len > 0) || ((len == 0) && (temp[word_len] != ' ')))
break;
p = temp + len;
while(*p == ' ')
p++;
strcpy(msg->data,p);
printf("found:%s",msg->data);
do{
ret = send(fd, msg, sizeof(MSG), 0);
}while(ret < 0);fclose(fp);
return 1;
}
strcpy(msg->data,"not found");
do{
ret = send(fd, msg, sizeof(MSG), 0);
}while(ret < 0);
fclose(fp);
return -1;
}static char *do_time(int fd, sqlite3 *db, MSG *msg)
{
static char sql[1024]={0};
time_t te;
struct tm *tt;
time(&te);
tt = localtime(&te);
sprintf(sql,"%d/%d/%d %d:%d:%d",tt->tm_year+1900, tt->tm_mon, tt->tm_hour, tt->tm_mday, tt->tm_min, tt->tm_sec);
return sql;
}
static int do_query(int fd,sqlite3 *db,MSG *msg)
{
int ret = 0;
char sql[1024]={0};
char *errmsg;
char word[64]={0};
strcpy(word,msg->data);
ret = do_searchword(word,fd, db, msg);
if(ret == 1)
{
sprintf(sql,"insert into record values('%s','%s','%s');",msg->name,do_time(fd,db,msg),msg->data);
ret = sqlite3_exec(db, sql,NULL,NULL,&errmsg);
if(ret != 0)
{
printf("insert %s\n",errmsg);
return -1;
}else{
printf("query success\n");
return 1;
}
}
return -1;
}
4.查询历史
static int do_history(int fd,sqlite3 *db,MSG *msg)
{
char sql[1024]={0};
char buf[1024]={0};
int ret;
char *pzErrmsg;
char **pazResult;
int pnRow,pnColumn;
int i,j,index;
sprintf(sql,"select * from record where name = '%s';",msg->name);
if(sqlite3_get_table(db, sql, &pazResult, &pnRow, &pnColumn,&pzErrmsg) != 0)
{
printf("history %s",pzErrmsg);
return -1;
}else
{
printf("pnRow=%d,pnC=%d\n",pnRow,pnColumn);
index = pnColumn;
for(i=0;i<pnRow;i++)
{
for(j=0;j<pnColumn;j++)
{
printf("%s ",pazResult[index]);
sprintf(buf,"%s ",pazResult[index++]);
do{
ret = send(fd, buf, sizeof(buf),0);
}while(ret < 0);
}
}
bzero(buf,sizeof(buf));
strcpy(buf,"OK");
printf("buf=%s\n",buf);
do{
ret = send(fd,buf,sizeof(buf),0);
}while(ret < 0);
printf("do_histroy success\n");
}return 1;
}
客户端
1.注册账号
int do_register(int fd,MSG *msg)
{
int ret;
msg->type = R;
printf("please input you name:");
scanf("%s",msg->name