一、项目功能描述
-
用户注册和登录验证
-
服务器端将用户信息和历史记录保存在数据库中
-
客户端输入用户名和密码,服务器端在数据库中查找、匹配,返回结果
-
单词在线翻译
-
根据客户端输入的单词在字典文件中搜索
-
历史记录查询
二、项目流程
1. 定义数据库表的结构
$ 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> .tables
sqlite> create table usr (name text primary key, pass text);
sqlite> create table record(name text, data text, word text);
sqlite> .schema usr
CREATE TABLE usr (name text primary key, pass text);
sqlite> .schema record
CREATE TABLE record (name text, data text, word text);
2. 定义消息结构体
typedef struct{
int type; //数据类型
char name[N]; //姓名
char data[256]; //密码或者查询的单词
}MSG;
3. 服务端和客户端流程分析
客户端
服务端
4. 代码实现
//头文件
#ifndef __ONLIN__
#define __ONLIN__
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <strings.h>
#include <signal.h>
#include <sqlite3.h>
#include <string.h>
#include <time.h>
#include <curses.h>
#define N 64
#define R 1 //注册
#define L 2 //登录
#define Q 3 //查询
#define H 4 //历史记录
typedef struct{
int type; //数据类型
char name[N]; //姓名
char data[256]; //密码或者查询的单词
}MSG; //消息结构体
#endif
//客户端
#include "ondi.h"
void do_register(int sockfd, MSG *msg);
int do_login(int sockfd, MSG *msg);
int do_query(int sockfd, MSG *msg);
int do_history(int sockfd, MSG *msg);
void getpwd(char *pwd, int pwdlen);
int main(int argc, const char *argv[])
{
int sockfd;
struct sockaddr_in serv_addr;
int n;
MSG msg;
if(argc != 3){
printf("Usage:%s <serverip> <port>.\n", argv[0]);
exit(0);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0){
perror("sockfd");
exit(0);
}
bzero(&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(atoi(argv[2]));
if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
perror("connect");
exit(0);
}
while(1){
printf("********************\n");
printf("1.注册 2.登录 3.退出\n");
printf("********************\n");
printf("请选择:");
scanf("%d", &n);
getchar();
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(1);
break;
default:
printf("输入无效\n");
}
}
NEXT:
while(1){
printf("********************\n");
printf("1.查询 2.历史 3.退出\n");
printf("********************\n");
printf("请选择:");
scanf("%d", &n);
getchar();
switch(n) {
case 1:
do_query(sockfd, &msg);
break;
case 2:
do_history(sockfd, &msg);
break;
case 3:
close(sockfd);
exit(1);
break;
default:
printf("输入无效\n");
}
}
return 0;
}
/*注册模块*/
void do_register(int sockfd, MSG *msg){
msg->type = R;
printf("输入用户名:");
scanf("%s", msg->name);
getchar();
//输入密码不可见
char *password = getpass("输入密码:");
strcpy(msg->data, password);
//将用户名和密码传递给服务端
if(send(sockfd, msg, sizeof(MSG), 0) < 0) {
perror("send");
exit(0);
}
//接收服务端发来的反馈
if(recv(sockfd, msg, sizeof(MSG), 0) < 0) {
perror("recv");
exit(0);
}
printf("%s\n", msg->data);
return;
}
/*登录模块*/
int do_login(int sockfd, MSG *msg){
msg->type = L;
printf("输入用户名:");
scanf("%s", msg->name);
getchar();
//使输入密码不可见
char *password = getpass("输入密码:");
strcpy(msg->data, password);
//将用户名和密码传递给服务端
if(send(sockfd, msg, sizeof(MSG), 0) < 0) {
perror("send");
return -1;
}
//接收服务端发来的反馈
if(recv(sockfd, msg, sizeof(MSG), 0) < 0) {
perror("recv");
return -1;
}
if(strncmp(msg->data, "OK", 3) == 0){
printf("登录成功!\n");
return 1;
}else{
printf("%s\n", msg->data);
}
return 0;
}
int do_query(int sockfd, MSG *msg){
msg->type = Q;
printf("输入#退出查询!\n");
while(1){
printf("输入查询的单词:");
scanf("%s", msg->data);
getchar();
//输入#号返回上一级菜单
if(strncmp(msg->data , "#", 1) == 0)
break;
// printf("%d\n", msg->type);
/*将要查询的单词发送至服务端*/
if(send(sockfd, msg, sizeof(MSG), 0) < 0){
perror("send");
return -1;
}
/*等待接受服务器传递回来的单词信息*/
if(recv(sockfd, msg, sizeof(MSG), 0) < 0){
perror("recv");
return -1;
}
/*打印接受到的单词信息*/
printf("%s\n", msg->data);
}
return 0;
}
int do_history(int sockfd, MSG *msg){
msg->type = H;
/*发送消息类型给服务端*/
if(send(sockfd, msg, sizeof(MSG), 0) < 0) {
perror("send");
return -1;
}
while(1){
/*接收服务端发来的历史记录*/
if(recv(sockfd, msg, sizeof(MSG), 0) < 0) {
perror("recv");
return -1;
}
if(msg->data[0] == '\0')
break;
/*输出历史记录信息*/
printf("%s\n", msg->data);
}
return 0;
}
//服务端
#include "ondi.h"
#define DATABASE "my.db"
int do_client(int acceptfd, sqlite3 *db);
void do_register(int acceptfd, MSG *msg, sqlite3 *db);
int do_login(int acceptfd, MSG *msg, sqlite3 *db);
int do_query(int acceptfd, MSG *msg, sqlite3 *db);
int do_history(int acceptfd, MSG *msg, sqlite3 *db);
int history_callback(void* arg,int f_num,char** f_value,char** f_name);
int do_searchword(int acceptfd, MSG *msg, char word[]);
int get_date(char *date);
int main(int argc, const char *argv[])
{
int sockfd;
struct sockaddr_in serveraddr;
int n;
MSG msg;
sqlite3 *db;
int acceptfd;
pid_t pid;
//入参检查
if(argc != 3)
{
printf("Usage:%s <serverip> <port>.\n", argv[0]);
return -1;
}
//打开数据库
if(sqlite3_open(DATABASE, &db) != SQLITE_OK)
{
printf("%s\n", sqlite3_errmsg(db));
return -1;
}
else
{
printf("open DATABASE success.\n");
}
//创建流式套接字
if((sockfd = socket(AF_INET, SOCK_STREAM,0)) < 0)
{
perror("fail to socket.\n");
return -1;
}
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
serveraddr.sin_port = htons(atoi(argv[2]));
//建立套接字与地址联系
if(bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0)
{
perror("fail to bind.\n");
return -1;
}
//将套接字设为监听模式
if(listen(sockfd, 5) < 0)
{
perror("listen");
return -1;
}
//处理僵尸进程
signal(SIGCHLD, SIG_IGN);
while(1)
{
if((acceptfd = accept(sockfd, NULL, NULL)) < 0)
{
perror("fail to accept");
return -1;
}
if((pid = fork()) < 0)
{
perror("fail to fork");
return -1;
}
else if(pid == 0) // 儿子进程
{
//处理客户端具体的消息
close(sockfd);
do_client(acceptfd, db);
}
else // 父亲进程,用来接受客户端的请求的
{
close(acceptfd);
}
}
return 0;
}
int do_client(int acceptfd, sqlite3 *db)
{
MSG msg;
//接收消息类型
while(recv(acceptfd, &msg, sizeof(msg), 0) > 0)
{
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("客户端退出\n");
close(acceptfd);
exit(0);
return 0;
}
void do_register(int acceptfd, MSG *msg, sqlite3 *db)
{
char *errmsg;
char sql[128];
sprintf(sql,"insert into usr values('%s','%s');", msg->name, msg->data);
if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("%s\n", errmsg);
strcpy(msg->data, "用户名已存在");
}else{
printf("客户端注册成功\n");
strcpy(msg->data, "注册成功!");
}
//发送反馈给客户端
if(send(acceptfd, msg, sizeof(MSG), 0) < 0)
{
perror("send");
return;
}
return ;
}
int do_login(int acceptfd, MSG *msg , sqlite3 *db)
{
char sql[128] = {0};
char *errmsg;
int nrow;
int ncloumn;
char **resultp;
sprintf(sql, "select * from usr where name = '%s' and pass = '%s';", msg->name, msg->data);
if(sqlite3_get_table(db, sql, &resultp, &nrow, &ncloumn, &errmsg) != SQLITE_OK){
printf("%s\n", errmsg);
return -1;
}
if(nrow == 1){ //查询成功
strcpy(msg->data, "OK");
send(acceptfd, msg, sizeof(MSG), 0);
return 1;
}
if(nrow == 0){ //数据库中没有
strcpy(msg->data, "用户名或密码错误");
send(acceptfd, msg, sizeof(MSG), 0);
}
return 0;
}
int do_searchword(int acceptfd, MSG *msg, char word[])
{
FILE *fp;
char temp[512] = {0};
int result;
int len;
char *p;
/*打开文件*/
fp = fopen("dict.txt", "r");
if(fp == NULL){
perror("fopen");
strcpy(msg->data, "文件打开失败");
if(send(acceptfd, msg, sizeof(MSG), 0) < 0){
perror("send");
return -1;
}
return -1;
}
len = strlen(word);
//读文件,查询单词
while(fgets(temp, 512, fp) != NULL) //一次读一行
{
result = strncmp(temp, word, len);
//FIXME
/*例如:查询单词word = abc, 找到了tomp = abb...,则继续向下查找*/
if(result < 0){
continue;
}
/*例如:查询单词word = abc,找到了temp = abd...*/
/*例如:查询单词word = abc,找到了temp = abcd..*/
/*表示没找到,直接退出*/
if(result > 0 || ((result == 0)&&(temp[len] != ' ')))
{
break;
}
/*表示找到了单词*/
/*跳到单词的末尾空格处 abc v.adsdfs*/
p = temp + len;
/*跳跃所有空格*/
while(*p == ' '){
p++;
}
/*将注释信息拷贝到msg->data*/
strcpy(msg->data, p);
/*最后关闭文件文件流指针,返回1表示查找成功*/
fclose(fp);
return 1;
}
fclose(fp);
return 0;
}
/*得到系统时间*/
int get_date(char *date)
{
time_t t;
struct tm *tp;
time(&t);
//进行时间格式转换
tp = localtime(&t);
/*将时间装进date*/
sprintf(date, "%4d-%2d-%2d %2d:%2d:%2d", tp->tm_year + 1900, tp->tm_mon+1, tp->tm_mday,
tp->tm_hour, tp->tm_min , tp->tm_sec);
return 0;
}
int do_query(int acceptfd, MSG *msg , sqlite3 *db)
{
char word[64];
int found = 0;
char date[128] = {0};
char sql[128] = {0};
char *errmsg;
/*拿出msg结构体中的单词*/
strcpy(word, msg->data);
/*在dict.txt中查询单词word*/
found = do_searchword(acceptfd,msg, word);
/*表示找到了单词,将用户名,时间,单词,输入到历史记录表*/
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;
}
}
/*表示没找到*/
if(found == 0){
strcpy(msg->data, "没有找到!");
}
/*将查询到的结果发送给客户端*/
if(send(acceptfd, msg, sizeof(MSG), 0) < 0){
perror("send");
return -1;
}
return 0;
}
// 得到查询结果,并且需要将历史记录发送给客户端
int history_callback(void* arg,int f_num,char** f_value,char** f_name)
{
//record:name、date、word
int acceptfd;
MSG msg;
/*查询结果*/
acceptfd = *((int *)arg);
/*
*f_value[1] data
*f_value[2] word
*/
sprintf(msg.data, "%s %s", f_value[1], f_value[2]);
send(acceptfd, &msg, sizeof(MSG), 0);
return 0;
}
int do_history(int acceptfd, MSG *msg, sqlite3 *db)
{
char sql[128];
char *errmsg;
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);
}
/*所有的历史记录查询发送完,给客户端发送一个结束信息*/
msg->data[0] = '\0';
send(acceptfd, msg, sizeof(MSG), 0);
return 0;
}
5. 演示
$ ./server 127.0.0.1 5555
open DATABASE success.
UNIQUE constraint failed: usr.name
客户端注册成功
客户端退出
$ ./client 127.0.0.1 5555
********************
1.注册 2.登录 3.退出
********************
请选择:1
输入用户名:zhangsan
输入密码:
用户名已存在
********************
1.注册 2.登录 3.退出
********************
请选择:1
输入用户名:main
输入密码:
注册成功!
********************
1.注册 2.登录 3.退出
********************
请选择:2
输入用户名:main
输入密码:
登录成功!
********************
1.查询 2.历史 3.退出
********************
请选择:1
输入#退出查询!
输入查询的单词:cat
n. small furry domesticated animal often kept as a pet or for catching mice
输入查询的单词:dog
n. common domestic animal kept by human beings for work, hunting, etc or as a pet
输入查询的单词:#
********************
1.查询 2.历史 3.退出
********************
请选择:2
2022- 8-26 8:59:52 cat
2022- 8-26 8:59:56 dog
********************
1.查询 2.历史 3.退出
********************
请选择:3