一、项目要求:
1.登录注册功能,不能重复登录,重复注册。用户信息也存储在数据库中。
2.单词查询功能
3.历史记录功能,存储单词,意思,以及查询时间,存储在数据库
4.基于TCP,支持多客户端连接
5.采用数据库保存用户信息与历史记录
6.将dict.txt的数据导入到数据库中保存。
7.返回上级、按下ctrl+c退出客户端后,该用户退出登录
二、格式要求:
1.main函数只跑逻辑,不允许跑功能代码
2.功能代码封装成函数
三、项目功能:
1.注册
2.登录
3.查询单词
4.查看历史记录
5.退出登录
四、框架设计:
4.1服务器设计
4.2客户端设计
五、功能说明:
5.1用户注册
输入用户名和密码,插入用户表中,用户名不能和已有用户名字冲突,否则失败。
5.2用户登录
输入用户名和密码,通过遍历表中数据,核对数据信息,成功,转到第二个操作界面。
5.3查询单词
输入单词,在词典文件中查找,存在得到单词解释,并且将查询记录保存到记录表中。
5.4查询历史记录
遍历记录表中数据,将用户查询的时间和单词打印到终端上。
5.5多进程实现TCP并发
六、具体代码:
head.h:
#ifndef __HEAD_H__
#define __HEAD_H__#include <stdio.h>
#include <sqlite3.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <time.h>#define PORT 8808
#define IP "127.0.0.1"
#define ERR_MSG(msg) { fprintf(stderr,"__%d__",__LINE__); perror(msg); }int put_dict(sqlite3 *db);
void handler(int sig);
int do_register(sqlite3 *db,char *buf);
void do_register_cli(char *buf);
int Register(sqlite3 *db,char *buf);
void do_login(char *buf);
int Login(sqlite3 *db,char *buf);
int do_select_user(sqlite3 *db,char *name);
int do_insert(sqlite3 *db,char *words,char *mean,char *save_buf);
int Search(sqlite3 *db,char *buf,char *save_buf);
int Search_res(sqlite3 *db,char *buf,char *save_buf);
int deal_cli_msg(int newfd,struct sockaddr_in cin,sqlite3 *db);#endif
dictionary.c(功能函数):
#include "head.h"//导入词典
int put_dict(sqlite3 *db)
{//创建并打开一个数据库if(sqlite3_open("./dict.db",&db) != SQLITE_OK){printf("sqlite3_open:%s %d __%d__\n",\sqlite3_errmsg(db),sqlite3_errcode(db),__LINE__);return -1;}//创建一个表格 create table stu (id int,name char,score float);//数据库中sql语句怎么写,这里就怎么写char sql[128] = "create table dict (word char,mean char);";char* errmsg = NULL;if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_open:%s %d __%d__\n",\errmsg,sqlite3_errcode(db),__LINE__);return -1;}//打开文件FILE* fp = fopen("./dict.txt","r");if(NULL == fp){perror("fopen");return -1;}//循环读取文件中的数据,一行一行的读取char buf[256] = "";char word[32] = "";char mean[200] = "";int count = 1;int i =0;char* ptr = NULL;while(1){if(fgets(buf,sizeof(buf),fp) == NULL)break;buf[strlen(buf)-1] = 0;//分离单词和意思bzero(word,sizeof(word));bzero(mean,sizeof(mean));//获取" "子串在buf中的地址ptr = strstr(buf," ");if(NULL == ptr){printf("没有找到对应子串\n");break;}strncpy(word,buf,ptr-buf); //" "子串前面是单词strcpy(mean,ptr+3); //" "子串后面是意思//插入到数据库中sprintf(sql,"insert into dict values(\"%s\",\"%s\");",word,mean);if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){printf("sqlite3_exec failed:%s __%d__\n",errmsg,__LINE__);return -1;}}printf("数据库建立成功\n");//关闭文件fclose(fp);//关闭数据库if(sqlite3_close(db) != SQLITE_OK){fprintf(stderr,"sqlite3_close:%s %d __%d__\n",\sqlite3_errmsg(db),sqlite3_errcode(db),__LINE__);return -1;}return 0;
}//捕获信号
void handler(int sig)
{while(waitpid(-1, NULL, WNOHANG) > 0);
}//实现注册功能
int do_register(sqlite3 *db,char *buf)
{//打开数据库if(sqlite3_open("./dict.db",&db) != SQLITE_OK){fprintf(stderr,"sqlite3_open: %s __%d__\n",sqlite3_errmsg(db),__LINE__);return -1;}//创建一个表char sql[128] = "create table if not exists user (name char,passwd char);";char *errmsg = NULL;if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_exec: %s __%d__\n",errmsg,__LINE__);return -1;}char *name = buf+2;char *passwd = buf+2+strlen(buf+2)+1;if(do_select_user(db,name) == 1){if(sqlite3_close(db) != SQLITE_OK){printf("sqlite3_close failed:%s __%d__\n", sqlite3_errmsg(db), __LINE__);return -1;}return 1;}sprintf(sql,"insert into user values ('%s','%s');",name,passwd);if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_exec: %s __%d__\n",errmsg,__LINE__);return -1;}printf("register success...\n");//关闭数据库,释放对应的内存空间if(sqlite3_close(db) != SQLITE_OK){printf("sqlite3_close failed:%s __%d__\n", sqlite3_errmsg(db), __LINE__);return -1;}return 0;
}//客户端注册函数
void do_register_cli(char *buf)
{char name[32] = "";char passwd[32] = "";printf("请输入您的用户名>>>");scanf("%s",name);getchar();printf("请输入您的密码>>>");scanf("%s",passwd);getchar();sprintf(buf,"%s%c%s%c%s","1",0,name,0,passwd);
}//实现注册功能
int Register(sqlite3 *db,char *buf)
{//打开数据库if(sqlite3_open("./dict.db",&db) != SQLITE_OK){fprintf(stderr,"sqlite3_open: %s __%d__\n",sqlite3_errmsg(db),__LINE__);return -1;}//创建一个表char sql[128] = "create table if not exists user (name char,passwd char);";char *errmsg = NULL;if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_exec: %s __%d__\n",errmsg,__LINE__);return -1;}char *name = buf+2;char *passwd = buf+2+strlen(buf+2)+1;if(do_select_user(db,name) == 1){if(sqlite3_close(db) != SQLITE_OK){printf("sqlite3_close failed:%s __%d__\n", sqlite3_errmsg(db), __LINE__);return -1;}return 1;}sprintf(sql,"insert into user values ('%s','%s');",name,passwd);if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_exec: %s __%d__\n",errmsg,__LINE__);return -1;}printf("register success...\n");//关闭数据库,释放对应的内存空间if(sqlite3_close(db) != SQLITE_OK){printf("sqlite3_close failed:%s __%d__\n", sqlite3_errmsg(db), __LINE__);return -1;}return 0;
}//登录函数
void do_login(char *buf)
{char name[32] = "";char passwd[32] = "";printf("请输入用户名>>>");scanf("%s",name);getchar();printf("请输入密码>>>");scanf("%s",passwd);getchar();sprintf(buf,"%s%c%s%c%s","2",0,name,0,passwd);
}//实现登录功能
int Login(sqlite3 *db,char *buf)
{//打开数据库if(sqlite3_open("./dict.db",&db) != SQLITE_OK){fprintf(stderr,"sqlite3_open: %s __%d__\n",sqlite3_errmsg(db),__LINE__);return -1;}char sql[128] = "create table if not exists user (name char,passwd char);";char *errmsg = NULL;if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_exec: %s __%d__\n",errmsg,__LINE__);return -1;}strcpy(sql,"select * from user;");char **pres = NULL;int row,column;if(sqlite3_get_table(db,sql,&pres,&row,&column,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_exec: %s __%d__\n",errmsg,__LINE__);return -1;}char *name = buf+2;char *passwd = buf+2+strlen(buf+2)+1;for(int i=2;i<(row+1)*column;i++){if(strcmp(pres[i],name) == 0 && strcmp(pres[i+1],passwd) == 0){printf("login success...\n");if(sqlite3_close(db) != SQLITE_OK){printf("sqlite3_close failed:%s __%d__\n", sqlite3_errmsg(db), __LINE__);return -1;}return 2;}}//关闭数据库,释放对应的内存空间if(sqlite3_close(db) != SQLITE_OK){printf("sqlite3_close failed:%s __%d__\n", sqlite3_errmsg(db), __LINE__);return -1;}return 3;
}//查找用户名是否存在
int do_select_user(sqlite3 *db,char *name)
{char sql[128] = "select name from user;";char **pres = NULL;int row,column;char *errmsg = NULL;if(sqlite3_get_table(db,sql,&pres,&row,&column,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_exec: %s __%d__\n",errmsg,__LINE__);return -1;}for(int i=1;i<(row+1)*column;i++){if(strcmp(pres[i],name) == 0){fprintf(stderr,"sqlite3_exec: name already existss\n");return 1;}}return 0;
}//实现查询功能函数
int do_insert(sqlite3 *db,char *words,char *mean,char *save_buf)
{//获取当前时间戳time_t t;struct tm *info=NULL;char mytime[128] = "";t = time(NULL);info = localtime(&t);sprintf(mytime,"%d-%02d-%02d %02d:%02d:%02d",info->tm_year+1900,info->tm_mon+1,info->tm_mday,info->tm_hour,info->tm_min,info->tm_sec);//打开数据库if(sqlite3_open("./dict.db",&db) != SQLITE_OK){fprintf(stderr,"sqlite3_open: %s __%d__\n",sqlite3_errmsg(db),__LINE__);return -1;}//创建一个表char sql[128] = "create table if not exists history (name char,word char,mean char,time char);";char *errmsg = NULL;char *name = save_buf;if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_exec: %s __%d__\n",errmsg,__LINE__);return -1;}//插入查询记录sprintf(sql,"insert into history values (\"%s\",\"%s\", \"%s\",\"%s\");",name,words, mean,mytime);if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK){printf("sqlite3_exec failed:%s __%d__\n", errmsg, __LINE__);return -1;}printf("do_insert success...\n");
}//查询单词
int do_search(sqlite3 *db,char *buf,char *save_buf)
{//打开数据库if(sqlite3_open("./dict.db",&db) != SQLITE_OK){fprintf(stderr,"sqlite3_open: %s __%d__\n",sqlite3_errmsg(db),__LINE__);return -1;}char sql[128] = "select * from dict;";char **pres = NULL;int row,column;char *errmsg = NULL;if(sqlite3_get_table(db,sql,&pres,&row,&column,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_get_table: %s __%d__\n",errmsg,__LINE__);return -1;}char *words = buf+2;for(int i=0;i<(row+1)*column;i++){if(strcmp(pres[i],words) == 0){//若查询成功,则将该单词插入记录do_insert(db,words,pres[i+1],save_buf);bzero(buf, sizeof(buf));sprintf(buf,"\t%s\t\t%s\t",pres[i],pres[i+1]);if(sqlite3_close(db) != SQLITE_OK){printf("sqlite3_close failed:%s __%d__\n", sqlite3_errmsg(db), __LINE__);return -1;}return 4;}}//关闭数据库,释放对应的内存空间if(sqlite3_close(db) != SQLITE_OK){printf("sqlite3_close failed:%s __%d__\n", sqlite3_errmsg(db), __LINE__);return -1;}return 5;
}//查询记录函数
int do_search_res(sqlite3 *db,char *buf,char *save_buf)
{//打开数据库if(sqlite3_open("./dict.db",&db) != SQLITE_OK){fprintf(stderr,"sqlite3_open: %s __%d__\n",sqlite3_errmsg(db),__LINE__);return -1;}char sql[128] = "create table if not exists history (name char,word char,mean char,time char);";char *errmsg = NULL;if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_exec: %s __%d__\n",errmsg,__LINE__);return -1;}char msg[128] = "";strcpy(sql,"select * from history;");char **pres = NULL;int row,column;if(sqlite3_get_table(db,sql,&pres,&row,&column,&errmsg) != SQLITE_OK){fprintf(stderr,"sqlite3_get_table: %s __%d__\n",errmsg,__LINE__);return -1;}bzero(buf,sizeof(buf));for(int i=4;i<(row+1)*column;i++){if(i%4==0 && strcmp(save_buf,pres[i]) == 0){sprintf(msg,"%s\t%s\t%s\t%s\n",pres[i],pres[i+1],pres[i+2],pres[i+3]);strcat(buf,msg);}}if(strlen(buf)!=0)*(buf+strlen(buf)-1) = 0;//关闭数据库,释放对应的内存空间if(sqlite3_close(db) != SQLITE_OK){printf("sqlite3_close failed:%s __%d__\n", sqlite3_errmsg(db), __LINE__);return -1;}if(strlen(buf) == 0)return 7;return 6;
}
//服务器子进程处理客户端信息
int deal_cli_msg(int newfd,struct sockaddr_in cin,sqlite3 *db)
{int flag = 0;char buf[128] = "";char save_buf[128] = "";ssize_t res = 0;int f_res = -1;while(1){bzero(buf, sizeof(buf));//接收数据res = recv(newfd, buf, sizeof(buf), 0);if(res < 0){ERR_MSG("recv");return -1;}else if(0 == res){printf("[%s : %d] client offline\n",inet_ntoa(cin.sin_addr), ntohs(cin.sin_port));break;}//调用功能函数if(strcmp(buf,"1") == 0)f_res = Register(db,buf); //注册else if(strcmp(buf,"2") == 0){f_res = Login(db,buf); //登录strcpy(save_buf,buf+2); //将用户名另存}else if(strcmp(buf,"3") == 0){f_res = do_search(db,buf,save_buf); //查询单词}else if(strcmp(buf,"4") == 0)f_res = do_search_res(db,buf,save_buf); //查询记录//发送数据if(0 == f_res){bzero(buf, sizeof(buf));strcpy(buf,"[注册成功!]");}else if(1 == f_res){bzero(buf, sizeof(buf));strcpy(buf,"[对不起,注册失败,该用户名已存在!]");}else if(2 == f_res){bzero(buf, sizeof(buf));strcpy(buf,"[登录成功!]");}else if(3 == f_res){bzero(buf, sizeof(buf));strcpy(buf,"[对不起,登录失败,该用户名/密码不存在!]");}else if(5 == f_res){bzero(buf, sizeof(buf));strcpy(buf,"[对不起,无法查找找到该单词!]");}else if(7 == f_res){bzero(buf, sizeof(buf));strcpy(buf,"[对不起,该用户没有历史记录!]");}if(send(newfd, buf, sizeof(buf), 0) < 0){ERR_MSG("send");return -1;}}close(newfd);return 0;
}
服务器(Ser.c):
#include "head.h"
int main(int argc, const char *argv[])
{sqlite3 *db = NULL;if(signal(17, handler) == SIG_ERR){ERR_MSG("signal");return -1;}//导入词典 put_dict(db);//创建流式套接字int sfd = socket(AF_INET,SOCK_STREAM,0);if(sfd < 0){ERR_MSG("socket");return -1;}//设置允许端口号复用int reuse = 1;if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) { perror("setsockopt");return -1;}//绑定服务器IP和端口号struct sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_port = htons(PORT);sin.sin_addr.s_addr = inet_addr(IP);if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin)) < 0){ERR_MSG("bind");return -1;}//将套接字设置为被动监听状态if(listen(sfd,10) < 0){ERR_MSG("listen");return -1;}int newfd = -1;struct sockaddr_in cin;socklen_t addrlen = sizeof(cin);//从已完成连接的队列中获取一个客户端信息,生成一个新的文件描述符while(1){newfd = accept(sfd,(struct sockaddr *)&cin,&addrlen);if(newfd < 0){ERR_MSG("accept");return -1;}printf("[%s : %d] connect success...\n",inet_ntoa(cin.sin_addr), ntohs(cin.sin_port));if(0 == fork()){close(sfd);deal_cli_msg(newfd,cin,db);exit(0);}close(newfd);}close(sfd);return 0;
}
客户端(Cli.c):
#include "head.h"int main(int argc, const char *argv[])
{sqlite3 *db =NULL;//创建流式套接字int cfd = socket(AF_INET, SOCK_STREAM, 0);if(cfd < 0){ERR_MSG("socket");return -1;}printf("cfd = %d\n", cfd);//绑定客户端的的IP和端口--->非必须绑定//若不绑定,则操作系统会给客户端绑定上客户端所在的主机IP,以及随机端口(49152~65535)//填充服务器的地址信息结构体,真实的地址信息结构体根据地址族指定//AF_INET: man 7 ip //要连接哪个服务器,就填对应服务器的IP和端口struct sockaddr_in sin;sin.sin_family = AF_INET; //必须填AF_INETsin.sin_port = htons(PORT); //端口号: 填服务器绑定端口号sin.sin_addr.s_addr = inet_addr(IP); //IP地址:服务器绑定的IP地址//连接服务器if(connect(cfd, (struct sockaddr*)&sin, sizeof(sin)) < 0){ERR_MSG("connect");return -1;}printf("connect success\n");ssize_t res = 0;char buf[128] = "";char buf1[128] ="";char save_buf[128] ="";char choose = 0;while(1){system("clear");printf("-----------------------\n");printf("---------1.注册--------\n");printf("---------2.登录--------\n");printf("---------3.退出--------\n");printf("-----------------------\n");printf("请输入您需要执行的操作>>>");choose = getchar();while(getchar() != 10);switch(choose){case '1'://注册函数do_register_cli(buf);break;case '2'://登录函数do_login(buf);break;case '3'://退出函数goto END;default:printf("对不起,您的输入有误,请重新输入\n");}//发送数据if(send(cfd,buf,sizeof(buf),0) < 0){perror("send");return -1;}//接受数据bzero(buf,sizeof(buf));res = recv(cfd,buf,sizeof(buf),0);if(res < 0){perror("recv");return -1;}else if(0 == res){printf("[%s : %d] server offline\n",IP,PORT);break;}printf("%s\n",buf);strcpy(save_buf,buf);while(strcmp(save_buf,"[登录成功!]") == 0){system("clear");printf("--------------------------\n");printf("-------1.查找单词---------\n");printf("-------2.查找历史记录-----\n");printf("-------3.退出登录---------\n");printf("--------------------------\n");printf("请输入您要执行的操作>>>");choose = getchar();while(getchar() != 10);// bzero(buf,sizeof(buf));// fgets(buf,sizeof(buf),stdin);// buf[strlen(buf)-1] = 0;char words[20] = "";switch(choose){case '1':{while(1){bzero(buf,sizeof(buf));printf("请输入您要查找的单词(输入 \"#\" 结束)>>>");scanf("%s",words);getchar();if(strcmp(words,"#") == 0)break;sprintf(buf,"%s%c%s%c","3",0,words,0);//发送数据if(send(cfd,buf,sizeof(buf),0) < 0){perror("send");return -1;}//接收查询单词结果bzero(buf,sizeof(buf));res = recv(cfd,buf,sizeof(buf),0);if(res < 0){perror("recv");return -1;}else if(0 == res){printf("[%s : %d] server offline\n",IP,PORT);// break;goto END;}printf("%s\n",buf);}}break;case '2'://登录函数{sprintf(buf,"%s%c","4",0);//发送数据if(send(cfd,buf,sizeof(buf),0) < 0){perror("send");return -1;}//接受查询单词结果bzero(buf,sizeof(buf));res = recv(cfd,buf,sizeof(buf),0);if(res < 0){perror("recv");return -1;}else if(0 == res){printf("[%s : %d] server offline\n",IP,PORT);goto END;// break;}printf("%s\n",buf);}break;case '3':goto END1;default:printf("对不起,您的输入有误,请重新输入\n");}printf("输入任意字符清屏>>>");while(getchar() != 10);}
END1:printf("输入任意字符清屏>>>");while(getchar() != 10);}
END://关闭所有文件描述符close(cfd);return 0;
}