基于TCP的在线词典系统(分阶段实现)(阻塞io和多路io复用(select)实现)

1.功能说明

一共四个功能:

                注册

                登录

                查询单词

                查询历史记录

单词和解释保存在文件中,单词和解释只占一行,

一行最多300个字节,单词和解释之间至少有一个空格。

2.功能演示

3、分阶段完成各个功能

3.1 完成服务器和客户端的连接

service.c

#include <head.h>int main(int argc, const char *argv[])
{// 入参合理性检查if (3 != argc){printf("usage error:%s <port> <ip>\n", argv[0]);exit(-1);}// 创建套接字int sockfd;struct sockaddr_in serveraddr;memset(&serveraddr, 0, sizeof(serveraddr));if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){perror("sockfd error");exit(-1);}// 填充服务器网络信息结构体serveraddr.sin_family = AF_INET;serveraddr.sin_port = htons(atoi(argv[2]));serveraddr.sin_addr.s_addr = inet_addr(argv[1]);socklen_t serveraddr_len = sizeof(serveraddr);struct sockaddr_in clientaddr;socklen_t clientaddr_len = sizeof(clientaddr);memset(&clientaddr, 0, sizeof(clientaddr));// 绑定if (-1 == bind(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len)){perror("bind error");exit(-1);}// 监听printf("%d\n", sockfd);if (listen(sockfd, 5) < 0){perror("fail to listen");exit(-1);}int acceptfd;while (1){acceptfd = 0;printf("阻塞等待客户端连接...\n");if (-1 == (acceptfd = accept(sockfd, (struct sockaddr *)&clientaddr, &clientaddr_len))){perror("accept errror");exit(-1);}printf("111111111\n");printf("客户端[%s][%d]连接成功...\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));pid_t pid;pid = fork();if (pid == -1){perror("fork error");exit(-1);}else if (pid == 0){// 子进程}else{// 父进程wait(NULL);}}return 0;
}

client.c

#include <head.h>
void print_menu();
int main(int argc, const char *argv[])
{// 入参合理性检查if (3 != argc){printf("usage error:%s <ip> <port>\n", argv[1]);exit(-1);}// 创建套接字int sockfd;if (-1 == (sockfd = socket(AF_INET, SOCK_STREAM, 0))){perror("socket error");exit(-1);}// 填充服务器网络信息结构体struct sockaddr_in serveraddr;socklen_t serveraddr_len = sizeof(serveraddr);serveraddr.sin_family = AF_INET;serveraddr.sin_addr.s_addr = inet_addr(argv[1]);serveraddr.sin_port = htons(atoi(argv[2]));if (-1 == connect(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len)){perror("connect error");exit(-1);}printf("连接服务器成功...\n");while (1){print_menu();sleep(10);}return 0;
}
void print_menu()
{printf("-------------------------------------\n");printf("|    1.regist     2.login     3.quit|\n");printf("-------------------------------------\n");return;
}

3.2 新增完成注册和退出功能

service.c

#include <head.h>
#include <sqlite3.h> //sqlite3
#define R 1
#define L 2
#define Q 3
#define H 4
#define DATABASE "test.db"
typedef struct _MSG
{int type;char name[32];char data[128];
} msg_t;
void do_regist(msg_t *msg, int acceptfd, sqlite3 *my_db);
void do_quit(msg_t *msg, int acceptfd, sqlite3 *my_db);
int main(int argc, const char *argv[])
{// 入参合理性检查if (3 != argc){printf("usage error:%s <port> <ip>\n", argv[0]);exit(-1);}// 创建数据库sqlite3 *my_db;if (SQLITE_OK != sqlite3_open(DATABASE, &my_db)){printf("sqlite3 open error:%s\n", sqlite3_errmsg(my_db));exit(-1);}// 创建套接字int sockfd;struct sockaddr_in serveraddr;memset(&serveraddr, 0, sizeof(serveraddr));if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){perror("sockfd error");exit(-1);}// 填充服务器网络信息结构体serveraddr.sin_family = AF_INET;serveraddr.sin_port = htons(atoi(argv[2]));serveraddr.sin_addr.s_addr = inet_addr(argv[1]);socklen_t serveraddr_len = sizeof(serveraddr);struct sockaddr_in clientaddr;socklen_t clientaddr_len = sizeof(clientaddr);memset(&clientaddr, 0, sizeof(clientaddr));// 绑定if (-1 == bind(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len)){perror("bind error");exit(-1);}// 监听printf("%d\n", sockfd);if (listen(sockfd, 5) < 0){perror("fail to listen");exit(-1);}int acceptfd;msg_t msg;while (1){acceptfd = 0;printf("阻塞等待客户端连接...\n");if (-1 == (acceptfd = accept(sockfd, (struct sockaddr *)&clientaddr, &clientaddr_len))){perror("accept errror");exit(-1);}printf("111111111\n");printf("客户端[%s][%d]连接成功...\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));pid_t pid;pid = fork();if (pid == -1){perror("fork error");exit(-1);}else if (pid == 0){while (1){if (-1 == recv(acceptfd, &msg, sizeof(msg), 0)){perror("recv error");exit(-1);}// printf("msg_type = %d\n", msg.type);// printf("msg.name = %s\n", msg.name);// printf("msg.data = %s\n", msg.data);switch (msg.type){case R:do_regist(&msg, acceptfd, my_db);break;case L:break;case Q:printf("客户端[%s][%d]退出了...\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));do_quit(&msg, acceptfd, my_db);break;case H:break;}}// 子进程}else{// 父进程close(acceptfd);wait(NULL);}}return 0;
}
void do_regist(msg_t *msg, int acceptfd, sqlite3 *my_db)
{char sqlstr[512] = {0};char *errmsg;// printf("msg]msg.type = %d\n", msg->type);// printf("[msg]msg.name = %s\n", msg->name);// printf("[msg]msg.data = %s\n", msg->data);sprintf(sqlstr, "insert into user values('%s','%s')", msg->name, msg->data);// printf("[do_regist]sqlstr = %s\n", sqlstr);if (sqlite3_exec(my_db, sqlstr, NULL, NULL, &errmsg) != SQLITE_OK){printf("errmsg = %s\n", errmsg);sprintf(msg->data, "user already exit,regist fail");}else{strcpy(msg->data, "OK");}// printf("[do_regist_send]msg.type = %d\n", msg->type);// printf("[do_regist_send]msg.name = %s\n", msg->name);// printf("[do_regist_send]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return;
}
void do_quit(msg_t *msg, int acceptfd, sqlite3 *my_db)
{exit(-1);
}

client.c

#include <head.h>
#include <sqlite3.h> //sqlite3
#define R 1
#define L 2
#define Q 3
#define H 4
#define DATABASE "test.db"
typedef struct _MSG
{int type;char name[32];char data[128];
} msg_t;
void print_menu();
void login_user(msg_t msg, int sockfd);
void exit_system(msg_t *msg, int sockfd);
int regist_user(msg_t *msg, int sockfd);
int check_user(msg_t msg, int sockfd);
int main(int argc, const char *argv[])
{// 入参合理性检查if (3 != argc){printf("usage error:%s <ip> <port>\n", argv[1]);exit(-1);}// 创建套接字sqlite3 *my_db;if (SQLITE_OK != sqlite3_open(DATABASE, &my_db)){printf("sqlite3 open error:%s\n", sqlite3_errmsg(my_db));exit(-1);}int sockfd;if (-1 == (sockfd = socket(AF_INET, SOCK_STREAM, 0))){perror("socket error");exit(-1);}// 填充服务器网络信息结构体struct sockaddr_in serveraddr;socklen_t serveraddr_len = sizeof(serveraddr);serveraddr.sin_family = AF_INET;serveraddr.sin_addr.s_addr = inet_addr(argv[1]);serveraddr.sin_port = htons(atoi(argv[2]));int choose = 0;msg_t msg;if (-1 == connect(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len)){perror("connect error");exit(-1);}printf("连接服务器成功...\n");while (1){print_menu();printf("请输入您的选择:");scanf("%d", &choose);switch (choose){case R:regist_user(&msg, sockfd);case L:login_user(msg, sockfd);break;case Q:exit_system(&msg, sockfd);break;}}return 0;
}
void print_menu()
{printf("-------------------------------------\n");printf("|    1.regist     2.login     3.quit|\n");printf("-------------------------------------\n");return;
}
void exit_system(msg_t *msg, int sockfd)
{system("clear");msg->type = Q;if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}close(sockfd);printf("欢迎下次使用基于TCP的在线词典系统...\n");exit(-1);
}
int check_user(msg_t msg, int sockfd)
{printf("[check_user_send]msg.type = %d\n", msg.type);printf("[check_user_send]msg.name = %s\n", msg.name);printf("[check_user_send]msg.data = %s\n", msg.data);if (-1 == send(sockfd, &msg, sizeof(msg), 0)){perror("send error");exit(-1);}memset(&msg, 0, sizeof(msg));if (-1 == recv(sockfd, &msg, sizeof(msg), 0)){perror("recv error");exit(-1);}printf("[check_user_recv]msg.type = %d\n", msg.type);printf("[check_user_recv]msg.name = %s\n", msg.name);printf("[check_user_recv]msg.data = %s\n", msg.data);if (strcmp(msg.data, "user already exit,regist fail") == 0){return -1;}else{return 0;}
}
int regist_user(msg_t *msg, int sockfd)
{memset(msg, 0, sizeof(msg_t));msg->type = R;printf("[注册]请输入用户名:");scanf("%s", msg->name);printf("[注册]请输入密码:");scanf("%s", msg->data);// printf("[regist_user]msg.type = %d\n", msg->type);// printf("[regist_user]msg.name = %s\n", msg->name);// printf("[regist_user]msg.data = %s\n", msg->data);if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}// printf("[regist_send]msg.type = %d\n", msg->type);// printf("[regist_send]msg.name = %s\n", msg->name);// printf("[regist_send]msg.data = %s\n", msg->data);memset(msg, 0, sizeof(msg));if (-1 == recv(sockfd, msg, sizeof(msg_t), 0)){perror("recv error");exit(-1);}// printf("[regist_recv]msg.type = %d\n", msg->type);// printf("[regist_recv]msg.name = %s\n", msg->name);// printf("[regist_recv]msg.data = %s\n", msg->data);if (strncmp(msg->data, "OK", 3) == 0){printf("注册成功...\n");return 1;}printf("用户名已存在,注册失败...\n");return 0;
}
void login_user(msg_t msg, int sockfd)
{return;
}

结果图:

3.3 新增完成登录功能(密码和用户名都正确才能登录成功)

service.c

#include <head.h>
#include <sqlite3.h> //sqlite3
#define R 1
#define L 2
#define Q 3
#define S 4
#define H 5
#define E 6
#define DATABASE "test.db"
#define FILEPATH "./dict.txt"
typedef struct _MSG
{int type;char name[256];char data[128];
} msg_t;
void do_regist(msg_t *msg, int acceptfd, sqlite3 *my_db);
void do_quit(msg_t *msg, int acceptfd, sqlite3 *my_db);
int do_login(msg_t *msg, int acceptfd, sqlite3 *my_db);
int do_search(msg_t *msg, int acceptfd, sqlite3 *my_db, FILE *fp);
void getdata(char *data);
void do_history(msg_t *msg, int acceptfd, sqlite3 *my_db);
int flafs = 0; // 全局变量 使得系统只有在第一次查询时 打开一次文件,第二次不用再打开
int main(int argc, const char *argv[])
{// 入参合理性检查if (3 != argc){printf("usage error:%s <port> <ip>\n", argv[0]);exit(-1);}// 创建数据库sqlite3 *my_db;if (SQLITE_OK != sqlite3_open(DATABASE, &my_db)){printf("sqlite3 open error:%s\n", sqlite3_errmsg(my_db));exit(-1);}FILE *fp; // 传入do_search();// 创建套接字int sockfd;struct sockaddr_in serveraddr;memset(&serveraddr, 0, sizeof(serveraddr));if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){perror("sockfd error");exit(-1);}// 填充服务器网络信息结构体serveraddr.sin_family = AF_INET;serveraddr.sin_port = htons(atoi(argv[2]));serveraddr.sin_addr.s_addr = inet_addr(argv[1]);socklen_t serveraddr_len = sizeof(serveraddr);struct sockaddr_in clientaddr;socklen_t clientaddr_len = sizeof(clientaddr);memset(&clientaddr, 0, sizeof(clientaddr));// 绑定if (-1 == bind(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len)){perror("bind error");exit(-1);}// 监听printf("%d\n", sockfd);if (listen(sockfd, 5) < 0){perror("fail to listen");exit(-1);}int acceptfd;msg_t msg;while (1){acceptfd = 0;printf("阻塞等待客户端连接...\n");if (-1 == (acceptfd = accept(sockfd, (struct sockaddr *)&clientaddr, &clientaddr_len))){perror("accept errror");exit(-1);}printf("111111111\n");printf("客户端[%s][%d]连接成功...\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));pid_t pid;pid = fork();if (pid == -1){perror("fork error");exit(-1);}else if (pid == 0){while (1){if (-1 == recv(acceptfd, &msg, sizeof(msg), 0)){perror("recv error");exit(-1);}printf("[main]msg_type = %d\n", msg.type);printf("[main]msg.name = %s\n", msg.name);printf("[main]msg.data = %s\n", msg.data);switch (msg.type){case R:do_regist(&msg, acceptfd, my_db);break;case L:do_login(&msg, acceptfd, my_db);break;case Q:printf("客户端[%s][%d]退出了...\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));do_quit(&msg, acceptfd, my_db);break;case S:do_search(&msg, acceptfd, my_db, fp);break;case H:do_history(&msg, acceptfd, my_db);break;case E:break;}}// 子进程}else{// 父进程close(acceptfd);wait(NULL);}}return 0;
}
void getdata(char *data)
{time_t t;struct tm *tp;time(&t);tp = localtime(&t);sprintf(data, "%d-%d-%d %d:%d:%d", 1900 + tp->tm_year, 1 + tp->tm_mon, tp->tm_mday,tp->tm_hour, tp->tm_min, tp->tm_sec);
}
void do_history(msg_t *msg, int acceptfd, sqlite3 *my_db)
{char databuff[128] = {0};getdata(databuff);memset(msg, 0, sizeof(msg_t));msg->type = H;char sqlbuff[256] = {0};sprintf(sqlbuff, "select * from record");return;
}
int do_search(msg_t *msg, int acceptfd, sqlite3 *my_db, FILE *fp)
{char buff[256] = {0};printf("要查询的单词为:%s\n", msg->data);int word_len = 0;char *p = NULL;word_len = strlen(msg->data);printf("要查询的单词%s的长度%d\n", msg->data, word_len);// if (flafs == 0)// {// printf("第一次查询,单词文件打开中...\n");if (NULL == (fp = fopen(FILEPATH, "r"))){printf("[第一次]打开单词文件失败...\n");strcpy(msg->data, "open error or EOF");if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}}p = NULL;fgets(buff, sizeof(buff), fp);while (strncmp(msg->data, buff, word_len) != 0){if (fgets(buff, sizeof(buff), fp) == NULL){strcpy(msg->data, "NO_WORLD");// printf("11111111111111111\n");printf("[do_search]msg.type = %d\n", msg->type);printf("[do_search]msg.name = %s\n", msg->name);printf("[do_search]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return 0;}}printf("[strncmp]msg.data = %s\n", msg->data);printf("[strncmp]buff=%s\n", buff);p = buff;p += word_len;if (*p != ' '){// printf("22222222222\n");strcpy(msg->data, "NO_WORLD");printf("[do_search]msg.type = %d\n", msg->type);printf("[do_search]msg.name = %s\n", msg->name);printf("[do_search]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return 0;}while (*p == ' '){p++;}strcpy(msg->data, p);printf("[do_search]msg.type = %d\n", msg->type);printf("[do_search]msg.name = %s\n", msg->name);printf("[do_search]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}fclose(fp);return 1;
}void do_regist(msg_t *msg, int acceptfd, sqlite3 *my_db)
{char sqlstr[512] = {0};char *errmsg;printf("[msg]msg.type = %d\n", msg->type);printf("[msg]msg.name = %s\n", msg->name);printf("[msg]msg.data = %s\n", msg->data);sprintf(sqlstr, "insert into user values('%s','%s')", msg->name, msg->data);printf("[do_regist]sqlstr = %s\n", sqlstr);if (sqlite3_exec(my_db, sqlstr, NULL, NULL, &errmsg) != SQLITE_OK){printf("errmsg = %s\n", errmsg);strcpy(msg->data, "user already exit,regist fail");}else{strcpy(msg->data, "OK");}printf("[do_regist_send]msg.type = %d\n", msg->type);printf("[do_regist_send]msg.name = %s\n", msg->name);printf("[do_regist_send]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return;
}
// int callback(void *arg, int ncolumn, char **f_value, char **f_name)
// {
//     int i = 0;
//     int flag = 0;
//     msg_t *msg_callback = (msg_t *)arg;
//     printf("[msg_callbackmsg_callback.type = %d\n", msg_callback->type);
//     printf("[msg_callback]msg_callback.name = %s\n", msg_callback->name);
//     printf("[msg_callback]msg_callback.data = %s\n", msg_callback->data);
//     if (0 == flag)
//     {
//         // 先打印字段名
//         for (i = 0; i < ncolumn; i++)
//         {
//             // strcpy(msg_callback->data, "user or passwd ereror");
//             printf("\n");
//         }
//         flag = 1;
//     }
//     // 再打印字段的值
//     for (i = 0; i < ncolumn; i++)
//     {
//         if ((f_value[i] != msg_callback->name) || (f_value[i] != msg_callback->data))
//         {
//             strcpy(msg_callback->data, "user or passwd ereror");
//         }
//         else
//         {
//             strcpy(msg_callback->data, "OK");
//         }
//     }
//     return 0; // 这里的回调函数要求必须有返回值 如果没有会报错 query aborted
// }int do_login(msg_t *msg, int acceptfd, sqlite3 *my_db)
{char sqlstr[512] = {0};printf("[msg]msg.type = %d\n", msg->type);printf("[msg]msg.name = %s\n", msg->name);printf("[msg]msg.data = %s\n", msg->data);sprintf(sqlstr, "select * from user where name='%s' and passwd='%s'", msg->name, msg->data);printf("[do_login]sqlstr = %s\n", sqlstr);char *errmsg, **result;int nrow, ncolumn;// 通过sqlite3_get_table函数查询记录是否存在sprintf(sqlstr, "select * from user where name = '%s' and passwd = '%s'", msg->name, msg->data);if (sqlite3_get_table(my_db, sqlstr, &result, &nrow, &ncolumn, &errmsg) != SQLITE_OK){printf("error : %s\n", errmsg);}// 通过nrow参数判断是否能够查询到疾记录,如果值为0,则查询不到,如果值为非0,则查询到if (nrow == 0){strcpy(msg->data, "user or passwd ereror");printf("[do_login_send]msg.type = %d\n", msg->type);printf("[do_login_send]msg.name = %s\n", msg->name);printf("[do_login_send]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return 0;}else{strncpy(msg->data, "OK", 256);printf("[do_login_send]msg.type = %d\n", msg->type);printf("[do_login_send]msg.name = %s\n", msg->name);printf("[do_login_send]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return 1;}
}
void do_quit(msg_t *msg, int acceptfd, sqlite3 *my_db)
{exit(-1);
}

client.c

#include <head.h>
#include <sqlite3.h> //sqlite3
#define R 1
#define L 2
#define Q 3
#define S 4
#define H 5
#define E 6
#define DATABASE "test.db"
#define FILEPATH "./dict.txt"
typedef struct _MSG
{int type;char name[256];char data[128];
} msg_t;
void print_menu();
int login_user(msg_t *msg, int sockfd, sqlite3 *my_db);
void exit_system(msg_t *msg, int sockfd);
int regist_user(msg_t *msg, int sockfd);
int check_user(msg_t msg, int sockfd);
int search_world(msg_t *msg, int sockfd, sqlite3 *my_db);
int exit_search(msg_t *msg, int sockfd);
void query_menu();
void printf_history(msg_t *msg, int sockfd, sqlite3 *my_db);
int main(int argc, const char *argv[])
{// 入参合理性检查if (3 != argc){printf("usage error:%s <ip> <port>\n", argv[1]);exit(-1);}// 创建套接字sqlite3 *my_db;if (SQLITE_OK != sqlite3_open(DATABASE, &my_db)){printf("sqlite3 open error:%s\n", sqlite3_errmsg(my_db));exit(-1);}int sockfd;if (-1 == (sockfd = socket(AF_INET, SOCK_STREAM, 0))){perror("socket error");exit(-1);}// 填充服务器网络信息结构体struct sockaddr_in serveraddr;socklen_t serveraddr_len = sizeof(serveraddr);serveraddr.sin_family = AF_INET;serveraddr.sin_addr.s_addr = inet_addr(argv[1]);serveraddr.sin_port = htons(atoi(argv[2]));int choose = 0;msg_t msg;if (-1 == connect(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len)){perror("connect error");exit(-1);}printf("连接服务器成功...\n");
NEXT2:while (1){print_menu();printf("请输入您的选择(1-3):");scanf("%d", &choose);switch (choose){case R:regist_user(&msg, sockfd);break;case L:if (login_user(&msg, sockfd, my_db) == 1){goto NEXT;}break;case Q:close(sockfd);// system("clear");printf("欢迎下次使用基于TCP的在线词典系统...\n");exit(0);}}
NEXT:while (1){// system("clear");query_menu();printf("请输入您的选择(4-6):");scanf("%d", &choose);switch (choose){case S:search_world(&msg, sockfd, my_db);break;case H:printf_history(&msg, sockfd, my_db);break;case E:goto NEXT2;break;}}return 0;
}
void print_menu()
{printf("-------------------------------------\n");printf("|  1.regist     2.login     3.quit  |\n");printf("-------------------------------------\n");return;
}
void query_menu()
{printf("-------------------------------------\n");printf("|  4.search    5.history    6.quit  |\n");printf("-------------------------------------\n");return;
}
void exit_system(msg_t *msg, int sockfd)
{system("clear");msg->type = Q;if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}close(sockfd);printf("欢迎下次使用基于TCP的在线词典系统...\n");exit(-1);
}void printf_history(msg_t *msg, int sockfd, sqlite3 *my_db)
{// memset(msg, 0, sizeof(msg_t));// printf("[printf_history]msg.type = %d\n", msg->type);// printf("[printf_history]msg.name = %s\n", msg->name); // 单词// printf("[printf_history]msg.data = %s\n", msg->data); // OKmsg->type = H;printf("<---历史记录如下--->\n");while (1){if (-1 == recv(sockfd, msg, sizeof(msg_t), 0)){perror("recv error");exit(-1);}if (strcmp(msg->data, "***OVER***") == 0){break;}printf("%s:%s\n", msg->name, msg->data);}return;
}
int regist_user(msg_t *msg, int sockfd)
{memset(msg, 0, sizeof(msg_t));msg->type = R;printf("[注册]请输入用户名:");scanf("%s", msg->name);printf("[注册]请输入密码:");scanf("%s", msg->data);// printf("[regist_user]msg.type = %d\n", msg->type);// printf("[regist_user]msg.name = %s\n", msg->name);// printf("[regist_user]msg.data = %s\n", msg->data);if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}// printf("[regist_send]msg.type = %d\n", msg->type);// printf("[regist_send]msg.name = %s\n", msg->name);// printf("[regist_send]msg.data = %s\n", msg->data);memset(msg, 0, sizeof(msg));if (-1 == recv(sockfd, msg, sizeof(msg_t), 0)){perror("recv error");exit(-1);}// printf("[regist_recv]msg.type = %d\n", msg->type);// printf("[regist_recv]msg.name = %s\n", msg->name);// printf("[regist_recv]msg.data = %s\n", msg->data);if (strcmp(msg->data, "OK") == 0){printf("注册成功...\n");return 1;}printf("用户名已存在,注册失败...\n");return 0;
}
int login_user(msg_t *msg, int sockfd, sqlite3 *my_db)
{memset(msg, 0, sizeof(msg_t));msg->type = L;printf("[登录]请输入用户名:");scanf("%s", msg->name);printf("[登录]请输入密码:");scanf("%s", msg->data);// printf("[login_user]msg.type = %d\n", msg->type);// printf("[login_user]msg.name = %s\n", msg->name);// printf("[login_user]msg.data = %s\n", msg->data);if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");return -1;}// printf("[login_user_send]msg.type = %d\n", msg->type);// printf("[login_user_send]msg.name = %s\n", msg->name);// printf("[login_user_send]msg.data = %s\n", msg->data);// memset(msg, 0, sizeof(msg_t));if (-1 == recv(sockfd, msg, sizeof(msg_t), 0)){perror("recv error");return -1;}// printf("[login_user_recv]msg.type = %d\n", msg->type);// printf("[login_user_recv]msg.name = %s\n", msg->name);// printf("[login_user_recv]msg.data = %s\n", msg->data);if (strcmp(msg->data, "OK") == 0){printf("登录成功...\n");return 1;}printf("用户名或密码错误...\n");return 0;
}
// int exit_search(msg_t *msg, int sockfd)
// {
//     return 0;
// }
int search_world(msg_t *msg, int sockfd, sqlite3 *my_db)
{// msg->type = S;while (1){// memset(msg, 0, sizeof(msg_t));msg->type = S;printf("[查询]请输入要查询的单词(按#退出):");scanf("%s", msg->data);if (msg->data[strlen(msg->data) - 1] == '\n'){msg->data[strlen(msg->data) - 1] = '\0';}// printf("[scanf]msg->name = %s\n", msg->name);if (strcmp(msg->data, "#") == 0){printf("你主动退出了查单词功能...\n");break;}// printf("[do_search_send]msg.type = %d\n", msg->type);// printf("[do_search_send]msg.name = %s\n", msg->name);// printf("[do_search_send]msg.data = %s\n", msg->data);if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}memset(msg, 0, sizeof(msg));if (-1 == recv(sockfd, msg, sizeof(msg_t), 0)){perror("recv error");exit(-1);}// printf("[do_search_recv]msg.type = %d\n", msg->type);// printf("[do_search_recv]msg.name = %s\n", msg->name);// printf("[do_search_recv]msg.data = %s\n", msg->data);if (strncmp(msg->data, "NO_WORLD", 8) == 0){printf("[*****没有此单词*****]...\n");// return 0;}else{printf("[*****解释*****]:%s", msg->data);}}return 0;
}int check_user(msg_t msg, int sockfd)
{// printf("[check_user_send]msg.type = %d\n", msg.type);// printf("[check_user_send]msg.name = %s\n", msg.name);// printf("[check_user_send]msg.data = %s\n", msg.data);if (-1 == send(sockfd, &msg, sizeof(msg), 0)){perror("send error");exit(-1);}memset(&msg, 0, sizeof(msg));if (-1 == recv(sockfd, &msg, sizeof(msg), 0)){perror("recv error");exit(-1);}// printf("[check_user_recv]msg.type = %d\n", msg.type);// printf("[check_user_recv]msg.name = %s\n", msg.name);// printf("[check_user_recv]msg.data = %s\n", msg.data);if (strcmp(msg.data, "user already exit,regist fail") == 0){return -1;}else{return 0;}
}

运行结果图

 

3.4 完成查询单词功能(search)

单词文件

链接:https://pan.baidu.com/s/17qgZZpO7YyyQ0pCLYclg8A 
提取码:关注收藏后,私信获取

service.c

#include <head.h>
#include <sqlite3.h> //sqlite3
#define R 1
#define L 2
#define Q 3
#define S 4
#define H 5
#define E 6
#define DATABASE "test.db"
#define FILEPATH "./dict.txt"
typedef struct _MSG
{int type;char name[256];char data[128];
} msg_t;
void do_regist(msg_t *msg, int acceptfd, sqlite3 *my_db);
void do_quit(msg_t *msg, int acceptfd, sqlite3 *my_db);
int do_login(msg_t *msg, int acceptfd, sqlite3 *my_db);
int do_search(msg_t *msg, int acceptfd, sqlite3 *my_db, FILE *fp);
int flafs = 0; // 全局变量 使得系统只有在第一次查询时 打开一次文件,第二次不用再打开
int main(int argc, const char *argv[])
{// 入参合理性检查if (3 != argc){printf("usage error:%s <port> <ip>\n", argv[0]);exit(-1);}// 创建数据库sqlite3 *my_db;if (SQLITE_OK != sqlite3_open(DATABASE, &my_db)){printf("sqlite3 open error:%s\n", sqlite3_errmsg(my_db));exit(-1);}FILE *fp; // 传入do_search();// 创建套接字int sockfd;struct sockaddr_in serveraddr;memset(&serveraddr, 0, sizeof(serveraddr));if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){perror("sockfd error");exit(-1);}// 填充服务器网络信息结构体serveraddr.sin_family = AF_INET;serveraddr.sin_port = htons(atoi(argv[2]));serveraddr.sin_addr.s_addr = inet_addr(argv[1]);socklen_t serveraddr_len = sizeof(serveraddr);struct sockaddr_in clientaddr;socklen_t clientaddr_len = sizeof(clientaddr);memset(&clientaddr, 0, sizeof(clientaddr));// 绑定if (-1 == bind(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len)){perror("bind error");exit(-1);}// 监听printf("%d\n", sockfd);if (listen(sockfd, 5) < 0){perror("fail to listen");exit(-1);}int acceptfd;msg_t msg;while (1){acceptfd = 0;printf("阻塞等待客户端连接...\n");if (-1 == (acceptfd = accept(sockfd, (struct sockaddr *)&clientaddr, &clientaddr_len))){perror("accept errror");exit(-1);}printf("111111111\n");printf("客户端[%s][%d]连接成功...\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));pid_t pid;pid = fork();if (pid == -1){perror("fork error");exit(-1);}else if (pid == 0){while (1){if (-1 == recv(acceptfd, &msg, sizeof(msg), 0)){perror("recv error");exit(-1);}printf("[main]msg_type = %d\n", msg.type);printf("[main]msg.name = %s\n", msg.name);printf("[main]msg.data = %s\n", msg.data);switch (msg.type){case R:do_regist(&msg, acceptfd, my_db);break;case L:do_login(&msg, acceptfd, my_db);break;case Q:printf("客户端[%s][%d]退出了...\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));do_quit(&msg, acceptfd, my_db);break;case S:do_search(&msg, acceptfd, my_db, fp);break;case H:break;case E:break;}}// 子进程}else{// 父进程close(acceptfd);wait(NULL);}}return 0;
}
int do_search(msg_t *msg, int acceptfd, sqlite3 *my_db, FILE *fp)
{char buff[256] = {0};printf("要查询的单词为:%s\n", msg->name);int word_len = 0;char *p = NULL;word_len = strlen(msg->name);printf("要查询的单词%s的长度%d\n", msg->name, word_len);// if (flafs == 0)// {// printf("第一次查询,单词文件打开中...\n");if (NULL == (fp = fopen(FILEPATH, "r"))){printf("[第一次]打开单词文件失败...\n");strcpy(msg->name, "open error or EOF");strcpy(msg->data, "search error");if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}}p = NULL;fgets(buff, sizeof(buff), fp);while (strncmp(msg->name, buff, word_len) != 0){if (fgets(buff, sizeof(buff), fp) == NULL){strcpy(msg->name, "NO_WORLD");// printf("11111111111111111\n");printf("[do_search]msg.type = %d\n", msg->type);printf("[do_search]msg.name = %s\n", msg->name);printf("[do_search]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return 0;}}printf("[strncmp]msg.name = %s\n", msg->name);printf("[strncmp]buff=%s\n", buff);p = buff;p += word_len;if (*p != ' '){// printf("22222222222\n");strcpy(msg->name, "NO_WORLD");printf("[do_search]msg.type = %d\n", msg->type);printf("[do_search]msg.name = %s\n", msg->name);printf("[do_search]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return 0;}while (*p == ' '){p++;}strcpy(msg->name, "OK");strcpy(msg->data, p);printf("[do_search]msg.type = %d\n", msg->type);printf("[do_search]msg.name = %s\n", msg->name);printf("[do_search]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}fclose(fp);return 1;
}void do_regist(msg_t *msg, int acceptfd, sqlite3 *my_db)
{char sqlstr[512] = {0};char *errmsg;printf("[msg]msg.type = %d\n", msg->type);printf("[msg]msg.name = %s\n", msg->name);printf("[msg]msg.data = %s\n", msg->data);sprintf(sqlstr, "insert into user values('%s','%s')", msg->name, msg->data);printf("[do_regist]sqlstr = %s\n", sqlstr);if (sqlite3_exec(my_db, sqlstr, NULL, NULL, &errmsg) != SQLITE_OK){printf("errmsg = %s\n", errmsg);strcpy(msg->data, "user already exit,regist fail");}else{strcpy(msg->data, "OK");}printf("[do_regist_send]msg.type = %d\n", msg->type);printf("[do_regist_send]msg.name = %s\n", msg->name);printf("[do_regist_send]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return;
}
// int callback(void *arg, int ncolumn, char **f_value, char **f_name)
// {
//     int i = 0;
//     int flag = 0;
//     msg_t *msg_callback = (msg_t *)arg;
//     printf("[msg_callbackmsg_callback.type = %d\n", msg_callback->type);
//     printf("[msg_callback]msg_callback.name = %s\n", msg_callback->name);
//     printf("[msg_callback]msg_callback.data = %s\n", msg_callback->data);
//     if (0 == flag)
//     {
//         // 先打印字段名
//         for (i = 0; i < ncolumn; i++)
//         {
//             // strcpy(msg_callback->data, "user or passwd ereror");
//             printf("\n");
//         }
//         flag = 1;
//     }
//     // 再打印字段的值
//     for (i = 0; i < ncolumn; i++)
//     {
//         if ((f_value[i] != msg_callback->name) || (f_value[i] != msg_callback->data))
//         {
//             strcpy(msg_callback->data, "user or passwd ereror");
//         }
//         else
//         {
//             strcpy(msg_callback->data, "OK");
//         }
//     }
//     return 0; // 这里的回调函数要求必须有返回值 如果没有会报错 query aborted
// }int do_login(msg_t *msg, int acceptfd, sqlite3 *my_db)
{char sqlstr[512] = {0};printf("[msg]msg.type = %d\n", msg->type);printf("[msg]msg.name = %s\n", msg->name);printf("[msg]msg.data = %s\n", msg->data);sprintf(sqlstr, "select * from user where name='%s' and passwd='%s'", msg->name, msg->data);printf("[do_login]sqlstr = %s\n", sqlstr);char *errmsg, **result;int nrow, ncolumn;// 通过sqlite3_get_table函数查询记录是否存在sprintf(sqlstr, "select * from user where name = '%s' and passwd = '%s'", msg->name, msg->data);if (sqlite3_get_table(my_db, sqlstr, &result, &nrow, &ncolumn, &errmsg) != SQLITE_OK){printf("error : %s\n", errmsg);}// 通过nrow参数判断是否能够查询到疾记录,如果值为0,则查询不到,如果值为非0,则查询到if (nrow == 0){strcpy(msg->data, "user or passwd ereror");printf("[do_login_send]msg.type = %d\n", msg->type);printf("[do_login_send]msg.name = %s\n", msg->name);printf("[do_login_send]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return 0;}else{strncpy(msg->data, "OK", 256);printf("[do_login_send]msg.type = %d\n", msg->type);printf("[do_login_send]msg.name = %s\n", msg->name);printf("[do_login_send]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return 1;}
}
void do_quit(msg_t *msg, int acceptfd, sqlite3 *my_db)
{exit(-1);
}

client.c

#include <head.h>
#include <sqlite3.h> //sqlite3
#define R 1
#define L 2
#define Q 3
#define S 4
#define H 5
#define E 6
#define DATABASE "test.db"
#define FILEPATH "./dict.txt"
typedef struct _MSG
{int type;char name[256];char data[128];
} msg_t;
void print_menu();
int login_user(msg_t *msg, int sockfd, sqlite3 *my_db);
void exit_system(msg_t *msg, int sockfd);
int regist_user(msg_t *msg, int sockfd);
int check_user(msg_t msg, int sockfd);
int search_world(msg_t *msg, int sockfd, sqlite3 *my_db);
int exit_search(msg_t *msg, int sockfd);
void query_menu();
int main(int argc, const char *argv[])
{// 入参合理性检查if (3 != argc){printf("usage error:%s <ip> <port>\n", argv[1]);exit(-1);}// 创建套接字sqlite3 *my_db;if (SQLITE_OK != sqlite3_open(DATABASE, &my_db)){printf("sqlite3 open error:%s\n", sqlite3_errmsg(my_db));exit(-1);}int sockfd;if (-1 == (sockfd = socket(AF_INET, SOCK_STREAM, 0))){perror("socket error");exit(-1);}// 填充服务器网络信息结构体struct sockaddr_in serveraddr;socklen_t serveraddr_len = sizeof(serveraddr);serveraddr.sin_family = AF_INET;serveraddr.sin_addr.s_addr = inet_addr(argv[1]);serveraddr.sin_port = htons(atoi(argv[2]));int choose = 0;msg_t msg;if (-1 == connect(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len)){perror("connect error");exit(-1);}printf("连接服务器成功...\n");
NEXT2:while (1){print_menu();printf("请输入您的选择(1-3):");scanf("%d", &choose);switch (choose){case R:regist_user(&msg, sockfd);break;case L:if (login_user(&msg, sockfd, my_db) == 1){goto NEXT;}break;case Q:close(sockfd);// system("clear");printf("欢迎下次使用基于TCP的在线词典系统...\n");exit(0);}}
NEXT:while (1){// system("clear");query_menu();printf("请输入您的选择(4-6):");scanf("%d", &choose);switch (choose){case S:search_world(&msg, sockfd, my_db);break;case H:break;case E:goto NEXT2;break;}}return 0;
}
void print_menu()
{printf("-------------------------------------\n");printf("|  1.regist     2.login     3.quit  |\n");printf("-------------------------------------\n");return;
}
void query_menu()
{printf("-------------------------------------\n");printf("|  4.search    5.history    6.quit  |\n");printf("-------------------------------------\n");return;
}
void exit_system(msg_t *msg, int sockfd)
{system("clear");msg->type = Q;if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}close(sockfd);printf("欢迎下次使用基于TCP的在线词典系统...\n");exit(-1);
}
int regist_user(msg_t *msg, int sockfd)
{memset(msg, 0, sizeof(msg_t));msg->type = R;printf("[注册]请输入用户名:");scanf("%s", msg->name);printf("[注册]请输入密码:");scanf("%s", msg->data);// printf("[regist_user]msg.type = %d\n", msg->type);// printf("[regist_user]msg.name = %s\n", msg->name);// printf("[regist_user]msg.data = %s\n", msg->data);if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}// printf("[regist_send]msg.type = %d\n", msg->type);// printf("[regist_send]msg.name = %s\n", msg->name);// printf("[regist_send]msg.data = %s\n", msg->data);memset(msg, 0, sizeof(msg));if (-1 == recv(sockfd, msg, sizeof(msg_t), 0)){perror("recv error");exit(-1);}// printf("[regist_recv]msg.type = %d\n", msg->type);// printf("[regist_recv]msg.name = %s\n", msg->name);// printf("[regist_recv]msg.data = %s\n", msg->data);if (strcmp(msg->data, "OK") == 0){printf("注册成功...\n");return 1;}printf("用户名已存在,注册失败...\n");return 0;
}
int login_user(msg_t *msg, int sockfd, sqlite3 *my_db)
{memset(msg, 0, sizeof(msg_t));msg->type = L;printf("[登录]请输入用户名:");scanf("%s", msg->name);printf("[登录]请输入密码:");scanf("%s", msg->data);// printf("[login_user]msg.type = %d\n", msg->type);// printf("[login_user]msg.name = %s\n", msg->name);// printf("[login_user]msg.data = %s\n", msg->data);if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");return -1;}// printf("[login_user_send]msg.type = %d\n", msg->type);// printf("[login_user_send]msg.name = %s\n", msg->name);// printf("[login_user_send]msg.data = %s\n", msg->data);// memset(msg, 0, sizeof(msg_t));if (-1 == recv(sockfd, msg, sizeof(msg_t), 0)){perror("recv error");return -1;}// printf("[login_user_recv]msg.type = %d\n", msg->type);// printf("[login_user_recv]msg.name = %s\n", msg->name);// printf("[login_user_recv]msg.data = %s\n", msg->data);if (strcmp(msg->data, "OK") == 0){printf("登录成功...\n");return 1;}printf("用户名或密码错误...\n");return 0;
}
// int exit_search(msg_t *msg, int sockfd)
// {
//     return 0;
// }
int search_world(msg_t *msg, int sockfd, sqlite3 *my_db)
{// msg->type = S;while (1){memset(msg, 0, sizeof(msg_t));msg->type = S;printf("[查询]请输入要查询的单词(按#退出):");scanf("%s", msg->name);if (msg->name[strlen(msg->name) - 1] == '\n'){msg->name[strlen(msg->name) - 1] = '\0';}// printf("[scanf]msg->name = %s\n", msg->name);if (strcmp(msg->name, "#") == 0){printf("你主动退出了查单词功能...\n");break;}// printf("[do_search_send]msg.type = %d\n", msg->type);// printf("[do_search_send]msg.name = %s\n", msg->name);// printf("[do_search_send]msg.data = %s\n", msg->data);if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}memset(msg, 0, sizeof(msg));if (-1 == recv(sockfd, msg, sizeof(msg_t), 0)){perror("recv error");exit(-1);}// printf("[do_search_recv]msg.type = %d\n", msg->type);// printf("[do_search_recv]msg.name = %s\n", msg->name);// printf("[do_search_recv]msg.data = %s\n", msg->data);if (strncmp(msg->name, "OK", 3) == 0){printf("[*****解释*****]:%s", msg->data);}else if (strncmp(msg->name, "NO_WORLD", 8) == 0){printf("[*****没有此单词*****]...\n");}}return 0;
}int check_user(msg_t msg, int sockfd)
{// printf("[check_user_send]msg.type = %d\n", msg.type);// printf("[check_user_send]msg.name = %s\n", msg.name);// printf("[check_user_send]msg.data = %s\n", msg.data);if (-1 == send(sockfd, &msg, sizeof(msg), 0)){perror("send error");exit(-1);}memset(&msg, 0, sizeof(msg));if (-1 == recv(sockfd, &msg, sizeof(msg), 0)){perror("recv error");exit(-1);}// printf("[check_user_recv]msg.type = %d\n", msg.type);// printf("[check_user_recv]msg.name = %s\n", msg.name);// printf("[check_user_recv]msg.data = %s\n", msg.data);if (strcmp(msg.data, "user already exit,regist fail") == 0){return -1;}else{return 0;}
}

结果图

3.5 完成记录查询功能(history)

service.c

#include <head.h>
#include <sqlite3.h> //sqlite3
#define R 1
#define L 2
#define Q 3
#define S 4
#define H 5
#define E 6
#define DATABASE "test.db"
#define FILEPATH "./dict.txt"
typedef struct _MSG
{int type;char name[256];char data[128];
} msg_t;
void do_regist(msg_t *msg, int acceptfd, sqlite3 *my_db);
void do_quit(msg_t *msg, int acceptfd, sqlite3 *my_db);
int do_login(msg_t *msg, int acceptfd, sqlite3 *my_db);
int do_search(msg_t *msg, int acceptfd, sqlite3 *my_db, FILE *fp);
void getdata(char *data);
int history_callback(void *arg, int f_num, char **f_value, char **f_name);
void do_history(msg_t *msg, int acceptfd, sqlite3 *my_db);
int flafs = 0; // 全局变量 使得系统只有在第一次查询时 打开一次文件,第二次不用再打开
int main(int argc, const char *argv[])
{// 入参合理性检查if (3 != argc){printf("usage error:%s <port> <ip>\n", argv[0]);exit(-1);}// 创建数据库sqlite3 *my_db;if (SQLITE_OK != sqlite3_open(DATABASE, &my_db)){printf("sqlite3 open error:%s\n", sqlite3_errmsg(my_db));exit(-1);}FILE *fp; // 传入do_search();// 创建套接字int sockfd;struct sockaddr_in serveraddr;memset(&serveraddr, 0, sizeof(serveraddr));if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){perror("sockfd error");exit(-1);}// 填充服务器网络信息结构体serveraddr.sin_family = AF_INET;serveraddr.sin_port = htons(atoi(argv[2]));serveraddr.sin_addr.s_addr = inet_addr(argv[1]);socklen_t serveraddr_len = sizeof(serveraddr);struct sockaddr_in clientaddr;socklen_t clientaddr_len = sizeof(clientaddr);memset(&clientaddr, 0, sizeof(clientaddr));// 绑定if (-1 == bind(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len)){perror("bind error");exit(-1);}// 监听printf("%d\n", sockfd);if (listen(sockfd, 5) < 0){perror("fail to listen");exit(-1);}int acceptfd;msg_t msg;while (1){acceptfd = 0;printf("阻塞等待客户端连接...\n");if (-1 == (acceptfd = accept(sockfd, (struct sockaddr *)&clientaddr, &clientaddr_len))){perror("accept errror");exit(-1);}printf("111111111\n");printf("客户端[%s][%d]连接成功...\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));pid_t pid;pid = fork();if (pid == -1){perror("fork error");exit(-1);}else if (pid == 0){while (1){if (-1 == recv(acceptfd, &msg, sizeof(msg), 0)){perror("recv error");exit(-1);}printf("[main]msg_type = %d\n", msg.type);printf("[main]msg.name = %s\n", msg.name);printf("[main]msg.data = %s\n", msg.data);switch (msg.type){case R:do_regist(&msg, acceptfd, my_db);break;case L:do_login(&msg, acceptfd, my_db);break;case Q:printf("客户端[%s][%d]退出了...\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));do_quit(&msg, acceptfd, my_db);break;case S:do_search(&msg, acceptfd, my_db, fp);break;case H:do_history(&msg, acceptfd, my_db);break;case E:break;}}// 子进程}else{// 父进程close(acceptfd);wait(NULL);}}return 0;
}
void getdata(char *data)
{time_t t;struct tm *tp;time(&t);tp = localtime(&t);sprintf(data, "%d-%d-%d %d:%d:%d", 1900 + tp->tm_year, 1 + tp->tm_mon, tp->tm_mday,tp->tm_hour, tp->tm_min, tp->tm_sec);
}
void do_history(msg_t *msg, int acceptfd, sqlite3 *my_db)
{char databuff[128] = {0};getdata(databuff);printf("[do_history]msg.type = %d\n", msg->type);printf("[do_history]msg.name = %s\n", msg->name);printf("[do_history]msg.data = %s\n", msg->data);msg->type = H;char sqlbuff[512] = {0};sprintf(sqlbuff, "select * from record where  name='%s'", msg->name);if (sqlite3_exec(my_db, sqlbuff, history_callback, (void *)&acceptfd, NULL) != SQLITE_OK){printf("do_history sqlite3_exec error");}strcpy(msg->data, "***OVER***");send(acceptfd, msg, sizeof(msg_t), 0);return;
}
int history_callback(void *arg, int f_num, char **f_value, char **f_name)
{int acceptfd_1;msg_t msg;acceptfd_1 = *(int *)arg;sprintf(msg.data, "%s:%s", f_value[0], f_value[2]);send(acceptfd_1, &msg, sizeof(msg_t), 0);return 0;
}int do_search(msg_t *msg, int acceptfd, sqlite3 *my_db, FILE *fp)
{char buff[256] = {0};char databuff[128] = {0};getdata(databuff);printf("要查询的单词为:%s\n", msg->data);char sqlbuff[512] = {0};sprintf(sqlbuff, "insert into record values('%s','%s','%s')", databuff, msg->name, msg->data);printf("sqlbuff = %s\n", sqlbuff);if (sqlite3_exec(my_db, sqlbuff, NULL, NULL, NULL) != SQLITE_OK){printf("do_search sqlite3_exec error\n");return -1;}int word_len = 0;char *p = NULL;word_len = strlen(msg->data);printf("要查询的单词%s的长度%d\n", msg->data, word_len);// if (flafs == 0)// {// printf("第一次查询,单词文件打开中...\n");if (NULL == (fp = fopen(FILEPATH, "r"))){printf("[第一次]打开单词文件失败...\n");strcpy(msg->data, "open error or EOF");if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}}p = NULL;fgets(buff, sizeof(buff), fp);while (strncmp(msg->data, buff, word_len) != 0){if (fgets(buff, sizeof(buff), fp) == NULL){strcpy(msg->data, "NO_WORLD");// printf("11111111111111111\n");printf("[do_search]msg.type = %d\n", msg->type);printf("[do_search]msg.name = %s\n", msg->name);printf("[do_search]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return 0;}}printf("[strncmp]msg.data = %s\n", msg->data);printf("[strncmp]buff=%s\n", buff);p = buff;p += word_len;if (*p != ' '){// printf("22222222222\n");strcpy(msg->data, "NO_WORLD");printf("[do_search]msg.type = %d\n", msg->type);printf("[do_search]msg.name = %s\n", msg->name);printf("[do_search]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return 0;}while (*p == ' '){p++;}strcpy(msg->data, p);printf("[do_search]msg.type = %d\n", msg->type);printf("[do_search]msg.name = %s\n", msg->name);printf("[do_search]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}fclose(fp);return 1;
}void do_regist(msg_t *msg, int acceptfd, sqlite3 *my_db)
{char sqlstr[512] = {0};char *errmsg;printf("[msg]msg.type = %d\n", msg->type);printf("[msg]msg.name = %s\n", msg->name);printf("[msg]msg.data = %s\n", msg->data);sprintf(sqlstr, "insert into user values('%s','%s')", msg->name, msg->data);printf("[do_regist]sqlstr = %s\n", sqlstr);if (sqlite3_exec(my_db, sqlstr, NULL, NULL, &errmsg) != SQLITE_OK){printf("errmsg = %s\n", errmsg);strcpy(msg->data, "user already exit,regist fail");}else{strcpy(msg->data, "OK");}printf("[do_regist_send]msg.type = %d\n", msg->type);printf("[do_regist_send]msg.name = %s\n", msg->name);printf("[do_regist_send]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return;
}
// int callback(void *arg, int ncolumn, char **f_value, char **f_name)
// {
//     int i = 0;
//     int flag = 0;
//     msg_t *msg_callback = (msg_t *)arg;
//     printf("[msg_callbackmsg_callback.type = %d\n", msg_callback->type);
//     printf("[msg_callback]msg_callback.name = %s\n", msg_callback->name);
//     printf("[msg_callback]msg_callback.data = %s\n", msg_callback->data);
//     if (0 == flag)
//     {
//         // 先打印字段名
//         for (i = 0; i < ncolumn; i++)
//         {
//             // strcpy(msg_callback->data, "user or passwd ereror");
//             printf("\n");
//         }
//         flag = 1;
//     }
//     // 再打印字段的值
//     for (i = 0; i < ncolumn; i++)
//     {
//         if ((f_value[i] != msg_callback->name) || (f_value[i] != msg_callback->data))
//         {
//             strcpy(msg_callback->data, "user or passwd ereror");
//         }
//         else
//         {
//             strcpy(msg_callback->data, "OK");
//         }
//     }
//     return 0; // 这里的回调函数要求必须有返回值 如果没有会报错 query aborted
// }int do_login(msg_t *msg, int acceptfd, sqlite3 *my_db)
{char sqlstr[512] = {0};printf("[msg]msg.type = %d\n", msg->type);printf("[msg]msg.name = %s\n", msg->name);printf("[msg]msg.data = %s\n", msg->data);sprintf(sqlstr, "select * from user where name='%s' and passwd='%s'", msg->name, msg->data);printf("[do_login]sqlstr = %s\n", sqlstr);char *errmsg, **result;int nrow, ncolumn;// 通过sqlite3_get_table函数查询记录是否存在sprintf(sqlstr, "select * from user where name = '%s' and passwd = '%s'", msg->name, msg->data);if (sqlite3_get_table(my_db, sqlstr, &result, &nrow, &ncolumn, &errmsg) != SQLITE_OK){printf("error : %s\n", errmsg);}// 通过nrow参数判断是否能够查询到疾记录,如果值为0,则查询不到,如果值为非0,则查询到if (nrow == 0){strcpy(msg->data, "user or passwd ereror");printf("[do_login_send]msg.type = %d\n", msg->type);printf("[do_login_send]msg.name = %s\n", msg->name);printf("[do_login_send]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return 0;}else{strncpy(msg->data, "OK", 256);printf("[do_login_send]msg.type = %d\n", msg->type);printf("[do_login_send]msg.name = %s\n", msg->name);printf("[do_login_send]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return 1;}
}
void do_quit(msg_t *msg, int acceptfd, sqlite3 *my_db)
{exit(-1);
}

client.c

#include <head.h>
#include <sqlite3.h> //sqlite3
#define R 1
#define L 2
#define Q 3
#define S 4
#define H 5
#define E 6
#define DATABASE "test.db"
#define FILEPATH "./dict.txt"
typedef struct _MSG
{int type;char name[256];char data[128];
} msg_t;
void print_menu();
int login_user(msg_t *msg, int sockfd, sqlite3 *my_db);
void exit_system(msg_t *msg, int sockfd);
int regist_user(msg_t *msg, int sockfd);
int check_user(msg_t msg, int sockfd);
int search_world(msg_t *msg, int sockfd, sqlite3 *my_db);
int exit_search(msg_t *msg, int sockfd);
void query_menu();
int printf_history(msg_t *msg, int sockfd, sqlite3 *my_db);
int main(int argc, const char *argv[])
{// 入参合理性检查if (3 != argc){printf("usage error:%s <ip> <port>\n", argv[1]);exit(-1);}// 创建套接字sqlite3 *my_db;if (SQLITE_OK != sqlite3_open(DATABASE, &my_db)){printf("sqlite3 open error:%s\n", sqlite3_errmsg(my_db));exit(-1);}int sockfd;if (-1 == (sockfd = socket(AF_INET, SOCK_STREAM, 0))){perror("socket error");exit(-1);}// 填充服务器网络信息结构体struct sockaddr_in serveraddr;socklen_t serveraddr_len = sizeof(serveraddr);serveraddr.sin_family = AF_INET;serveraddr.sin_addr.s_addr = inet_addr(argv[1]);serveraddr.sin_port = htons(atoi(argv[2]));int choose = 0;msg_t msg;if (-1 == connect(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len)){perror("connect error");exit(-1);}printf("连接服务器成功...\n");
NEXT2:while (1){print_menu();printf("请输入您的选择(1-3):");scanf("%d", &choose);switch (choose){case R:regist_user(&msg, sockfd);break;case L:if (login_user(&msg, sockfd, my_db) == 1){goto NEXT;}break;case Q:close(sockfd);// system("clear");printf("欢迎下次使用基于TCP的在线词典系统...\n");exit(0);}}
NEXT:while (1){// system("clear");query_menu();printf("请输入您的选择(4-6):");scanf("%d", &choose);switch (choose){case S:search_world(&msg, sockfd, my_db);break;case H:printf_history(&msg, sockfd, my_db);break;case E:goto NEXT2;break;}}return 0;
}
void print_menu()
{printf("-------------------------------------\n");printf("|  1.regist     2.login     3.quit  |\n");printf("-------------------------------------\n");return;
}
void query_menu()
{printf("-------------------------------------\n");printf("|  4.search    5.history    6.quit  |\n");printf("-------------------------------------\n");return;
}
void exit_system(msg_t *msg, int sockfd)
{system("clear");msg->type = Q;if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}close(sockfd);printf("欢迎下次使用基于TCP的在线词典系统...\n");exit(-1);
}int printf_history(msg_t *msg, int sockfd, sqlite3 *my_db)
{// memset(msg, 0, sizeof(msg_t));// printf("[printf_history]msg.type = %d\n", msg->type);// printf("[printf_history]msg.name = %s\n", msg->name); // 单词// printf("[printf_history]msg.data = %s\n", msg->data); // OKmsg->type = H;if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");return -1;}printf("<---历史记录如下--->\n");while (1){if (-1 == recv(sockfd, msg, sizeof(msg_t), 0)){perror("recv error");exit(-1);}if (strcmp(msg->data, "***OVER***") == 0){break;}printf("%s\n", msg->data);}return 0;
}
int regist_user(msg_t *msg, int sockfd)
{memset(msg, 0, sizeof(msg_t));msg->type = R;printf("[注册]请输入用户名:");scanf("%s", msg->name);printf("[注册]请输入密码:");scanf("%s", msg->data);// printf("[regist_user]msg.type = %d\n", msg->type);// printf("[regist_user]msg.name = %s\n", msg->name);// printf("[regist_user]msg.data = %s\n", msg->data);if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}// printf("[regist_send]msg.type = %d\n", msg->type);// printf("[regist_send]msg.name = %s\n", msg->name);// printf("[regist_send]msg.data = %s\n", msg->data);memset(msg, 0, sizeof(msg));if (-1 == recv(sockfd, msg, sizeof(msg_t), 0)){perror("recv error");exit(-1);}// printf("[regist_recv]msg.type = %d\n", msg->type);// printf("[regist_recv]msg.name = %s\n", msg->name);// printf("[regist_recv]msg.data = %s\n", msg->data);if (strcmp(msg->data, "OK") == 0){printf("注册成功...\n");return 1;}printf("用户名已存在,注册失败...\n");return 0;
}
int login_user(msg_t *msg, int sockfd, sqlite3 *my_db)
{memset(msg, 0, sizeof(msg_t));msg->type = L;printf("[登录]请输入用户名:");scanf("%s", msg->name);printf("[登录]请输入密码:");scanf("%s", msg->data);// printf("[login_user]msg.type = %d\n", msg->type);// printf("[login_user]msg.name = %s\n", msg->name);// printf("[login_user]msg.data = %s\n", msg->data);if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");return -1;}// printf("[login_user_send]msg.type = %d\n", msg->type);// printf("[login_user_send]msg.name = %s\n", msg->name);// printf("[login_user_send]msg.data = %s\n", msg->data);// memset(msg, 0, sizeof(msg_t));if (-1 == recv(sockfd, msg, sizeof(msg_t), 0)){perror("recv error");return -1;}// printf("[login_user_recv]msg.type = %d\n", msg->type);// printf("[login_user_recv]msg.name = %s\n", msg->name);// printf("[login_user_recv]msg.data = %s\n", msg->data);if (strcmp(msg->data, "OK") == 0){printf("登录成功...\n");return 1;}printf("用户名或密码错误...\n");return 0;
}
// int exit_search(msg_t *msg, int sockfd)
// {
//     return 0;
// }
int search_world(msg_t *msg, int sockfd, sqlite3 *my_db)
{// msg->type = S;while (1){// memset(msg, 0, sizeof(msg_t));msg->type = S;printf("[查询]请输入要查询的单词(按#退出):");scanf("%s", msg->data);if (msg->data[strlen(msg->data) - 1] == '\n'){msg->data[strlen(msg->data) - 1] = '\0';}// printf("[scanf]msg->name = %s\n", msg->name);if (strcmp(msg->data, "#") == 0){printf("你主动退出了查单词功能...\n");break;}// printf("[do_search_send]msg.type = %d\n", msg->type);// printf("[do_search_send]msg.name = %s\n", msg->name);// printf("[do_search_send]msg.data = %s\n", msg->data);if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}memset(msg, 0, sizeof(msg));if (-1 == recv(sockfd, msg, sizeof(msg_t), 0)){perror("recv error");exit(-1);}// printf("[do_search_recv]msg.type = %d\n", msg->type);// printf("[do_search_recv]msg.name = %s\n", msg->name);// printf("[do_search_recv]msg.data = %s\n", msg->data);if (strncmp(msg->data, "NO_WORLD", 8) == 0){printf("[*****没有此单词*****]...\n");// return 0;}else{printf("[*****解释*****]:%s", msg->data);}}return 0;
}int check_user(msg_t msg, int sockfd)
{// printf("[check_user_send]msg.type = %d\n", msg.type);// printf("[check_user_send]msg.name = %s\n", msg.name);// printf("[check_user_send]msg.data = %s\n", msg.data);if (-1 == send(sockfd, &msg, sizeof(msg), 0)){perror("send error");exit(-1);}memset(&msg, 0, sizeof(msg));if (-1 == recv(sockfd, &msg, sizeof(msg), 0)){perror("recv error");exit(-1);}// printf("[check_user_recv]msg.type = %d\n", msg.type);// printf("[check_user_recv]msg.name = %s\n", msg.name);// printf("[check_user_recv]msg.data = %s\n", msg.data);if (strcmp(msg.data, "user already exit,regist fail") == 0){return -1;}else{return 0;}
}

3.5通过select实现以上全部功能(多路IO复用)

service.c

#include <head.h>
#include <sqlite3.h> //sqlite3
#define R 1
#define L 2
#define Q 3
#define S 4
#define H 5
#define E 6
#define DATABASE "test.db"
#define FILEPATH "./dict.txt"
typedef struct _MSG
{int type;char name[256];char data[128];
} msg_t;
void do_regist(msg_t *msg, int acceptfd, sqlite3 *my_db);          // 处理注册信息的函数
int do_login(msg_t *msg, int acceptfd, sqlite3 *my_db);            // 处理登录请求的函数
int do_search(msg_t *msg, int acceptfd, sqlite3 *my_db, FILE *fp); // 处理查单词的函数
void getdata(char *data);
int history_callback(void *arg, int f_num, char **f_value, char **f_name); // 历史记录查询回调函数 显示指定用户的所有历史查询记录
void do_history(msg_t *msg, int acceptfd, sqlite3 *my_db);
int main(int argc, const char *argv[])
{// 入参合理性检查if (3 != argc){printf("usage error:%s <port> <ip>\n", argv[0]);exit(-1);}// 打开数据库 需要自己提前创建好.db结尾的数据库文件// 终端执行sqlite3 test.db// CREATE TABLE user(name TEXT PRIMARY KEY,passwd TEXT);sqlite3 *my_db;                                  // 数据库句柄if (SQLITE_OK != sqlite3_open(DATABASE, &my_db)) // 打开数据库{printf("sqlite3 open error:%s\n", sqlite3_errmsg(my_db));exit(-1);}FILE *fp; // FILE *指针 对应打开文件// 创建套接字int sockfd;struct sockaddr_in serveraddr;memset(&serveraddr, 0, sizeof(serveraddr));if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){perror("sockfd error");exit(-1);}// 填充服务器网络信息结构体serveraddr.sin_family = AF_INET;serveraddr.sin_port = htons(atoi(argv[2]));serveraddr.sin_addr.s_addr = inet_addr(argv[1]);socklen_t serveraddr_len = sizeof(serveraddr);struct sockaddr_in clientaddr;socklen_t clientaddr_len = sizeof(clientaddr);memset(&clientaddr, 0, sizeof(clientaddr));// 绑定if (-1 == bind(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len)){perror("bind error");exit(-1);}// 监听printf("%d\n", sockfd);if (listen(sockfd, 5) < 0){perror("fail to listen");exit(-1);}int max_fd = 0; // 用来记录最大的文件描述符int acceptfd;msg_t msg;fd_set readfds; // 母本 监听集合FD_ZERO(&readfds);fd_set readfds_temp; // 副本 用于给select擦除用FD_ZERO(&readfds_temp);// 将sockfd放入监听集合FD_SET(sockfd, &readfds);max_fd = max_fd > sockfd ? max_fd : sockfd;printf("max_fd = %d\n", max_fd);int ret;int i;int recvbyte = 0;while (1){readfds_temp = readfds;if (-1 == (ret = select(max_fd + 1, &readfds_temp, NULL, NULL, NULL))) // select阻塞{perror("select error");return -1;}if (FD_ISSET(sockfd, &readfds_temp)) // 判断是否有客户端要连接{if (-1 == (acceptfd = accept(sockfd, (struct sockaddr *)&clientaddr, &clientaddr_len))){perror("accpet error");return -1;}printf("*****客户端[%s][%d]连接成功...\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));FD_SET(acceptfd, &readfds);printf("%d\n", acceptfd);max_fd = max_fd > acceptfd ? max_fd : acceptfd;}for (i = 5; i < max_fd + 1 && ret != 0; i++) // 检测客户端,检查是哪一个客户端发送的消息{if (FD_ISSET(i, &readfds_temp)){if (-1 == (recvbyte = recv(i, &msg, sizeof(msg_t), 0))){perror("recv error");return -1;}else if (recvbyte == 0){close(i);FD_CLR(i, &readfds);if (i == max_fd){--max_fd;}printf("客户端退出了...\n");}else{switch (msg.type){case R:do_regist(&msg, i, my_db);break;case L:do_login(&msg, i, my_db);break;case Q:printf("客户端[%s][%d]退出了...\n", inet_ntoa(clientaddr.sin_addr), ntohs(clientaddr.sin_port));break;case S:do_search(&msg, i, my_db, fp);break;case H:do_history(&msg, i, my_db);break;case E:break;}}}}}return 0;
}
void getdata(char *data) // 获取系统时间函数
{time_t t;struct tm *tp;time(&t);tp = localtime(&t);sprintf(data, "%d-%d-%d %d:%d:%d", 1900 + tp->tm_year, 1 + tp->tm_mon, tp->tm_mday,tp->tm_hour, tp->tm_min, tp->tm_sec);
}
void do_history(msg_t *msg, int acceptfd, sqlite3 *my_db)
{char databuff[128] = {0};getdata(databuff);printf("[do_history]msg.type = %d\n", msg->type);printf("[do_history]msg.name = %s\n", msg->name);printf("[do_history]msg.data = %s\n", msg->data);msg->type = H;char sqlbuff[512] = {0};sprintf(sqlbuff, "select * from record where  name='%s'", msg->name);if (sqlite3_exec(my_db, sqlbuff, history_callback, (void *)&acceptfd, NULL) != SQLITE_OK){printf("do_history sqlite3_exec error");}strcpy(msg->data, "***OVER***");send(acceptfd, msg, sizeof(msg_t), 0);return;
}
int history_callback(void *arg, int f_num, char **f_value, char **f_name)
{int acceptfd_1;msg_t msg;acceptfd_1 = *(int *)arg;sprintf(msg.data, "%s:%s", f_value[0], f_value[2]);send(acceptfd_1, &msg, sizeof(msg_t), 0);return 0;
}
int do_search(msg_t *msg, int acceptfd, sqlite3 *my_db, FILE *fp)
{if (NULL == (fp = fopen(FILEPATH, "r"))){printf("[第一次]打开单词文件失败...\n");strcpy(msg->data, "open error or EOF");if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}}char buff[256] = {0};char databuff[128] = {0};getdata(databuff);printf("要查询的单词为:%s\n", msg->data);char sqlbuff[512] = {0};sprintf(sqlbuff, "insert into record values('%s','%s','%s')", databuff, msg->name, msg->data);printf("sqlbuff = %s\n", sqlbuff);if (sqlite3_exec(my_db, sqlbuff, NULL, NULL, NULL) != SQLITE_OK){printf("do_search sqlite3_exec error\n");return -1;}int word_len = 0;char *p;p = NULL;word_len = strlen(msg->data);printf("要查询的单词%s的长度%d\n", msg->data, word_len);while (fgets(buff, sizeof(buff), fp) != NULL){p = buff;p += word_len;if (strncmp(msg->data, buff, word_len) == 0 && (*p == ' ')) // 相同字母{while (p++){if (*p != ' '){break;}} // 指向解释strcpy(msg->data, p);printf("[do_search2]msg.type = %d\n", msg->type);printf("[do_search2]msg.name = %s\n", msg->name);printf("[do_search2]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");return -1;}fclose(fp);return 1;}else{continue;}}strcpy(msg->data, "NO_WORLD");printf("[do_search]msg.type = %d\n", msg->type);printf("[do_search]msg.name = %s\n", msg->name);printf("[do_search]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}fclose(fp);return 0;
}void do_regist(msg_t *msg, int acceptfd, sqlite3 *my_db)
{char sqlstr[512] = {0};char *errmsg;printf("[msg]msg.type = %d\n", msg->type);printf("[msg]msg.name = %s\n", msg->name);printf("[msg]msg.data = %s\n", msg->data);sprintf(sqlstr, "insert into user values('%s','%s')", msg->name, msg->data);printf("[do_regist]sqlstr = %s\n", sqlstr);if (sqlite3_exec(my_db, sqlstr, NULL, NULL, &errmsg) != SQLITE_OK){printf("errmsg = %s\n", errmsg);strcpy(msg->data, "user already exit,regist fail");}else{strcpy(msg->data, "OK");}printf("[do_regist_send]msg.type = %d\n", msg->type);printf("[do_regist_send]msg.name = %s\n", msg->name);printf("[do_regist_send]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return;
}int do_login(msg_t *msg, int acceptfd, sqlite3 *my_db)
{char sqlstr[512] = {0};printf("[msg]msg.type = %d\n", msg->type);printf("[msg]msg.name = %s\n", msg->name);printf("[msg]msg.data = %s\n", msg->data);sprintf(sqlstr, "select * from user where name='%s' and passwd='%s'", msg->name, msg->data);printf("[do_login]sqlstr = %s\n", sqlstr);char *errmsg, **result;int nrow, ncolumn;// 通过sqlite3_get_table函数查询记录是否存在sprintf(sqlstr, "select * from user where name = '%s' and passwd = '%s'", msg->name, msg->data);if (sqlite3_get_table(my_db, sqlstr, &result, &nrow, &ncolumn, &errmsg) != SQLITE_OK){printf("error : %s\n", errmsg);}// 通过nrow参数判断是否能够查询到疾记录,如果值为0,则查询不到,如果值为非0,则查询到if (nrow == 0){strcpy(msg->data, "user or passwd ereror");printf("[do_login_send]msg.type = %d\n", msg->type);printf("[do_login_send]msg.name = %s\n", msg->name);printf("[do_login_send]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return 0;}else{strncpy(msg->data, "OK", 256);printf("[do_login_send]msg.type = %d\n", msg->type);printf("[do_login_send]msg.name = %s\n", msg->name);printf("[do_login_send]msg.data = %s\n", msg->data);if (-1 == send(acceptfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}return 1;}
}

client.c

#include <head.h>
#include <sqlite3.h> //sqlite3
#define R 1
#define L 2
#define Q 3
#define S 4
#define H 5
#define E 6
#define DATABASE "test.db"
#define FILEPATH "./dict.txt"
typedef struct _MSG
{int type;char name[256];char data[128];
} msg_t;
void print_menu();
int login_user(msg_t *msg, int sockfd, sqlite3 *my_db);
void exit_system(msg_t *msg, int sockfd);
int regist_user(msg_t *msg, int sockfd);
int check_user(msg_t msg, int sockfd);
int search_world(msg_t *msg, int sockfd, sqlite3 *my_db);
int exit_search(msg_t *msg, int sockfd);
void query_menu();
int printf_history(msg_t *msg, int sockfd, sqlite3 *my_db);
int main(int argc, const char *argv[])
{// 入参合理性检查if (3 != argc){printf("usage error:%s <ip> <port>\n", argv[1]);exit(-1);}// 创建套接字sqlite3 *my_db;if (SQLITE_OK != sqlite3_open(DATABASE, &my_db)){printf("sqlite3 open error:%s\n", sqlite3_errmsg(my_db));exit(-1);}int sockfd;if (-1 == (sockfd = socket(AF_INET, SOCK_STREAM, 0))){perror("socket error");exit(-1);}// 填充服务器网络信息结构体struct sockaddr_in serveraddr;socklen_t serveraddr_len = sizeof(serveraddr);serveraddr.sin_family = AF_INET;serveraddr.sin_addr.s_addr = inet_addr(argv[1]);serveraddr.sin_port = htons(atoi(argv[2]));int choose = 0;msg_t msg;if (-1 == connect(sockfd, (struct sockaddr *)&serveraddr, serveraddr_len)){perror("connect error");exit(-1);}printf("连接服务器成功...\n");
NEXT2:while (1){print_menu();printf("请输入您的选择(1-3):");scanf("%d", &choose);switch (choose){case R:regist_user(&msg, sockfd);break;case L:if (login_user(&msg, sockfd, my_db) == 1){goto NEXT;}break;case Q:// exit_system(&msg, sockfd);close(sockfd);system("clear");printf("欢迎下次使用基于TCP的在线词典系统...\n");exit(0);}}
NEXT:while (1){// system("clear");query_menu();printf("请输入您的选择(4-6):");scanf("%d", &choose);switch (choose){case S:search_world(&msg, sockfd, my_db);break;case H:printf_history(&msg, sockfd, my_db);break;case E:goto NEXT2;break;}}return 0;
}
void print_menu()
{printf("-------------------------------------\n");printf("|  1.regist     2.login     3.quit  |\n");printf("-------------------------------------\n");return;
}
void query_menu()
{printf("-------------------------------------\n");printf("|  4.search    5.history    6.quit  |\n");printf("-------------------------------------\n");return;
}
void exit_system(msg_t *msg, int sockfd)
{system("clear");msg->type = Q;if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}close(sockfd);printf("欢迎下次使用基于TCP的在线词典系统...\n");exit(-1);
}int printf_history(msg_t *msg, int sockfd, sqlite3 *my_db)
{// memset(msg, 0, sizeof(msg_t));// printf("[printf_history]msg.type = %d\n", msg->type);// printf("[printf_history]msg.name = %s\n", msg->name); // 单词// printf("[printf_history]msg.data = %s\n", msg->data); // OKmsg->type = H;if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");return -1;}printf("<---历史记录如下--->\n");while (1){if (-1 == recv(sockfd, msg, sizeof(msg_t), 0)){perror("recv error");exit(-1);}if (strcmp(msg->data, "***OVER***") == 0){break;}printf("%s\n", msg->data);}return 0;
}
int regist_user(msg_t *msg, int sockfd)
{memset(msg, 0, sizeof(msg_t));msg->type = R;printf("[注册]请输入用户名:");scanf("%s", msg->name);printf("[注册]请输入密码:");scanf("%s", msg->data);// printf("[regist_user]msg.type = %d\n", msg->type);// printf("[regist_user]msg.name = %s\n", msg->name);// printf("[regist_user]msg.data = %s\n", msg->data);if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}// printf("[regist_send]msg.type = %d\n", msg->type);// printf("[regist_send]msg.name = %s\n", msg->name);// printf("[regist_send]msg.data = %s\n", msg->data);memset(msg, 0, sizeof(msg));if (-1 == recv(sockfd, msg, sizeof(msg_t), 0)){perror("recv error");exit(-1);}// printf("[regist_recv]msg.type = %d\n", msg->type);// printf("[regist_recv]msg.name = %s\n", msg->name);// printf("[regist_recv]msg.data = %s\n", msg->data);if (strcmp(msg->data, "OK") == 0){printf("注册成功...\n");return 1;}printf("用户名已存在,注册失败...\n");return 0;
}
int login_user(msg_t *msg, int sockfd, sqlite3 *my_db)
{memset(msg, 0, sizeof(msg_t));msg->type = L;printf("[登录]请输入用户名:");scanf("%s", msg->name);printf("[登录]请输入密码:");scanf("%s", msg->data);// printf("[login_user]msg.type = %d\n", msg->type);// printf("[login_user]msg.name = %s\n", msg->name);// printf("[login_user]msg.data = %s\n", msg->data);if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");return -1;}// printf("[login_user_send]msg.type = %d\n", msg->type);// printf("[login_user_send]msg.name = %s\n", msg->name);// printf("[login_user_send]msg.data = %s\n", msg->data);// memset(msg, 0, sizeof(msg_t));if (-1 == recv(sockfd, msg, sizeof(msg_t), 0)){perror("recv error");return -1;}// printf("[login_user_recv]msg.type = %d\n", msg->type);// printf("[login_user_recv]msg.name = %s\n", msg->name);// printf("[login_user_recv]msg.data = %s\n", msg->data);if (strcmp(msg->data, "OK") == 0){printf("登录成功...\n");return 1;}printf("用户名或密码错误...\n");return 0;
}
// int exit_search(msg_t *msg, int sockfd)
// {
//     return 0;
// }
int search_world(msg_t *msg, int sockfd, sqlite3 *my_db)
{// msg->type = S;while (1){// memset(msg, 0, sizeof(msg_t));msg->type = S;printf("[查询]请输入要查询的单词(按#退出):");scanf("%s", msg->data);if (msg->data[strlen(msg->data) - 1] == '\n'){msg->data[strlen(msg->data) - 1] = '\0';}// printf("[scanf]msg->name = %s\n", msg->name);if (strcmp(msg->data, "#") == 0){printf("你主动退出了查单词功能...\n");break;}// printf("[do_search_send]msg.type = %d\n", msg->type);// printf("[do_search_send]msg.name = %s\n", msg->name);// printf("[do_search_send]msg.data = %s\n", msg->data);if (-1 == send(sockfd, msg, sizeof(msg_t), 0)){perror("send error");exit(-1);}memset(msg, 0, sizeof(msg));if (-1 == recv(sockfd, msg, sizeof(msg_t), 0)){perror("recv error");exit(-1);}// printf("[do_search_recv]msg.type = %d\n", msg->type);// printf("[do_search_recv]msg.name = %s\n", msg->name);// printf("[do_search_recv]msg.data = %s\n", msg->data);if (strncmp(msg->data, "NO_WORLD", 8) == 0){printf("[*****没有此单词*****]...\n");// return 0;}else{printf("[*****解释*****]:%s", msg->data);}}return 0;
}int check_user(msg_t msg, int sockfd)
{// printf("[check_user_send]msg.type = %d\n", msg.type);// printf("[check_user_send]msg.name = %s\n", msg.name);// printf("[check_user_send]msg.data = %s\n", msg.data);if (-1 == send(sockfd, &msg, sizeof(msg), 0)){perror("send error");exit(-1);}memset(&msg, 0, sizeof(msg));if (-1 == recv(sockfd, &msg, sizeof(msg), 0)){perror("recv error");exit(-1);}// printf("[check_user_recv]msg.type = %d\n", msg.type);// printf("[check_user_recv]msg.name = %s\n", msg.name);// printf("[check_user_recv]msg.data = %s\n", msg.data);if (strcmp(msg.data, "user already exit,regist fail") == 0){return -1;}else{return 0;}
}

需提前安装sqlite3数据库,安装教程参考以下文章

Linux 基于sqlite3数据库的学生管理系统-CSDN博客文章浏览阅读204次。sqlite官网:www.sqlite.org 学生管理系统增删改查https://blog.csdn.net/CSDN_DU666666/article/details/139999025?spm=1001.2014.3001.5502然后执行

gcc service.c -lsqlite3 -o service

gcc client.c -lsqlite3 -o client

./service 192.168.250.100 8888

./client 192.168.250.100 8888

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/871486.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vue el-input 限制输入内容

&#x1f914;日常项目中经常遇到既要el-input的样式&#xff0c;又要el-input-number限制&#xff0c;所以需要绑定input事件进行约束输入限制。 以下使用自定义指令进行约束el-input输入的值&#xff0c;便于后期统一管理和拓展。 预览 代码 <!DOCTYPE html> <ht…

响应式编程-数据劫持

响应式编程的核心思想是观察者模式&#xff0c;被观察的对象我们可以称之为数据源&#xff0c;所以&#xff0c;数据是响应式编程所关注的核心。 假设有一个数据对象,有一个字段age值为18&#xff1a; let obj {age:18 } 然后有一个函数&#xff0c;在这个函数打印age字段&a…

quota使用

一、检查系统是否支持 grep CONFIG_QUOTA /boot/config* CONFIG_QUOTAy CONFIG_QUOTA_NETLINK_INTERFACEy # CONFIG_QUOTA_DEBUG is not set CONFIG_QUOTA_TREEy CONFIG_QUOTACTLy CONFIG_QUOTACTL_COMPATy二、安装 yum install -y quota三、配置 3.1 创建磁盘 格式一定要 …

【RPC注册发现框架实战】一个简易的RPC注册发现框架

Java实现 服务端起10个线程ID监听40-49这10个端口&#xff0c;这10个端口注册到注册中心&#xff0c;提供同一个服务&#xff0c;发个A&#xff0c;响应B&#xff0c;客户端起10个线程去注册中心请求 好的&#xff0c;我们可以通过实现一个简单的服务端、注册中心和客户端来达到…

【机器学习】精准农业新纪元:机器学习引领的作物管理革命

&#x1f4dd;个人主页&#x1f339;&#xff1a;Eternity._ &#x1f339;&#x1f339;期待您的关注 &#x1f339;&#x1f339; ❀目录 &#x1f50d;1. 引言&#x1f4d2;2. 精准农业的背景与现状&#x1f341;精准农业的概念与发展历程&#x1f342;国内外精准农业实践案…

002-ESP32怎么 上电就能启动指定代码

ESP32在上电后能够启动指定代码&#xff0c;主要依赖于其内部的启动流程和固件配置。以下是一个详细的步骤说明&#xff0c;以及如何实现这一功能&#xff1a; 一、ESP32的启动流程 ESP32的启动流程大致可以分为以下几个阶段&#xff1a; 一级引导程序&#xff1a;被固化在ES…

【数据结构】手写堆 HEAP

heap【堆】掌握 手写上浮、下沉、建堆函数 对一组数进行堆排序 直接使用接口函数heapq 什么是堆&#xff1f;&#xff1f;&#xff1f;堆是一个二叉树。也就是有两个叉。下面是一个大根堆&#xff1a; 大根堆的每一个根节点比他的子节点都大 有大根堆就有小根堆&#xff1…

Qt/QML学习-BusyIndicator

QML学习 BusyIndicator例程视频讲解代码 main.qml import QtQuick 2.15 import QtQuick.Window 2.15 import QtQuick.Controls 2.15Window {width: 640height: 480visible: truetitle: qsTr("Hello World")BusyIndicator {id: busyIndicatoranchors.fill: parentM…

深入解析std::string的设计哲学【C++、STL库】

为什么在C中字符串长度需要调用函数而不是直接访问&#xff1f;深入解析std::string的设计哲学 在C中&#xff0c;获取字符串长度需要调用size()或length()方法&#xff0c;而不是直接访问一个常量或属性。这一设计让许多初学者感到困惑。那么&#xff0c;为什么C会选择这种方…

(南京观海微电子)——二极管应用及选取

二极管是 用半导体材料(硅、硒、锗等)制成的一种电子器件。二极管有两个电极&#xff0c;正极&#xff0c;又叫阳极&#xff1b;负极&#xff0c;又叫阴极&#xff0c;给二极管两极间加上正向电压时&#xff0c;二极管导通&#xff0c; 加上反向电压时&#xff0c;二极管截止。…

Vue1-Vue核心

目录 Vue简介 官网 介绍与描述 Vue的特点 与其它 JS 框架的关联 Vue周边库 初识Vue Vue模板语法 数据绑定 el与data的两种写法 MVVM模型 数据代理 回顾Object.defineProperty方法 何为数据代理 Vue中的数据代理 数据代理图示 事件处理 事件的基本使用 事件修…

【“码上”大模型简介】

“码上”大模型 码上是北京邮电大学EZCoding雏雁/大创团队自主研发、运营和支撑的大模型赋能的智能编程教学应用平台。针对编程教学过程中学生亟需一对一辅导的需求痛点&#xff0c;码上基于讯飞星火大模型&#xff0c;采用北邮自研核心技术&#xff0c;为学生提供实时、个性化…

【UE5.1】Chaos物理系统基础——06 子弹破坏石块

前言 在前面我们已经完成了场系统的制作&#xff08;【UE5.1】Chaos物理系统基础——02 场系统的应用_ue5&#xff09;以及子弹的制作&#xff08;【UE5.1 角色练习】16-枪械射击——瞄准&#xff09;&#xff0c;现在我们准备实现的效果是&#xff0c;角色发射子弹来破坏石柱。…

前端代码基本逻辑-vue3

前端vue建立过程 安装nodejs 官网下载安装&#xff0c;并且记住安装路径&#xff0c;记得配置系统变量Path 安装VUE/CLI npm install -g vue/cli --全局安装vue 使用VUE/CLI生成代码框架 vue create your-project-name --我的your-project-name为web 运行项目 cd your-…

STM32智能空气质量监测系统教程

目录 引言环境准备智能空气质量监测系统基础代码实现&#xff1a;实现智能空气质量监测系统 4.1 数据采集模块 4.2 数据处理与控制模块 4.3 通信与网络系统实现 4.4 用户界面与数据可视化应用场景&#xff1a;空气质量监测与优化问题解决方案与优化收尾与总结 1. 引言 智能空…

基于Java+SpringMvc+Vue技术的药品进销存仓库管理系统设计与实现系统(源码+LW+部署讲解)

注&#xff1a;每个学校每个老师对论文的格式要求不一样&#xff0c;故本论文只供参考&#xff0c;本论文页数达到60页以上&#xff0c;字数在6000及以上。 基于JavaSpringMvcVue技术的在线学习交流平台设计与实现 目录 第一章 绪论 1.1 研究背景 1.2 研究现状 1.3 研究内容…

卸载wps office的几种方法收录

​ 第一种方法: 1.打开【任务管理器】&#xff0c;找到相关程序&#xff0c;点击【结束任务】。任务管理器可以通过左下角搜索找到。 2.点击【开始】&#xff0d;【设置】&#xff0d;【应用】&#xff0d;下拉找到WPS应用&#xff0c;右键卸载&#xff0c;不保留软件配置 …

SQLite3封装类教程

SQLite3封装类教程 SQLite是一种轻量级的数据库&#xff0c;它不需要一个独立的服务器进程。SQLite数据库存储在一个单一的磁盘文件中&#xff0c;这使得它非常适合小型到中型的应用程序&#xff0c;例如移动应用、桌面应用和小型的Web应用。以下是使用Python封装SQLite3数据库…

Promise.all、any、race和allSettled的相同点与不同点与应用场景

在JavaScript中&#xff0c;Promise对象是一种处理异步操作的方式。它允许我们以一种更优雅的方式来处理异步代码&#xff0c;而不是使用回调函数。在Promise中&#xff0c;有一些方法可以帮助我们更好地管理多个Promise实例&#xff0c;这些方法包括Promise.all、Promise.any、…

tomcat为什么要自定义类加载器?

一个Tomcat中可以部署多个应用&#xff0c;而每个应用中都存在很多类&#xff0c;并且各个应用中的类是独立的&#xff0c;全类名是可以相同的。 比如一个订单系统中可能存在com.xushu.User类&#xff0c;一个库存系统中可能也存com.xushu.User类&#xff0c;一个Tomcat&#x…