#include"dict.h"voiddo_client(int,sqlite3*);voiddo_register(int,message*,sqlite3*);voiddo_login(int,message*,sqlite3*);voiddo_query(int,message*,sqlite3*);intmain(int argc,constchar*argv[]){int listenfd,connectfd;struct sockaddr_in server_addr;
pid_t pid;
sqlite3 *data;if(argc <3){printf("usage : %s <ip> <port> \n",argv[0]);exit(-1);}if(sqlite3_open("my.db",&data)!= SQLITE_OK){printf("errer:%s\n",sqlite3_errmsg(data));exit(-1);}if((listenfd =socket(PF_INET,SOCK_STREAM,0))<0){perror("fail to socket");exit(-1);}bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = PF_INET;
server_addr.sin_addr.s_addr =inet_addr(argv[1]);
server_addr.sin_port =htons(atoi(argv[2]));if(bind(listenfd,(struct sockaddr *)&server_addr,sizeof(server_addr))<0){perror("fail to bind");exit(-1);}if(listen(listenfd,5)<0){perror("fail to listen");exit(-1);}signal(SIGCHLD,SIG_IGN);while(1){if((connectfd =accept(listenfd,NULL,NULL))<0){perror("fail to accept");exit(-1);}if((pid =fork())<0){perror("fail to fork");exit(-1);}if(pid ==0){do_client(connectfd,data);}close(connectfd);}return0;}voiddo_client(int connectfd,sqlite3 *data){
message msg;puts("do_client");while(recv(connectfd,&msg,sizeof(message),0)>0){printf("msg.name:%s msg.data:%s\n",msg.name,msg.data);switch(msg.type){case R:puts("R");do_register(connectfd,&msg,data);break;case L:puts("L");do_login(connectfd,&msg,data);break;case Q:puts("Q");do_query(connectfd,&msg,data);break;case H:puts("H");break;default:puts("wrong msg type:");}}puts("client quit");close(connectfd);exit(0);}voiddo_register(int connectfd,message *msg,sqlite3 *data){char sqlcmd[DATA_MAX]={0};char*errmsg;sprintf(sqlcmd,"insert into usr(name,passwd) values('%s','%s')",msg->name,msg->data);if(sqlite3_exec(data,sqlcmd,NULL,NULL,&errmsg)!= SQLITE_OK){sprintf(msg->data,"register :%s\n",errmsg);}else{strcpy(msg->data,"register is ok");}printf("msg->name:%s,msg->data:%s\n",msg->name,msg->data);send(connectfd,msg,sizeof(message),0);return;}voiddo_login(int connectfd,message *msg,sqlite3* data){char sqlcmd[DATA_MAX]={0};char*errmsg;char**result;int nrow =0;int ncolumn =0;sprintf(sqlcmd,"select * from usr where name = '%s' and passwd = '%s'",msg->name,msg->data);puts(sqlcmd);if(sqlite3_get_table(data,sqlcmd,&result,&nrow,&ncolumn,&errmsg)!= SQLITE_OK){printf("do_login error!:%s \n",errmsg);}if(nrow ==1){strcpy(msg->data,"Login is ok!");
msg->type = OK;}else{strcpy(msg->data,"Usr name or the passwd is wrong!");}send(connectfd,msg,sizeof(message),0);return;}voiddo_query(int connectfd,message* msg,sqlite3* data){char sqlcmd[DATA_MAX]={0};char**result;char*errmsg;int nrow;int ncolumn;printf("your name:%s ,your word :%s\n",msg->name,msg->data);sprintf(sqlcmd,"select * from dict where word = '%s'",msg->data);puts(sqlcmd);if(sqlite3_get_table(data,sqlcmd,&result,&nrow,&ncolumn,&errmsg)!= SQLITE_OK){printf("sqlite3_get_table is error!");}if(!nrow){strcpy(msg->data,"your word is not found");}else{
msg->type = OK;printf("data:%s\n",result[3]);strcpy(msg->data,result[3]);}puts(msg->data);send(connectfd,msg,sizeof(message),0);return;}
client.c
#include"dict.h"voiddo_register(int);voiddo_login(int);voiddo_query(int,char*name);voiddo_history(int,char*name);intdictionary(int,char*);intmain(int argc,constchar*argv[]){int socketfd;struct sockaddr_in server_addr;if(argc <3){printf("usage : %s <server_ip> <server_port>\n",argv[0]);exit(-1);}if((socketfd =socket(PF_INET,SOCK_STREAM,0))<0){perror("fail to socket");exit(-1);}bzero(&server_addr,sizeof(server_addr));
server_addr.sin_family = PF_INET;
server_addr.sin_addr.s_addr =inet_addr(argv[1]);
server_addr.sin_port =htons(atoi(argv[2]));if(connect(socketfd,(struct sockaddr*)&server_addr,sizeof(server_addr))<0){perror("fail to connect");exit(-1);}int input;while(1){putchar(10);putchar(10);puts("*******************************************");puts("* Welcom to online dictionary *");puts("* 1.register 2.login 3.quit *");puts("*******************************************");putchar(10);putchar(10);puts("input a number in (1-3):");if(scanf("%d",&input)<=0){perror("in scanf");setbuf(stdin,NULL);//clear input buffercontinue;}switch(input){case1:do_register(socketfd);break;case2:puts("2");do_login(socketfd);break;case3:puts("3");exit(0);}}close(socketfd);return0;}voiddo_register(int socketfd){
message msg;bzero(&msg,sizeof(message));
msg.type = R;puts("input :your user name");scanf("%s",msg.name);puts("input your user passwd");scanf("%s",msg.data);send(socketfd,&msg,sizeof(message),0);recv(socketfd,&msg,sizeof(message),0);printf("register :%s\n",msg.data);return;}voiddo_login(int socketfd){
message msg;bzero(&msg,sizeof(message));
msg.type = L;puts("input your usr name:");scanf("%s",msg.name);puts("input your passwd:");scanf("%s",msg.data);printf("login :%s :%s \n",msg.name,msg.data);send(socketfd,&msg,sizeof(msg),0);recv(socketfd,&msg,sizeof(msg),0);printf("login :%s\n",msg.data);if(msg.type == OK){dictionary(socketfd,msg.name);}return;}intdictionary(int socketfd,char* name){while(1){putchar(10);putchar(10);puts("****************************");printf(" HELLO %s\n",name);puts("* Welcom to login! *");puts("* please take a choose! *");puts("*1.query 2,history 3.back*");puts("****************************");putchar(10);putchar(10);int input ;puts("input a number in (1-3):");if(scanf("%d",&input)<=0){perror("error in scanf");setbuf(stdin,NULL);//clear input buffercontinue;}switch(input){case1:do_query(socketfd,name);break;case2:do_history(socketfd,name);break;case3:return0;}}return0;}voiddo_query(int socketfd,char*name){
message msg;char word[DATA_MAX]={0};// the word you want to query// char meaning[DATA_MAX] = {0}; // meaning of the word from server
msg.type = Q;puts("please input a word:");scanf("%s",word);strcpy(msg.name,name);strcpy(msg.data,word);printf("msg.name :%s msg.word :%s\n",msg.name,msg.data);send(socketfd,&msg,sizeof(msg),0);recv(socketfd,&msg,sizeof(msg),0);printf("%s : %s\n",name,msg.data);printf("Thanks for query ,bye!\n");return;}voiddo_history(int socketfd,char* name){
message msg;
msg.type = H;send(socketfd,&msg,sizeof(msg),0);return;}
dict.h
#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<sqlite3.h>#include<sys/socket.h>#include<netinet/in.h>#include<arpa/inet.h>#include<signal.h>#include<time.h>#include<strings.h>#include<string.h>#define NAME_MAX 16#define DATA_MAX 512 #define OK 0#define R 1 //user Register#define L 2 //user Login#define Q 3 //user query#define H 4 //history recordtypedefstruct{int type;char name[NAME_MAX];char data[DATA_MAX];}message;
PushWordToSQlite.c
#include<stdio.h>#include<stdlib.h>#include<sqlite3.h>#include<string.h>/*
*This code tries to push all data from dict.txt to sqlite3
*
*
*/#define WORD_MAX 64 #define SQ_CMD_MAX 512 * 2#define STRING_MAX 512
sqlite3*init_sqlite(void);char*init_word(char*data);intpush_word_into_sqlite(sqlite3*,char*);intmain(int argc,constchar*argv[]){
sqlite3 *data =NULL;
data =init_sqlite();//init sqlite and create a table if necessary
FILE *fp =NULL;if((fp =fopen("dict.txt","r"))==NULL){perror("fopen is error!");exit(0);}char temp[STRING_MAX]={0};char*p_word;while(fgets(temp,sizeof(temp),fp)!=NULL){
p_word =init_word(temp);//init the word that you want searchprintf("p_word****:%s\n",p_word);push_word_into_sqlite(data,p_word);}puts("insert the dictionary into sqlite has done!");return0;}
sqlite3*init_sqlite(void){char sqlcmd[SQ_CMD_MAX]={0};int result =0;char* errmsg;
sqlite3* data;if((result =sqlite3_open("my.db",&data))!= SQLITE_OK){printf("sqlite3_open result:%d\n",result);returnNULL;}sprintf(sqlcmd,"create table dict(word text primary key,meaning text not null)");if((result =sqlite3_exec(data,sqlcmd,NULL,NULL,&errmsg))!= SQLITE_OK){puts(errmsg);}return data;}/*
* Function : init_word
* it searches for ' and " from the string
* if it has ' or " ,add \ befor it
* return: a new string which has ' \'' or '\"'
*/char*init_word(char*data){char* new =NULL;char* temp;if((new =(char*)malloc(sizeof(char)* STRING_MAX))==NULL){puts("malloc error!");returnNULL;}
temp = new;while(*data){if(*data =='\''||*data =='\"'){*temp++='\\';}*temp++=*data++;}*temp =*data;//copy the '\0'return new;}intpush_word_into_sqlite(sqlite3 * data,char*p_word){char sqlcmd [SQ_CMD_MAX]={0};char word [WORD_MAX]={0};char meaning [STRING_MAX]={0};char* errmsg =NULL;char* temp = word;char* temp_p_word = p_word;while(*p_word !=' '){*temp++=*p_word++;}while(*p_word ==' ')
p_word++;
temp = meaning;while((*temp++=*p_word++)!='\0');printf("%s word:%s meaning:%s\n",__FUNCTION__,word,meaning);sprintf(sqlcmd,"insert into dict values('%s','%s')",word,meaning);puts(sqlcmd);if(sqlite3_exec(data,sqlcmd,NULL,NULL,&errmsg)!= SQLITE_OK){printf("insert into is error!:%s\n",errmsg);}else{printf("insert into success!\n");}printf("return from push_word_into_sqlite \n");free(temp_p_word);
temp_p_word =NULL;return0;}