这里写目录标题
- 服务器
-
- main.c
- server.c
- sqlite.c
- public.h
- 客户端
-
- main.c
- client.c
- sqlite.c
- public.h
服务器
main.c
#include "public.h"
int main(int argc, char const *argv[])
{
int sfd = 0;
if(NET_init(&sfd) == -1) printf("网络初始化失败");
if(sqlite3_init(&db) == -1) printf("数据库初始化失败");
if(wait_cli_link(sfd) == -1) printf("服务器出错");
return 0;
}
server.c
#include "public.h"
void handler(int sig)
{
while(waitpid(-1,NULL,0) >0);
}
int NET_init(int* sfd)
{
*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)))
{
ERR_MSG("setsockopt");
return -1;
}
struct sockaddr_in sin_addr;
sin_addr.sin_family = AF_INET;
sin_addr.sin_port = htons(8080);
sin_addr.sin_addr.s_addr = inet_addr("192.168.2.101");
if(bind(*sfd,(const struct sockaddr*)&sin_addr,sizeof(sin_addr))<0)
{
ERR_MSG("bind");
return -1;
}
if(listen(*sfd,BACKLOG) == -1)
{
ERR_MSG("listen");
return -1;
}
printf("网络初始化完成\r\n当前服务器>192.168.2.101:8080\r\n");
return 0;
}
int wait_cli_link(int sfd)
{
struct sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
int newfd = -1;
pid_t cli_pid = 0;
while(1)
{
newfd = accept(sfd,(struct sockaddr*)&addr,&addrlen);
if(newfd < 0)
{
ERR_MSG("accept");
return -1;
}
cli_pid = fork();
if(cli_pid == 0)
{
close(sfd);
printf("客户端已连接>%s:%d\r\n",inet_ntoa(addr.sin_addr),ntohs(addr.sin_port));
cli_interactive(newfd,addr);
printf("客户端已断开>%s:%d\r\n",inet_ntoa(addr.sin_addr),ntohs(addr.sin_port));
exit(0);
}
}
}
void cli_interactive(int fd,struct sockaddr_in cli_addr)
{
time_t sys_t = time(NULL);
struct tm *fomattime = localtime(&sys_t);
char time_str[50] = {0};
char str[1024];
char buf[1024];
char word[25] = {0};
cli_date_t client;
bzero(client.user, sizeof(client.user));
bzero(client.pawd, sizeof(client.pawd));
int res;
char flag = 0;
while(1)
{
switch(flag)
{
case 0:{
res = recv(fd,str,sizeof(str),0);
analysis_login_json(str,&client);
if(insert_date(&db,"registry",client) != 0)
{
switch(check_user_pawd(client))
{
case 0:{
if(insert_date(&db,"login",client) != 0)
{
TCP_printf(fd,"The account has been logged in\r\n");
}
else
{
TCP_printf(fd,"Login succeeded\r\n");
flag = 1;
}
}break;
case 1:{
TCP_printf(fd,"password error\tr\n");
}break;
case 2:{
TCP_printf(fd,"user error\r\n");
}break;
}
}
else
{
insert_date(&db,"login",client);
TCP_printf(fd,"The username does not exist, it is now registered and logged in\r\n");
flag = 1;
}
}break;
case 1:{
res = recv(fd,str,sizeof(str),0);
if(res == 0)
{
if(delete_login(client.user) == -1) printf("delete login fail!!\r\n");
printf("The client close >%s:%d\r\n",inet_ntoa(cli_addr.sin_addr),ntohs(cli_addr.sin_port));
exit(0);
}
if(analysis_word_json(str,word) == -1)
{
TCP_printf(fd,"405 Not find\r\n");
}else
{
if(check_word(word,buf) == -1)
{
printf("404 Not find >> %s\r\n",word);
}
else
{
GET_TIME(time_str);
sprintf(str,"{\"word\":%s,\"translate\":%s,\"time\":%s}",word,buf,time_str);
TCP_printf(fd,"%s\r\n",str);
}
}
}break;
}
}
}
void analysis_login_json(const char* str,cli_date_t* client)
{
char* p1 = NULL;
int i;
p1 = strstr(str,"\"user\":");
p1+=7;
for(i=0;*(p1+i)!=',';i++)
{
client->user[i] = *(p1+i);
}
p1 = strstr(str,"\"pawd\":");
p1+=7;
for(i=0;*(p1+i)!='}';i++)
{
client->pawd[i] = *(p1+i);
}
}
int analysis_word_json(const char* str,char* word)
{
char* p1 = NULL;
int i;
bzero(word,sizeof(word));
if((p1 = strstr(str,"\"word\":")) == NULL)
{
return -1;
}
p1+=7;
for(i=0;*(p1+i)!='}';i++)
{
word[i] = *(p1+i);
}
}
void TCP_printf(int sockfd,char* fmt,...)
{
char buf[1024];
char buf_tim[50];
time_t sys_t;
struct tm *fomattime;
va_list ap;
va_start(ap,fmt);
vsprintf(buf,fmt,ap);
va_end(ap);
GET_TIME(buf_tim);
send(sockfd,buf,strlen(buf),0);
}
sqlite.c
#include "public.h"
sqlite3 *db;
void parsing_file(const char* date,date_t* dict)
{
char str[100] = {0};
char buf[100] = {0};
strcpy(str,date);
char* p = str;
int i = 0;
int cnt = 0;
for(i=0;cnt!=2;i++)
{
buf[i] = *(p+i);
if(*(p+i) == ' ') cnt++;
}
strcpy(dict->word,buf);
strcpy(dict->translate,(p+i));
dict->translate[strlen(dict->translate)-1] = '\0';
}
int sqlite3_init(sqlite3 **db)
{
char str[1024] = {0};
char SQ_name[25] = {0};
char* err_msg = NULL;
int menu_num = 0;
char ch = 0;
int num = 0;
FILE* fp = NULL;
int ret = 0;
int cnt = 0;
date_t dict;
if((fp=fopen("./dict.txt","r")) == NULL)
{
perror("open file");
return -1;
}
if(sqlite3_open("sq.db",&*db) != SQLITE_OK)
{
printf("sqlite3_open : %s\r\n",err_msg);
return -1;
}
sprintf(str,"create table if not exists Registry (user char PRIMARY KEY, pawd char)");
if(sqlite3_exec(*db,str,NULL,NULL,&err_msg) != SQLITE_OK)
{
printf("sqlite3_exec : %s\r\n",err_msg);
return -1;
}
if(sqlite3_exec(*db,"DROP TABLE login",NULL,NULL,&err_msg) != SQLITE_OK)
{
printf("sqlite3_exec : %s\r\n",err_msg);
}
sprintf(str,"create table if not exists login (user char PRIMARY KEY, pawd char)");
if(sqlite3_exec(*db,str,NULL,NULL,&err_msg) != SQLITE_OK)
{
printf("sqlite3_exec : %s\r\n",err_msg);
return -1;
}
printf("Do you want to reload the dictionary? [y/n]:");
scanf("%c",&ch);
if(ch == 'y')
{
if(sqlite3_exec(*db,"DROP TABLE dict",NULL,NULL,&err_msg) != SQLITE_OK)
{
printf("sqlite3_exec : %s\r\n",err_msg);
}
sprintf(str,"create table if not exists dict (word char, translate char)");
if(sqlite3_exec(*db,str,NULL,NULL,&err_msg) != SQLITE_OK)
{
printf("sqlite3_exec : %s\r\n",err_msg);
return -1;
}
do
{
bzero(str,sizeof(str));
fgets(str,sizeof(str),fp);
ret = strlen(str);
if(ret == 0) break;
parsing_file(str,&dict);
sprintf(str,"insert into dict values (\"%s\",\"%s\")\r\n",dict.word,dict.translate);
if(sqlite3_exec(*db,str,NULL,NULL,&err_msg) != SQLITE_OK)
{
printf("sqlite3_exec : %s\r\n",err_msg);
printf("str: %s\r\n",str);
return -1;
}
printf("\r%.0f%%",(++num/7987.0)*100.0);
}while(ret != 0);
printf("\n词典导入完成\r\n");
}
printf("词典已打开\r\n");
fclose(fp);
return 0;
}
int insert_date(sqlite3 **db,const char* name,cli_date_t client)
{
char* err_msg = NULL;
char str[256] = {0};
sprintf(str,"insert into %s values ('%s','%s')\r\n",name,client.user,client.pawd);
if(sqlite3_exec(*db,str,NULL,NULL,&err_msg) != SQLITE_OK)
{
printf("sqlite3_exec : %s\r\n",err_msg);
return -1;
}
return 0;
}
int check_user_pawd(cli_date_t client)
{
char* err_msg = NULL;
char str[100] = {0};
char** pres = NULL;
int i = 1;
int row,column;
sprintf(str,"select * from registry WHERE user = '%s'",client.user);
if(sqlite3_get_table(db,str,&pres,&row,&column,&err_msg) != SQLITE_OK)
{
printf("sqlite3_get_table : %s\r\n",err_msg);
return -1;
}
if(strcmp(client.user, pres[2]) != 0)
{
sqlite3_free_table(pres);
return -1;
}
if(strcmp(client.pawd, pres[3]) != 0)
{
sqlite3_free_table(pres);
return 1;
}
sqlite3_free_table(pres);
return 0;
}
int check_word(const char* word,char* buf)
{
char* err_msg = NULL;
char str[100] = {0};
char** pres = NULL;
int i = 1;
int row,column;
sprintf(str,"select * from dict");
if(sqlite3_get_table(db,str,&pres,&row,&column,&err_msg) != SQLITE_OK)
{
printf("sqlite3_get_table : %s\r\n",err_msg);
return -1;
}
for(i=0; i<row*2; i+=2)
{
if(strstr(pres[i+2],word) != NULL)
{
strcpy(buf,pres[i+3]);
sqlite3_free_table(pres);
return 0;
}
}
sqlite3_free_table(pres);
return -1;
}
int delete_login(const char* user)
{
char* err_msg = NULL;
char str[50] = {0};
sprintf(str,"DELETE FROM login WHERE user='%s'",user);
if(sqlite3_exec(db,str,NULL,NULL,&err_msg) != SQLITE_OK)
{
printf("sqlite3_exec : %s\r\n",err_msg);
return -1;
}
return 0;
}
public.h
#ifndef __PUBLIC_H_
#define __PUBLIC_H_
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define R O_RDONLY
#define W O_WRONLY|O_CREAT|O_TRUNC
#define A O_WRONLY|O_CREAT|O_APPEND
#define Rp O_RDWR
#define Wp O_RDWR|O_CREAT|O_TRUNC
#define Ap O_RDWR|O_CREAT|O_APPEND
#define IP "192.168.2.52"
#define PORT 8080
#define BACKLOG 128
#define ERR_MSG(msg) do{\
fprintf(stderr, "Line:%d :%s %s\n",__LINE__, __FILE__,__func__);\
perror(msg);\
}while(0)
#define GET_TIME(str) do{\
time_t sys_t = time(NULL);\
struct tm *fomattime = localtime(&sys_t);\
sprintf(str,"[%.4d-%.2d-%.2d %.2d:%.2d:%.2d]",\
fomattime->tm_year+1900,fomattime->tm_mon+1,fomattime->tm_mday,\
fomattime->tm_hour,fomattime->tm_min,fomattime->tm_sec);\
}while(0)
#pragma pack(1)
typedef struct
{
char word[50];
char translate[50];
}date_t;
typedef struct
{
int line;
char user[50];
char pawd[50];
}cli_date_t;
extern sqlite3 *db;
int NET_init(int* sfd);
void TCP_printf(int sockfd,char* fmt,...);
int wait_cli_link(int sfd);
void cli_interactive(int fd,struct sockaddr_in cli_addr);
void analysis_login_json(const char* str,cli_date_t* client);
int analysis_word_json(const char* str,char* word);
int sqlite3_init(sqlite3 **db);
int insert_date(sqlite3 **db,const char* name,cli_date_t client);
int check_user_pawd(cli_date_t client);
int check_word(const char* word,char* buf);
int delete_login(const char* user);
#endif
客户端
main.c
#include "public.h"
int main(int argc, char const *argv[])
{
time_t sys_t = time(NULL);
struct tm *fomattime = localtime(&sys_t);
char time_str[50] = {0};
char ch = 0;
int sfd = 0;
pthread_t Rx_server_pid;
if(NET_init(&sfd) == -1) printf("网络初始化失败");
if(create_server_line(&Rx_server_pid,&sfd) == -1) printf("客户端接收线程初始化失败");
if(sqlite3_init(&db) == -1) printf("数据库初始化失败");
Client_Operations(sfd);
return 0;
}
client.c
#include "public.h"
char cil_flig = 0;
cli_date_t cilent;
int NET_init(int* cfd)
{
*cfd = socket(AF_INET, SOCK_STREAM, 0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
struct sockaddr_in cin_addr;
cin_addr.sin_family = AF_INET;
cin_addr.sin_port = htons(8080);
cin_addr.sin_addr.s_addr = inet_addr("192.168.2.101");
if(connect(*cfd,(const struct sockaddr*)&cin_addr,sizeof(cin_addr)) == -1)
{
ERR_MSG("connect");
return -1;
}
printf("网络初始化完成\r\n当前已链接服务器>192.168.2.101:8080\r\n");
return 0;
}
int create_server_line(pthread_t* Rx_pid,int* sfd)
{
if(pthread_create(Rx_pid, NULL,TCP_server_Rx,sfd) != 0)
{
perror("pthread task1");
return -1;
}
return 0;
}
void TCP_printf(int sockfd,char* fmt,...)
{
char buf[1024];
char buf_tim[50];
time_t sys_t;
struct tm *fomattime;
va_list ap;
va_start(ap,fmt);
vsprintf(buf,fmt,ap);
va_end(ap);
GET_TIME(buf_tim);
send(sockfd,buf,strlen(buf),0);
}
int server_josn(const char* str,record_date_t* record)
{
char* p1 = NULL;
char buf[50] = {0};
int i;
p1 = strstr(str, "\"word\":");
p1+=7;
for(i=0;*(p1+i)!=',';i++)
{
record->date[i] = *(p1+i);
}
p1 = strstr(str, "\"translate\":");
p1+=12;
for(i=0;*(p1+i)!=',';i++)
{
buf[i] = *(p1+i);
}
strcat(record->date,buf);
p1 = strstr(str, "\"time\":");
p1+=7;
for(i=0;*(p1+i)!='}';i++)
{
record->time[i] = *(p1+i);
}
}
void menu(void)
{
printf("********************************************\r\n");
printf("1. 登录/注册 | 2. 查询 | 3.历史记录| 4. 退出客户端\r\n");
printf("********************************************\r\n");
}
void* TCP_server_Rx(void* input)
{
time_t sys_t = time(NULL);
struct tm *fomattime = localtime(&sys_t);
char time_str[50] = {0};
record_date_t record;
int res = 0;
int sfd = *(int*) input;
char buf1[256] = {0};
char buf2[256] = {0};
printf("子线程启动\r\n");
while(1)
{
res = recv(sfd,buf1,sizeof(buf1),0);
if(res == -1) continue;
switch(cil_flig)
{
case 0:{
if(strstr(buf1,"Login succeeded") != NULL)
{
GET_TIME(time_str);
sprintf(buf2,"%s>>%s\r\n",time_str,buf1);
printf("%s\r\n",buf2);
sqlite3_user_create(&db,cilent);
cil_flig = 1;
}else if(strstr(buf1,"The username does not exist, it is now registered and logged in") != NULL)
{
GET_TIME(time_str);
sprintf(buf2,"%s>>%s\r\n",time_str,buf1);
printf("%s\r\n",buf2);
cil_flig = 1;
}else
{
printf("server connection failed\r\n");
}
}break;
case 1:{
bzero(record.date,sizeof(record.date));
bzero(record.time,sizeof(record.time));
server_josn(buf1,&record);
insert_date(&db,cilent.user,record);
GET_TIME(time_str);
sprintf(buf2,"%s>>%s\r\n",time_str,record.date);
printf("%s",buf2);
}break;
}
if(res == 0)
{
printf("服务器下线\r\n");
}
}
printf("\r\n");
}
int Client_Operations(int sfd)
{
int menu_num = 0;
int res = 0;
char buf1[256] = {0};
char buf2[256] = {0};
while(1)
{
menu();
scanf("%d", &menu_num);
switch(menu_num)
{
case 1:{
printf("Please enter your user: ");
scanf("\r\n%s",cilent.user);
printf("Please enter your password: ");
scanf("\r\n%s",cilent.pawd);
sprintf(buf1,"{\"user\":%s,\"pawd\":%s}",cilent.user,cilent.pawd);
TCP_printf(sfd,"%s",buf1);
}break;
case 2:{
printf("Please enter the word you want to query:");
scanf("\r\n%s",buf1);
sprintf(buf2,"{\"word\":%s}",buf1);
TCP_printf(sfd,"%s",buf2);
}break;
case 3:{
sqlite3_History(cilent.user);
}break;
case 4:{
}break;
}
}
}
sqlite.c
#include "public.h"
sqlite3 *db;
int sqlite3_init(sqlite3 **db)
{
char str[1024] = {0};
char SQ_name[25] = {0};
char* err_msg = NULL;
int menu_num = 0;
char ch = 0;
int num = 0;
int ret = 0;
int cnt = 0;
date_t dict;
if(sqlite3_open("user.db",&*db) != SQLITE_OK)
{
printf("sqlite3_open : %s\r\n",err_msg);
return -1;
}
printf("用户数据库已打开\r\n");
return 0;
}
int sqlite3_user_create(sqlite3 **db,cli_date_t cli)
{
char* err_msg = NULL;
char str[1024] = {0};
sprintf(str,"create table if not exists '%s' ( date char , time char)",cli.user);
if(sqlite3_exec(*db,str,NULL,NULL,&err_msg) != SQLITE_OK)
{
printf("sqlite3_exec : %s\r\n",err_msg);
return -1;
}
printf("已创建/打开用户记录\r\n");
}
int insert_date(sqlite3 **db,const char* name,record_date_t record)
{
char* err_msg = NULL;
char str[256] = {0};
sprintf(str,"insert into '%s' values ('%s','%s')\r\n",name,record.date,record.time);
if(sqlite3_exec(*db,str,NULL,NULL,&err_msg) != SQLITE_OK)
{
printf("sqlite3_exec : %s\r\n",err_msg);
return -1;
}
return 0;
}
int check_word(const char* word,char* buf)
{
char* err_msg = NULL;
char str[100] = {0};
char** pres = NULL;
int i = 1;
int row,column;
sprintf(str,"select * from dict");
if(sqlite3_get_table(db,str,&pres,&row,&column,&err_msg) != SQLITE_OK)
{
printf("sqlite3_get_table : %s\r\n)",err_msg);
return -1;
}
printf("row:%d column:%d\r\n",row,column);
for(i = 0; i < row*2; i+=2)
{
if(strstr(*(pres+(i+2)),word) == NULL) continue;
strcpy(buf,*(pres+(i+3)));
return 0;
}
return -1;
}
void sqlite3_History(const char* name)
{
char* err_msg = NULL;
char str[100] = {0};
char** pres = NULL;
int i = 0;
int z = 0;
int row,column;
sprintf(str,"select * from '%s'",name);
if(sqlite3_get_table(db,str,&pres,&row,&column,&err_msg) != SQLITE_OK)
{
printf("sqlite3_get_table : %s\r\n)",err_msg);
return ;
}
printf("row:%d column:%d\r\n",row,column);
for(i = 0; i < row*2; i+=2)
{
if(i==0) printf("----------%s----------|----------%s----------\r\n",pres[i+1],pres[i]);
else printf("%s\t| %s\r\n",pres[i+1],pres[i]);
}
}
public.h
#ifndef __PUBLIC_H_
#define __PUBLIC_H_
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define R O_RDONLY
#define W O_WRONLY|O_CREAT|O_TRUNC
#define A O_WRONLY|O_CREAT|O_APPEND
#define Rp O_RDWR
#define Wp O_RDWR|O_CREAT|O_TRUNC
#define Ap O_RDWR|O_CREAT|O_APPEND
#define IP "192.168.2.52"
#define PORT 8080
#define BACKLOG 128
#define ERR_MSG(msg) do{\
fprintf(stderr, "Line:%d :%s %s\n",__LINE__, __FILE__,__func__);\
perror(msg);\
}while(0)
#define GET_TIME(str) do{\
time_t sys_t = time(NULL);\
struct tm *fomattime = localtime(&sys_t);\
sprintf(str,"[%.4d-%.2d-%.2d %.2d:%.2d:%.2d]",\
fomattime->tm_year+1900,fomattime->tm_mon+1,fomattime->tm_mday,\
fomattime->tm_hour,fomattime->tm_min,fomattime->tm_sec);\
}while(0)
#pragma pack(1)
typedef struct
{
char word[50];
char translate[50];
}date_t;
typedef struct
{
char user[50];
char pawd[50];
}cli_date_t;
typedef struct
{
char time[50];
char date[50];
}record_date_t;
extern sqlite3 *db;
int NET_init(int* sfd);
void TCP_printf(int sockfd,char* fmt,...);
void* TCP_server_Rx(void* input);
int create_server_line(pthread_t* Rx_pid,int* sfd);
int Client_Operations(int sfd);
int server_josn(const char* str,record_date_t* record);
int sqlite3_init(sqlite3 **db);
int insert_date(sqlite3 **db,const char* name,record_date_t record);
int sqlite3_user_create(sqlite3 **db,cli_date_t cli);
void sqlite3_History(const char* name);
#endif