一、项目要求:
1. 登录注册功能,不能重复登录,重复注册
2. 单词查询功能
3. 历史记录功能,存储单词,意思,以及查询时间
4. 基于TCP,支持多客户端连接
5. 采用数据库保存用户信息与历史记录
格式要求: 1. main函数只跑逻辑,不允许跑功能代码 2. 功能代码封装成函数
二、流程示意图
服务器:
客户端:
三、实现代码
server.c代码部分:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sqlite3.h>
#include <signal.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define DATABASE "my.db"
#define N 16
enum CMD{
R=1,
L,
Q,
H
};
typedef struct sockaddr SA;
typedef struct
{
int type;
char name[N];
char data[256]; // password or word
} MSG;
static sqlite3 *db; //数据库句柄指针
void do_register(int connfd, MSG *pbuf) //用户注册模块
{
char sqlstr[128];
char *errmsg;
sprintf(sqlstr, "insert into usr values ('%s', '%s')", pbuf->name, pbuf->data);
printf("%s\n", sqlstr);
if (sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg) != SQLITE_OK) //执行SQL语句并处理结果
{
sqlite3_free(errmsg);
sprintf(pbuf->data, "user %s already exist!!!", pbuf->name);
}
else
{
strncpy(pbuf->data, "OK", 256);
}
send(connfd, pbuf, sizeof(MSG), 0);
}
void do_login(int connfd, MSG *pbuf) //用户登录模块
{
char sqlstr[128]; //拼接后的查询语句
char *errmsg, **result; //存放错误信息和查询到的结果
int nrow, ncolumn; //查询到的行数和列数
sprintf(sqlstr, "select * from usr where name = '%s' and pass = '%s'", pbuf->name, pbuf->data);
if (sqlite3_get_table(db, sqlstr, &result, &nrow, &ncolumn, &errmsg) != SQLITE_OK)
{
printf("error : %s\n", errmsg);
sqlite3_free(errmsg);
}
if (nrow == 0) //查询到的条数为0条
{
strncpy(pbuf->data, "name or password is wrong!!!", 256);
}
else //查询到的所需信息
{
strncpy(pbuf->data, "OK", 256);
}
send(connfd, pbuf, sizeof(MSG), 0);
sqlite3_free_table(result);
return;
}
#if 0
int do_searchword(int connfd, MSG *pbuf) //查询单词模块调用的查询单词函数方式一
{
FILE *fp;
char line[300];
char *p;
int len, result;
len = strlen(pbuf->data); //要查询的单词长度
if ((fp = fopen("dict.txt", "r")) == NULL) //打开保存单词文件
{
strcpy(pbuf->data, "dict on server can't be opened :(");
send(connfd, pbuf, sizeof(MSG), 0);
return 0;
}
printf("query word is %s\n", pbuf->data); //打印一下客户端要查询的单词
while (fgets(line, 300, fp) != NULL) //读文件,每次读300个字符存放到字符数组line中,来查询单词
{
result = strncmp(pbuf->data, line, len); //比较一下要查询的单词和line中读取到的文件中每行的单词信息进行比较,比较的长度大小为要查询的单词大小
if (result > 0) continue; //大于0,说明要查询的单词大小大于读取的单词大小,则继续下一行,比如abandon ,查询abc,则跳入下一行
if (result < 0 || line[len] != ' ') break;//小于0,则说明要查找的单词小于读取的单词大小,但之前的都已比较完毕,则查询失败。不是空格也不行,比如查询ab,而读取的是abandon.
p = line + len; //指针指到单词末尾
while (*p == ' ') p++; //指针移动到注释之前
strcpy(pbuf->data, p); //拷贝单词注释
fclose(fp); //
return 1;
}
fclose(fp);
return 0;
}
#else
int do_searchword(int connfd, MSG *pbuf) //查询单词模块调用的查询单词函数方式二
{
char buf[128] = {0};
printf("query word is %s\n", pbuf->data);
sprintf(buf, "select * from dic where word='%s'", pbuf->data);
char *errmsg;
char **resultp;
int n_row, n_cloum;
if (sqlite3_get_table(db, buf, &resultp, &n_row, &n_cloum, &errmsg) != SQLITE_OK)
{
printf("err:%s\n", errmsg);
return -1;
}
if(n_row != 1)
{
return 0;
}
char **p;
p = resultp + n_cloum;
// printf("data=%s\n", p[1]);
strcpy(pbuf->data, p[1]);
return 1;
}
#endif
void get_date(char date[]) //获取系统时间
{
time_t t;
struct tm *tp;
time(&t); //获取到一个时间秒数
tp = localtime(&t);//进行时间格式转换
/*
//字符串拼接
sprintf(date, "%d-%02d-%02d %02d:%02d:%02d", tp->tm_year+1900,
tp->tm_mon+1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
*/
strftime(date, 64, "%Y-%m-%d %H:%M:%S", tp);
return;
}
void do_query(int connfd, MSG *pbuf) //查询单词模块
{
char sqlstr[128], *errmsg;
int len, result, found = 0;
char date[64], word[64];
strcpy(word, pbuf->data); //拿出pbuf结构体中要查询的单词
found = do_searchword(connfd, pbuf); //调用do_searchword函数模块去查询
if(found) //返回值非0,表示找到了该单词,则将用户查询信息(包括用户名,时间和单词)插入到历史记录表中
{
get_date(date);//调用时间函数获取时间
sprintf(sqlstr, "insert into record values ('%s', '%s', '%s')", pbuf->name, date, word);
if (sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("error : %s\n", errmsg);
sqlite3_free(errmsg);
}
}
else //返回值为0,表示没有找到,发送没到到提示信息
{
strcpy(pbuf->data, "not found\n");
}
send(connfd, pbuf, sizeof(MSG), 0); //把查找到的信息发送给客户端
return;
}
//得到查询结果,并将历史记录发送给客户端
int history_callback(void *arg, int f_num, char **f_value, char **f_name)
{
int connfd;
MSG buf;
connfd = *(int *)arg;
sprintf(buf.data, "%s : %s", f_value[1], f_value[2]);
send(connfd, &buf, sizeof(buf), 0);
return 0;
}
void do_history(int connfd, MSG *pbuf) //查询历史记录模块
{
char sqlstr[128], *errmsg;
sprintf(sqlstr, "select * from record where name = '%s'", pbuf->name);
if (sqlite3_exec(db, sqlstr, history_callback, (void *)&connfd, &errmsg) != SQLITE_OK) //查询数据库
{
printf("error : %s\n", errmsg);
sqlite3_free(errmsg);
}
pbuf->data[0] = '\0'; //所有的记录发送完毕之后,给客户端发送一个结束信息
send(connfd, pbuf, sizeof(MSG), 0);
return;
}
void do_client(int connfd) //处理客户端请求主模块,调用函数完成所需功能
{
MSG buf;
while(recv(connfd, &buf, sizeof(buf), 0) > 0){
switch (buf.type) {
case R:
printf("will reg\n");
do_register(connfd, &buf);
break;
case L:
printf("will login\n");
do_login(connfd, &buf);
break;
case Q:
printf("will query\n");
do_query(connfd, &buf);
break;
case H:
printf("will history\n");
do_history(connfd, &buf);
break;
default:
break;
}
}
exit(0);
}
int main(int argc, char *argv[]) //主函数
{
int listenfd, connfd;
struct sockaddr_in myaddr;
pid_t pid;
MSG buf;
if (argc < 3)
{
printf("Usage : %s <ip> <port>\n", argv[0]);
exit(-1);
}
//打开数据库
if(sqlite3_open(DATABASE, &db) < 0){
printf("fail to sqlite3_open : %s\n", sqlite3_errmsg(db));
return -1;
}
//创建服务器socket
listenfd = socket(PF_INET, SOCK_STREAM, 0);
if(listenfd < 0){
perror("fail to socket");
exit(-1);
}
bzero(&myaddr, sizeof(myaddr));
myaddr.sin_family = PF_INET;
myaddr.sin_port = htons(atoi(argv[2]));
myaddr.sin_addr.s_addr = inet_addr(argv[1]);
if (bind(listenfd, (SA *)&myaddr, sizeof(myaddr)) < 0)
{
perror("fail to bind");
exit(-1);
}
// 将套接字设为监听模式
if (listen(listenfd, 5) < 0)
{
perror("fail to listen");
exit(-1);
}
//并发服务器模型
while(1){
if((connfd = accept(listenfd, NULL, NULL)) < 0){
perror("fail to accept");
exit(-1);
}
pid = fork();
if(pid == -1){
perror("fail to fork\n");
exit(-1);
}
else if(pid == 0){ //子进程处理客户端具体的消息
printf("a user comming\n");
do_client(connfd);
}else{ //父进程用于接收客户端的请求
close(connfd);
}
}
}
client.c代码部分:文章来源:https://www.toymoban.com/news/detail-675390.html
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sqlite3.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define N 16
typedef struct sockaddr SA;
enum CMD{
R=1,
L,
Q,
H
};
typedef struct
{
int type;
char name[N];
char data[256]; // password or word
} MSG;
void do_register(int sockfd, MSG *pbuf) //一级菜单用户注册模块
{
pbuf->type = R;
printf("input name : ");
scanf("%s", pbuf->name);
printf("input password : ");
scanf("%s", pbuf->data);
send(sockfd, pbuf, sizeof(MSG), 0);
recv(sockfd, pbuf, sizeof(MSG), 0);
printf("register : %s\n\n", pbuf->data);
}
int do_login(int sockfd, MSG *pbuf) //一级菜单用户登录模块
{
pbuf->type = L;
printf("input name : ");
scanf("%s", pbuf->name);
printf("input password : ");
scanf("%s", pbuf->data);
send(sockfd, pbuf, sizeof(MSG), 0);
recv(sockfd, pbuf, sizeof(MSG), 0);
if(strncmp(pbuf->data, "OK", 3) == 0){ //3个字节:O K /0
printf("login sucess\n\n");
return 1;
}else{
printf("login : %s\n\n", pbuf->data);
return 0;
}
}
void do_history(int sockfd, MSG *pbuf) //二级菜单查询单词历史记录模块
{
pbuf->type = H;
send(sockfd, pbuf, sizeof(MSG), 0);
while ( 1 )
{
recv(sockfd, pbuf, sizeof(MSG), 0);
if (pbuf->data[0] == '\0') break;
printf("%s\n", pbuf->data);
}
return;
}
void do_query(int sockfd, MSG *pbuf) //二级菜单查询单词模块
{
pbuf->type = Q;
while ( 1 )
{
printf("input word(# to quit) : ");
scanf("%s", pbuf->data);
if (strcmp(pbuf->data, "#") == 0) break; //输入#返回上一级菜单
send(sockfd, pbuf, sizeof(MSG), 0); //将要查询的单词发送给服务器
recv(sockfd, pbuf, sizeof(MSG), 0); //等待接收服务器传递回来的单词信息
printf("%s\n", pbuf->data);
printf("\n");
}
return;
}
void enter_query(int sockfd, MSG *buf) //二级菜单主页面
{
int input;
char cleanbuf[64];
while (1) {
printf("***********************************************\n");
printf("* 1: query_word 2: history_record 3: quit *\n");
printf("***********************************************\n");
printf("please choose : ");
//获取用户输入
if(scanf("%d", &input) == 0){
fgets(cleanbuf, 64, stdin); //类型错误需要重新清除输入缓冲区
continue;
}
switch (input) {
case 1:
printf("\n");
do_query(sockfd, buf);
printf("\n");
break;
case 2:
printf("\n");
do_history(sockfd, buf);
printf("\n");
break;
case 3:
return;
default:
break;
}
}
}
int main(int argc, char *argv[]) //主函数
{
int sockfd, login = 0;
struct sockaddr_in servaddr;
MSG buf;
char clean[64];
if (argc < 3)
{
printf("Usage : %s <serv_ip> <serv_port>\n", argv[0]);
exit(-1);
}
//创建客户端socket
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
{
perror("fail to socket");
exit(-1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = PF_INET;
servaddr.sin_port = htons(atoi(argv[2]));
servaddr.sin_addr.s_addr = inet_addr(argv[1]);
//连接服务器
if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) < 0)
{
perror("fail to connect");
exit(-1);
}
int input;
char cleanbuf[64];
while(1){
printf("************************************\n");
printf("* 1: register 2: login 3: quit *\n");
printf("************************************\n");
printf("please choose : ");
//获取用户输入
if(scanf("%d", &input) == 0){
fgets(cleanbuf, 64, stdin); //类型错误需要重新清除输入缓冲区
continue;
}
switch (input) {
case 1:
do_register(sockfd, &buf);
break;
case 2:
if(do_login(sockfd, &buf) == 1){ //成功
enter_query(sockfd, &buf);
}
break;
case 3:
close(sockfd);
exit(0);
break;
default:
break;
}
}
}
Makefile代码部分:文章来源地址https://www.toymoban.com/news/detail-675390.html
all:server client
server:server.c
gcc $< -o $@ -l sqlite3
client:client.c
gcc $< -o $@
clean:
rm server client
到了这里,关于网络电子词典的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!