• 电子词典项目


    客服端的代码

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. //client.c
    12. #define N 16
    13. #define R 1 // user register
    14. #define L 2 // user login
    15. #define Q 3 // query word
    16. #define H 4 // history record
    17. #define L_not 5 //登录失败
    18. #define R_success 6 //注册成功
    19. void print1(){
    20. printf("-------------------------------------\n");
    21. printf("| 1.注册 2:登录 3:退出 |\n");
    22. printf("-------------------------------------\n");
    23. printf("请输入你需要的功能");
    24. }
    25. void print2(){
    26. printf("-------------------------------------\n");
    27. printf("| 1.查找单词 2:历史记录 3:退出 |\n");
    28. printf("-------------------------------------\n");
    29. printf("请输入你需要的功能");
    30. }
    31. int flag=0;
    32. struct MSG{
    33. char type;
    34. char name[30];
    35. char data[256];
    36. };
    37. struct MSG msg;
    38. void do_register(int socketfd,struct MSG *msg){
    39. char name[50];
    40. char password[200];
    41. printf("请输入用户名:");
    42. scanf("%s",msg->name);
    43. printf("请输入密码:");
    44. scanf("%s",msg->data);
    45. msg->type=R;
    46. if(-1==send(socketfd,msg,sizeof(struct MSG),0)){
    47. perror("send error ");
    48. exit(0);
    49. }
    50. if(-1==recv(socketfd,msg,sizeof(struct MSG),0)){
    51. perror("recv error ");
    52. exit(0);
    53. }
    54. if(msg->type==R_success){
    55. printf("注册成功\n");
    56. }
    57. return;
    58. }
    59. int login(int socketfd,struct MSG *msg){
    60. ;
    61. memset(msg,0,sizeof(struct MSG));
    62. printf("请输入用户名:");
    63. scanf("%s",msg->name);
    64. printf("请输入密码:");
    65. scanf("%s",msg->data);
    66. msg->type=L;//L代表登录
    67. if(-1 == send(socketfd,msg, sizeof(struct MSG), 0)){
    68. perror("send error");
    69. exit(0);
    70. }
    71. memset(msg,0,sizeof(msg));
    72. if(-1 == recv(socketfd, msg, sizeof(struct MSG), 0)){
    73. perror("recv error");
    74. exit(0);
    75. }
    76. if(msg->type==L){
    77. flag=1;
    78. printf("登录成功\n");
    79. return 1;
    80. }else if(msg->type==L_not){
    81. printf("登录失败");
    82. return 0;
    83. }
    84. }
    85. void do_query(int socketfd,struct MSG *msg)
    86. {
    87. msg->type=Q;
    88. printf("请输入单词\n");
    89. scanf("%s",msg->data);
    90. if(-1 == send(socketfd, msg, sizeof(struct MSG), 0)){
    91. perror("send error");
    92. exit(0);
    93. }
    94. if(-1 == recv(socketfd,msg, sizeof(struct MSG), 0)){
    95. perror("recv error");
    96. exit(0);
    97. }
    98. printf("解释:[%s]\n",msg->data);
    99. return;
    100. }
    101. void do_history(int socketfd, struct MSG *msg)
    102. {
    103. msg->type=H;
    104. printf("历史记录:%s\n",msg->name);
    105. if(-1==send(socketfd,msg,sizeof(struct MSG),0)){
    106. perror("send error");
    107. exit(-1);
    108. }
    109. while(1){
    110. recv(socketfd, msg, sizeof(struct MSG), 0);
    111. if(strcmp(msg->data,"**OVER**")==0){
    112. return;
    113. }
    114. printf("%s\n",msg->data);
    115. }
    116. }
    117. int main(int argc,const char *argv[])
    118. {
    119. if (argc < 3)
    120. {
    121. printf("Usage : %s \n", argv[0]);
    122. exit(-1);
    123. }
    124. struct MSG msg;
    125. memset(&msg,0,sizeof(msg));
    126. int socketfd=socket(AF_INET,SOCK_STREAM,0);
    127. struct sockaddr_in serveraddr;
    128. serveraddr.sin_family=AF_INET;
    129. serveraddr.sin_addr.s_addr=inet_addr(argv[1]);
    130. serveraddr.sin_port=htons(atoi(argv[2]));
    131. if(connect(socketfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr))){
    132. perror("connect error");
    133. exit(0);
    134. }
    135. int choose=0;
    136. while(1){
    137. print1();
    138. scanf("%d",&choose);
    139. switch(choose){
    140. case 1: do_register(socketfd,&msg);
    141. break;
    142. case 2:
    143. if(login(socketfd,&msg)){
    144. while(1){
    145. print2();
    146. scanf("%d",&choose);
    147. switch(choose)
    148. {
    149. case 1:
    150. do_query(socketfd, &msg);
    151. break;
    152. case 2:
    153. do_history(socketfd, &msg);
    154. break;
    155. case 3:
    156. close(socketfd);
    157. exit(0);
    158. }
    159. }
    160. }
    161. break;
    162. case 3: exit(0);
    163. break;
    164. }
    165. }
    166. return 0;
    167. }

    后面是服务端的代码

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. //server.c
    13. #define N 16
    14. #define R 1 // user register
    15. #define L 2 // user login
    16. #define Q 3 // query word
    17. #define H 4 // history record
    18. #define L_not 5 //登录失败
    19. #define R_success 6 //注册成功
    20. struct MSG{
    21. char type;
    22. char name[30];
    23. char data[256];
    24. };
    25. void getdata(char *data)
    26. {
    27. time_t t;
    28. struct tm *tp;
    29. time(&t);
    30. tp = localtime(&t);
    31. sprintf(data, "%d-%d-%d %d:%d:%d", 1900+tp->tm_year, 1+tp->tm_mon, tp->tm_mday, \
    32. tp->tm_hour, tp->tm_min, tp->tm_sec);
    33. }
    34. void do_register(int connectfd,struct MSG *msg,sqlite3 *db){
    35. char sql[512]={0};
    36. sprintf(sql,"insert into usr values('%s','%s')",msg->name,msg->data);
    37. sqlite3_exec(db,sql,NULL,NULL,NULL);
    38. msg->type=R_success;
    39. if(-1==send(connectfd,msg,sizeof(struct MSG),0)){
    40. perror("send error");
    41. exit(0);
    42. }
    43. }
    44. void login(int connectfd,struct MSG *msg,sqlite3 *db,int *flag){
    45. char sql[500]={0};
    46. char *errmsg, **result;
    47. int nrow, ncolumn;
    48. sprintf(sql,"select * from usr where name='%s' and pass='%s'",
    49. msg->name,msg->data);
    50. int row=0;
    51. printf("sql:[%s]",sql);
    52. printf("进入login\n");
    53. if(sqlite3_get_table(db,sql,&result,&row,&ncolumn,&errmsg)!= SQLITE_OK){
    54. printf("error:%s",errmsg);
    55. exit(-1);
    56. }
    57. printf("row=%d\n",row);
    58. if(row>=1) {
    59. *flag=1;
    60. if(-1 == send(connectfd,msg, sizeof(struct MSG), 0)){
    61. perror("send error");
    62. exit(0);
    63. }
    64. }else{
    65. msg->type=L_not;
    66. if(-1 == send(connectfd,msg, sizeof(struct MSG), 0)){
    67. perror("send error");
    68. exit(0);
    69. }
    70. }
    71. }
    72. void do_query(int connectfd,struct MSG *msg, sqlite3 *db)
    73. {
    74. int nbytes=0;
    75. FILE *fp;
    76. if(NULL==(fp=fopen("dict.txt","r"))){
    77. perror("fopen error");
    78. exit(-1);
    79. }
    80. char word[300]={0};
    81. sprintf(word,"%s ",msg->data);
    82. char buff[256]={0};
    83. while(fgets(buff,256,fp)){
    84. if(0==strncmp(buff,word,strlen(word))){
    85. char *p;
    86. p=buff;
    87. while(*p!=' '){
    88. p++;
    89. }
    90. while(*p==' '){
    91. p++;
    92. }
    93. sprintf(msg->data,"%s",p);
    94. printf("yfs:[%s]\n",p);
    95. if(-1 == send(connectfd, msg, sizeof(struct MSG), 0)){
    96. perror("send error");
    97. exit(-1);
    98. }
    99. char sql[128]={0};
    100. char date[100]={0};
    101. getdata(date);
    102. sprintf(sql,"INSERT INTO record VALUES('%s','%s','%s')",
    103. msg->name,date,word);
    104. sqlite3_exec(db,sql,NULL,NULL,NULL);
    105. return;
    106. }
    107. }
    108. printf("找不到该单词");
    109. return;
    110. }
    111. int callback(void *arg,int column,char** f_value,char** f_name){
    112. struct MSG msg;
    113. memset(&msg,0,sizeof(struct MSG));
    114. sprintf(msg.data,"%s|%s",f_value[1],f_value[2]);
    115. if(-1==send(*(int *)arg,&msg,sizeof(struct MSG),0)){
    116. perror("send error");
    117. exit(-1);
    118. }
    119. return 0;
    120. }
    121. void do_history(int connectfd, struct MSG *msg, sqlite3 *db)
    122. {
    123. printf("name=%s\n",msg->name);
    124. char sql[128]={0},*errmsg;
    125. sprintf(sql,"select * from record where name='%s'",msg->name);
    126. if(SQLITE_OK!=sqlite3_exec(db,sql,callback,&connectfd,&errmsg)){
    127. printf("sqlite3_exec[%s]",errmsg);
    128. exit(-1);
    129. }
    130. strcpy(msg->data, "**OVER**");
    131. send(connectfd, msg, sizeof(struct MSG), 0);
    132. }
    133. int main(int argc,const char *argv[])
    134. {
    135. if (argc < 3)
    136. {
    137. printf("Usage : %s \n", argv[0]);
    138. exit(-1);
    139. }
    140. //打开数据库
    141. sqlite3 *my_db;
    142. int ret = sqlite3_open("my.db", &my_db);
    143. if(SQLITE_OK != ret){
    144. printf("errcode:[%d] errstr:[%s]\n", ret, sqlite3_errmsg(my_db));
    145. exit(-1);
    146. }
    147. //执行sql语句
    148. #if 0
    149. int sqlite3_exec(
    150. sqlite3* db, /* An open database */
    151. const char *sql, /* SQL to be evaluated */
    152. int (*callback)(void*,int,char**,char**), /* Callback function */
    153. void *arg, /* 1st argument to callback */
    154. char **errmsg /* Error msg written here */
    155. );
    156. #endif
    157. //创建第一个表用来存储用户信息
    158. char* sql = "create table if not exists usr (name text primary key, pass text);" ;
    159. char* errmsg = NULL;
    160. if(sqlite3_exec(my_db, sql, NULL, NULL, &errmsg) != SQLITE_OK)
    161. {
    162. fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errmsg);
    163. return -1;
    164. }
    165. printf("create table success\n");
    166. //创建第二个表用来存查看记录
    167. char* sql2 = "create table if not exists usr1 (name text,data text, word text);" ;
    168. char* errms = NULL;
    169. if(sqlite3_exec(my_db, sql2, NULL, NULL, &errms) != SQLITE_OK)
    170. {
    171. fprintf(stderr, "__%d__ sqlite3_exec:%s\n", __LINE__, errms);
    172. return -1;
    173. }
    174. printf("create table success\n");
    175. struct MSG msg;
    176. int listenfd=0;
    177. int connectfd=0;
    178. struct sockaddr_in serveraddr;
    179. serveraddr.sin_family=PF_INET;
    180. serveraddr.sin_addr.s_addr=inet_addr(argv[1]);
    181. serveraddr.sin_port=htons(atoi(argv[2]));
    182. if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
    183. {
    184. perror("fail to socket");
    185. exit(-1);
    186. }
    187. if(-1==(bind(listenfd,(struct sockaddr *)&serveraddr,sizeof(serveraddr)))){
    188. perror("bind error");
    189. }
    190. if(listen(listenfd,5)<0){
    191. perror("listen error");
    192. exit(-1);
    193. }
    194. int pid;
    195. while(1){
    196. if((connectfd=accept(listenfd,NULL,NULL))<0){
    197. perror("accept error");
    198. exit(-1);
    199. }
    200. if((pid=fork())<0){
    201. perror("fork error");
    202. exit(0);
    203. }
    204. else if(pid==0){
    205. printf("进入子进程\n");
    206. while(recv(connectfd, &msg, sizeof(msg), 0) > 0){
    207. printf("进入while循环\n");
    208. printf("type:%d\n",msg.type);
    209. int flag=0;//标志是否登录0为没登录
    210. switch(msg.type){
    211. case 1: do_register(connectfd,&msg,my_db);
    212. break;
    213. case L:
    214. printf("进入L\n");
    215. login(connectfd,&msg,my_db,&flag);
    216. break;
    217. case 3: do_query(connectfd,&msg, my_db);
    218. break;
    219. case 4: do_history(connectfd, &msg, my_db);
    220. }
    221. }
    222. }else{
    223. //父进程
    224. }
    225. }
    226. return 0;
    227. }

     

  • 相关阅读:
    Cmake常用命令(二)
    docker、docker-compose安装教程,很详细
    式子表达ds类——多用位置/值域表示未知数+区间覆盖转区间加:CF407E
    bug05:腾讯短信SDK 引用报错 java.lang.NoSuchMethodError: org.json
    AutoCAD的各种方法
    彻底弄明白事件的捕获和冒泡
    Excel自学三部曲_Part3:Excel工作场景分析实战
    Java 对实例进行深拷贝操作
    Redis知识-实战篇(2)
    逆向-还原代码之url_encode (Arm 64)
  • 原文地址:https://blog.csdn.net/m0_58344947/article/details/127096020