本项目功能描述
表结构
usr表
name(primary key) passwd
record表
name date word
单词表
使用文件查询,如使用数据库的话代码会更简单
消息结构体
#define N 32
#define R 1 // user - register
#define L 2 // user - login
#define Q 3 // user - query
#define H 4 // user - history
// 定义通信双方的信息结构体
typedef struct {
int type;
char name[N];
char data[256];
}MSG;
连接服务端
一级菜单提示:1注册 2登录 3退出
如果登录成功,跳转至二级菜单
二级菜单提示:1查询 2历史 3退出 #返回上级菜单
分别实现对应功能
#include
#include /* See NOTES */
#include
#include
#include
#include
#include
#include
#define N 32
#define R 1 //注册
#define L 2 //登录
#define Q 3 //查询单词
#define H 4 //历史
typedef struct
{
int type;
char name[N];
char data[256]; //password or word
int root_flag;
}MSG_t;
int do_register(int sockfd, MSG_t *msg);
int do_login(int sockfd, MSG_t *msg);
int do_query(int sockfd, MSG_t *msg);
int do_history(int sockfd, MSG_t *msg);
int main(int argc, char *argv[])
{
int sockfd;
int n;
struct sockaddr_in addr;
MSG_t msg;
bzero(&msg, sizeof(MSG_t));
//参数检查
if(argc != 3)
{
printf("%s \n",argv[0]);
exit(0);
}
//创建socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
perror("socket");
exit(0);
}
//连接connect
addr.sin_family = AF_INET;
addr.sin_port = htons(atoi(argv[2]));
addr.sin_addr.s_addr = inet_addr(argv[1]);
if(connect(sockfd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("connect");
exit(0);
}
while(1)
{
printf("***********************************\n");
printf("*1:register 2:login 3:quit *\n");
printf("***********************************\n");
printf("Please choose:");
scanf("%d", &n);
switch(n)
{
case 1:
do_register(sockfd, &msg);
break;
case 2:
if(do_login(sockfd, &msg) == 1)
{
goto next;
}
break;
case 3:
close(sockfd);
exit(0);
}
}
next:
while(1)
{
printf("***********************************\n");
printf("*1:search 2:history 3:quit *\n");
printf("***********************************\n");
printf("Please choose:");
scanf("%d", &n);
switch(n)
{
case 1:
do_query(sockfd, &msg);
break;
case 2:
do_history(sockfd, &msg);
break;
case 3:
close(sockfd);
exit(0);
case 4:
break;
}
}
return 0;
}
int do_register(int sockfd,MSG_t *msg)
{
msg->type = R;
printf("Input name:");
scanf("%s", msg->name);
getchar();
printf("Input passwd:");
scanf("%s", msg->data);
if(write(sockfd, msg, sizeof(MSG_t)) < 0)
{
perror("write");
return -1;
}
if(read(sockfd, msg, sizeof(MSG_t)) < 0)
{
perror("read");
return -1;
}
//成功或者用户已经注册
printf("%s\n", msg->data);
return 0;
}
int do_login(int sockfd, MSG_t *msg)
{
msg->type = L;
printf("Input name:");
scanf("%s", msg->name);
getchar();
if(strcmp(msg->name, "root") == 0)
{
msg->root_flag = 1;
}
else
{
msg->root_flag = 0;
}
printf("Input passwd:");
scanf("%s", msg->data);
if(send(sockfd, msg, sizeof(MSG_t), 0) < 0)
{
perror("write");
return -1;
}
if(recv(sockfd, msg, sizeof(MSG_t), 0) < 0)
{
perror("read");
return -1;
}
if(strncmp(msg->data, "OK", 3) == 0)
{
printf("Login success.\n");
return 1;
}
else
{
printf("%s\n",msg->data);
}
return 0;
}
int do_query(int sockfd,MSG_t *msg)
{
msg->type = Q;
printf("--------------------------\n");
while(1)
{
printf("Input word:");
scanf("%s",msg->data);
getchar();
//输入#返回上级菜单
if(strncmp(msg->data, "#", 1) == 0)
{
break;
}
if(write(sockfd, msg, sizeof(MSG_t)) < 0)
{
perror("write");
return -1;
}
if(read(sockfd, msg, sizeof(MSG_t)) < 0)
{
perror("read");
return -1;
}
printf("%s\n",msg->data);
}
return 0;
}
int do_history(int sockfd, MSG_t *msg)
{
msg->type = H;
if(write(sockfd, msg, sizeof(MSG_t)) < 0)
{
perror("write");
return -1;
}
while(1)
{
if(read(sockfd, msg, sizeof(MSG_t)) < 0)
{
perror("read");
return -1;
}
if(msg->data[0] == '0')
break;
printf("%s\n",msg->data);
}
return 0;
}
表设计,创建usr表用于管理登录,创建record表用于记录查询历史
linux@linux:~/Desktop/dictionary$ sqlite3 my.db
SQLite version 3.8.2 2013-12-06 14:53:30
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .database
seq name file
--- --------------- ----------------------------------------------------------
0 main /home/linux/Desktop/dictionary/my.db
sqlite> create table usr(name text, passwd text);
sqlite> create table record(name text, date text, word text);
sqlite> :q
代码设计,接收客户端后fork出子进程,用于多客户端连接使用
#include
#include /* See NOTES */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define N 32
#define R 1 //register
#define L 2 //login
#define Q 3 //query
#define H 4 //history
#define DATABSAE "my.db"
typedef struct
{
int type;
char name[N];
char data[256]; //password or word
int root_flag;
}MSG_t;
int do_client(int acceptfd, sqlite3 *db);
void do_register(int acceptfd, MSG_t *msg, sqlite3 *db);
int do_login(int acceptfd, MSG_t *msg, sqlite3 *db);
int do_query(int acceptfd, MSG_t *msg, sqlite3 *db);
int do_history(int acceptfd, MSG_t *msg,sqlite3 *db);
int history_callback(void *arg, int f_num, char **f_value, char **f_name);
int do_searchword(int acceptfd, MSG_t *msg, char word[]);
int get_date(char *date);
int main(int argc, char *argv[])
{
int sockfd;
int acceptfd;
struct sockaddr_in serveraddr;
MSG_t msg;
sqlite3 *db;
pid_t pid;
bzero(&msg, sizeof(MSG_t));
//参数检查
if(argc != 3)
{
printf("%s \n",argv[0]);
exit(0);
}
//打开数据库
if(sqlite3_open(DATABSAE, &db) != SQLITE_OK)
{
printf("%s\n",sqlite3_errmsg(db));
exit(0);
}
else
{
printf("open DATABSAE success.\n");
}
//创建socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0)
{
perror("socket");
exit(0);
}
//绑定bind
bzero(&serveraddr,sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(atoi(argv[2]));
serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
if(bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
{
perror("bind");
exit(0);
}
//监听listen
if(listen(sockfd, 5) < 0)
{
printf("listen");
exit(0);
}
//处理僵尸进程
signal(SIGCHLD, SIG_IGN);
while(1)
{
if((acceptfd = accept(sockfd, NULL, NULL)) < 0)
{
perror("accept");
exit(0);
}
if((pid = fork()) < 0)
{
perror("fork");
exit(0);
}
else if(pid == 0) //儿子进程
{
//处理客户端具体的消息
close(sockfd);
do_client(acceptfd, db);
}
else //父进程
{
close(acceptfd);
}
}
return 0;
}
int do_client(int acceptfd, sqlite3 *db)
{
MSG_t msg;
while(read(acceptfd, &msg, sizeof(msg)) > 0)
{
printf("type:%d\n",msg.type);
switch(msg.type)
{
case R:
do_register(acceptfd, &msg, db);
break;
case L:
do_login(acceptfd, &msg, db);
break;
case Q:
do_query(acceptfd, &msg, db);
break;
case H:
do_history(acceptfd, &msg, db);
break;
default:
printf("Invalid data msg.\n");
}
}
printf("client exit.\n");
close(acceptfd);
exit(0);
return 0;
}
void do_register(int acceptfd, MSG_t *msg, sqlite3 *db)
{
char * errmsg;
char sql[128];
sprintf(sql, "insert into usr values('%s', %s);", msg->name, msg->data);
printf("%s\n", sql);
if(sqlite3_exec(db,sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("%s\n", errmsg);
strcpy(msg->data, "usr name already exist.");
}
else
{
printf("client register ok!\n");
strcpy(msg->data, "OK!");
}
if(send(acceptfd, msg, sizeof(MSG_t), 0) < 0)
{
perror("fail to send");
return ;
}
return ;
}
int do_login(int acceptfd, MSG_t *msg , sqlite3 *db)
{
char sql[128] = {};
char *errmsg;
int nrow;
int ncloumn;
char **resultp;
sprintf(sql, "select * from usr where name = '%s' and passwd = '%s';", msg->name, msg->data);
printf("%s\n", sql);
if(sqlite3_get_table(db, sql, &resultp, &nrow, &ncloumn, &errmsg)!= SQLITE_OK)
{
printf("%s\n", errmsg);
return -1;
}
else
{
printf("get_table ok!\n");
}
// 查询成功,数据库中拥有此用户
if(nrow == 1)
{
strcpy(msg->data, "OK");
send(acceptfd, msg, sizeof(MSG_t), 0);
return 1;
}
if(nrow == 0) // 密码或者用户名错误
{
strcpy(msg->data,"usr/passwd wrong.");
send(acceptfd, msg, sizeof(MSG_t), 0);
}
return 0;
}
int do_searchword(int acceptfd, MSG_t *msg, char word[])
{
FILE * fp;
int len = 0;
char temp[512] = {};
int result;
char *p;
//打开文件,读取文件,进行比对
if((fp = fopen("dict.txt", "r")) == NULL)
{
perror("fail to fopen.\n");
strcpy(msg->data, "Failed to open dict.txt");
send(acceptfd, msg, sizeof(MSG_t), 0);
return -1;
}
//打印出,客户端要查询的单词
len = strlen(word);
printf("%s , len = %d\n", word, len);
//读文件,来查询单词
while(fgets(temp, 512, fp) != NULL) //每次读取512字节
{
// printf("temp:%s\n", temp);
// abandon ab
result = strncmp(temp,word,len);
if(result < 0) //不相等
{
continue;
}
if(result > 0 || ((result == 0) && (temp[len]!=' '))) //第一个字符串>第二个或者两者相同但后面不是空格,表示在注释里出现的单词
{
break;
}
// 表示找到了,查询的单词result == 0 且 temp[len] == ' '。
p = temp + len; // abandon v.akdsf dafsjkj
// printf("found word:%s\n", p);
while(*p == ' ')
{
p++;
}
// 找到了注释,跳跃过所有的空格
strcpy(msg->data, p);
printf("found word:%s\n", msg->data);
// 注释拷贝完毕之后,应该关闭文件
fclose(fp);
return 1;
}
fclose(fp);
return 0;
}
int get_date(char *date)
{
time_t t;
struct tm *tp;
time(&t);
//进行时间格式转换
tp = localtime(&t);
sprintf(date, "%d-%d-%d %d:%d:%d", tp->tm_year + 1900, tp->tm_mon+1, tp->tm_mday,
tp->tm_hour, tp->tm_min , tp->tm_sec);
printf("get date:%s\n", date);
return 0;
}
int do_query(int acceptfd, MSG_t *msg , sqlite3 *db)
{
char word[64];
int found = 0;
char date[128] = {};
char sql[128] = {};
char *errmsg;
//拿出msg结构体中,要查询的单词
strcpy(word, msg->data);
found = do_searchword(acceptfd, msg, word);
printf("查询一个单词完毕.\n");
// 表示找到了单词,那么此时应该将 用户名,时间,单词,插入到历史记录表中去。
if(found == 1)
{
// 需要获取系统时间
get_date(date);
sprintf(sql, "insert into record values('%s', '%s', '%s')", msg->name, date, word);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("%s\n", errmsg);
return -1;
}
else
{
printf("Insert record done.\n");
}
}
else //表示没有找到
{
strcpy(msg->data, "Not found!");
}
// 将查询的结果,发送给客户端
send(acceptfd, msg, sizeof(MSG_t), 0);
return 0;
}
// 得到查询结果,并且需要将历史记录发送给客户端
int history_callback(void* arg,int f_num,char** f_value,char** f_name)
{
// record , name , date , word
int acceptfd;
MSG_t msg;
acceptfd = *((int *)arg);
sprintf(msg.data, "%s , %s", f_value[1], f_value[2]);
send(acceptfd, &msg, sizeof(MSG_t), 0);
return 0;
}
int do_history(int acceptfd, MSG_t *msg, sqlite3 *db)
{
char sql[128] = {};
char *errmsg;
if(msg->root_flag == 1)
{
sprintf(sql, "select * from record");
}
else
{
sprintf(sql, "select * from record where name = '%s'", msg->name);
}
//查询数据库
if(sqlite3_exec(db, sql, history_callback,(void *)&acceptfd, &errmsg)!= SQLITE_OK)
{
printf("%s\n", errmsg);
}
else
{
printf("Query record done.\n");
}
// 所有的记录查询发送完毕之后,给客户端发出一个结束信息
msg->data[0] = '\0';
send(acceptfd, msg, sizeof(MSG_t), 0);
return 0;
}
效果