基于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…

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

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

【数据结构】手写堆 HEAP

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

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

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

Vue1-Vue核心

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

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

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

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;不保留软件配置 …

Git学习1_Git安装(CSDN_20240714)

git下载 git下载官网如下&#xff1a; Git - Downloads (git-scm.com)https://git-scm.com/downloads 根据机器操作系统&#xff0c;下载对应的安装包 git安装 1. 点击安装程序&#xff0c;进入安装界面&#xff0c;如下图所示&#xff0c;点击next。 2. 选择安装路径&…

护网HW面试常问——组件中间件框架漏洞(包含流量特征)

apache&iis&nginx中间件解析漏洞 参考我之前的文章&#xff1a;护网HW面试—apache&iis&nginx中间件解析漏洞篇-CSDN博客 log4j2 漏洞原理&#xff1a; 该漏洞主要是由于日志在打印时当遇到${后&#xff0c;以:号作为分割&#xff0c;将表达式内容分割成两部…

Leetcode(经典题)day2

H指数 274. H 指数 - 力扣&#xff08;LeetCode&#xff09; 先对数组排序&#xff0c;然后从大的一头开始遍历&#xff0c;只要数组当前的数比现在的h指数大就给h指数1&#xff0c;直到数组当前的数比现在的h指数小的时候结束&#xff0c;这时h的值就是要返回的结果。 排序…

下载安装nodejs npm jarn笔记

下载安装nodejs npm jarn笔记 下载 Node.js安装Node.js修改node全局路径安装yarn 下载 Node.js 下载Node.js 安装Node.js 双击下载的下来的.msi文件运行并安装一直点next。安装路径可以是默认也可自定义。安装完成后Node.js和npm就安装完成了 命令行输入&#xff1a; nod…

LeetCode 面试题02.04.分割链表

LeetCode 面试题02.04.分割链表 C写法 思路&#x1f914;&#xff1a; ​ 将x分为两段&#xff0c;一段放小于x的值&#xff0c;另一段放大于x的值。开辟四个指针lesshead、lesstail、greaterhead、greatertail&#xff0c;head为哨兵位&#xff0c;防止链表为空时情况过于复杂…

推荐一款 uniapp Vaptcha 手势验证码插件

插件地址&#xff1a;VAPTCHA手势验证码 - DCloud 插件市场 具体使用方式可访问插件地址自行查阅

Vue从零到实战

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 非常期待和您一起在这个小…

WEB前端03-CSS3基础

CSS3基础 1.CSS基本概念 CSS是Cascading Style Sheets&#xff08;层叠样式表&#xff09;的缩写&#xff0c;它是一种对Web文档添加样式的简单机制&#xff0c;是一种表现HTML或XML等文件外观样式的计算机语言&#xff0c;是一种网页排版和布局设计的技术。 CSS的特点 纯C…

R语言安装devtools包失败过程总结

R语言安装devtools包时&#xff0c;遇到usethis包总是安装失败&#xff0c;现总结如下方法&#xff0c;亲测可有效 一、usethis包及cli包安装问题 首先&#xff0c;Install.packages("usethis")出现如下错误&#xff0c;定位到是这个cli包出现问题 载入需要的程辑包…

永磁同步电机控制算法--基于 SVM 的无磁链环 DTC

永磁同步电机无磁链环 DTC 通过控制定子磁链交轴分量来直接控制转矩&#xff0c;不再要求控制磁链幅值恒定&#xff0c;省去了传统 DTC 中的磁链环&#xff0c;不仅转矩响应更快&#xff0c;有效抑制了转矩脉动&#xff0c;而且提高了电机功率因数。但无磁链环 DTC 方案仍采用传…

YOWOv2(yowov2)动作识别+Fastreid身份识别 详细安装与实现

首先yowov2是一款简单且实时的时空动作检测方案&#xff0c;fastreid是行人重识别&#xff08;身份识别&#xff09; yowov2介绍链接直达fastreid链接直达为时空动作检测任务设计实时框架仍然是一个挑战。YOWOv2 提出了一种新颖的实时动作检测框架&#xff0c;利用三维骨干和二…