在线词典实现框架
应用综合项目:
1、在线英英词典
2、项目功能描述
3、用户注册和登录验证
4、服务器端将用户信息和历史记录保存在数据库中,客户端输入用户名和密码,服务器端在数据库中查找、匹配,返回结果
5、单次在线翻译
6、根据客户端输入的单次在字典文件中搜索
7、历史记录查询
8、项目分析
项目流程框架:
客户端
服务器端:
实现:
在终端,创建数据库,dic.db
创建记录table
create table user (name char , passwd char , date char);
create table record (name char , date char , word char);
在原有基础上增加实现
1、在搭建的框架中实现并发,实现多客户端的注册登录
2、同时支持管理员(用户名:root,密码:1)和普通用户
3、管理员可以查询所有用户的使用记录
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <sys/types.h> 4 #include <sys/socket.h> 5 #include <sys/socket.h> 6 #include <netinet/in.h> 7 #include <arpa/inet.h> 8 #include <string.h> 9 10 #define N 32 11 12 #define R 1 //usr--register 13 #define L 2 //usr--login 14 #define Q 3 //usr--query 15 #define H 4 //root--history 16 #define Y 5 //root--login 17 #define X 6 //root--user record 18 19 //定义通信双方的MSG信息结构体 20 typedef struct { 21 int type; 22 char name[N]; 23 char data[256]; 24 }MSG; 25 26 int do_regiser(int sockfd, MSG *msg); 27 int do_login(int sockfd, MSG *msg); 28 int do_login_root(int sockfd, MSG *msg); 29 int do_query(int sockfd, MSG *msg); 30 int do_history(int sockfd, MSG *msg); 31 int do_user_record(int sockfd, MSG *msg); 32 33 34 int main(int argc, const char *argv[]) 35 { 36 int sockfd; 37 struct sockaddr_in serveraddr; 38 int num; 39 MSG msg; 40 41 if(argc != 3) 42 { 43 printf("Usage :%s serverip port. ", argv[0]); 44 return -1; 45 } 46 47 if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) 48 { 49 perror("fail to socket! "); 50 return -1; 51 } 52 53 bzero(&serveraddr, sizeof(serveraddr)); 54 serveraddr.sin_family = AF_INET; 55 serveraddr.sin_addr.s_addr = inet_addr(argv[1]); //点分形式转为二进制数据 56 serveraddr.sin_port = htons(atoi(argv[2])); //先将字符型转为int型数据,再HBD->NBD 57 58 if(connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)) < 0) 59 { 60 perror("connect error! "); 61 return -1; 62 } 63 64 printf("Client starting... "); 65 66 while(1) 67 { 68 printf("*************************************************** "); 69 printf("* 1.regiser 2.login 3.root login 4.quit * "); 70 printf("*************************************************** "); 71 printf("Please choose:"); 72 73 scanf("%d",&num); 74 getchar(); //每次输入完毕会产生垃圾字符,清除 75 76 switch(num) 77 { 78 case 1: 79 do_regiser(sockfd, &msg); 80 break; 81 case 2: 82 if(do_login(sockfd,&msg) == 1) 83 { 84 goto next; //登录成功,跳到二级菜单 85 } 86 break; 87 case 3: 88 if(do_login_root(sockfd, &msg) == 1) 89 { 90 goto next_root; 91 } 92 case 4: 93 close(sockfd); 94 exit(0); 95 break; 96 default: 97 printf("Invalid data cmd. "); 98 } 99 } 100 101 next: //二级菜单 102 while(1) 103 { 104 printf("*************************************************** "); 105 printf("* 1.query word 2.quit "); 106 printf("*************************************************** "); 107 printf("Please choose:"); 108 109 scanf("%d",&num); 110 getchar(); //每次输入完毕会产生垃圾字符,清除 111 112 switch(num) 113 { 114 case 1: 115 do_query(sockfd, &msg); 116 break; 117 case 2: 118 close(sockfd); 119 exit(1); 120 break; 121 default: 122 printf("Invalid data cmd. "); 123 } 124 125 } 126 127 128 next_root: //管理员二级菜单 129 while(1) 130 { 131 printf("*************************************************** "); 132 printf("* 1.query word 2.history 3.user record 4.quit * "); 133 printf("*************************************************** "); 134 printf("Please choose:"); 135 136 scanf("%d",&num); 137 getchar(); //每次输入完毕会产生垃圾字符,清除 138 139 switch(num) 140 { 141 case 1: 142 do_query(sockfd, &msg); 143 break; 144 case 2: 145 do_history(sockfd, &msg); 146 break; 147 case 3: 148 do_user_record(sockfd, &msg); 149 break; 150 case 4: 151 close(sockfd); 152 exit(1); 153 break; 154 default: 155 printf("Invalid data cmd. "); 156 } 157 158 } 159 return 0; 160 } 161 162 int do_regiser(int sockfd, MSG *msg) 163 { 164 msg->type = R; 165 printf("Intput name:"); 166 scanf("%s",msg->name); 167 getchar(); 168 169 printf("Intput password:"); 170 scanf("%s",msg->data); 171 172 if(send(sockfd, msg, sizeof(MSG), 0) < 0) 173 { 174 printf("fail to send. "); 175 return -1; 176 } 177 178 if(recv(sockfd, msg, sizeof(MSG), 0) < 0) 179 { 180 printf("fail to recv. "); 181 exit(1); 182 } 183 184 //ok! or usr already exist 185 printf("%s ",msg->data); 186 187 return 0; 188 189 } 190 191 int do_login(int sockfd, MSG *msg) 192 { 193 msg->type = L; 194 195 printf("Intput name:"); 196 scanf("%s",msg->name); 197 getchar(); 198 199 printf("Intput password:"); 200 scanf("%s",msg->data); 201 202 if(send(sockfd, msg, sizeof(MSG), 0) < 0) 203 { 204 printf("fail to send. "); 205 exit(1); 206 } 207 208 if(recv(sockfd, msg, sizeof(MSG), 0) < 0) 209 { 210 printf("fail to recv. "); 211 return -1; 212 } 213 214 if(strncmp(msg->data, "ok", 3) == 0) 215 { 216 printf("Login ok! "); 217 return 1; 218 } 219 else 220 { 221 printf("%s ",msg->data); 222 } 223 224 225 return 0; 226 } 227 228 229 int do_login_root(int sockfd, MSG *msg) 230 { 231 msg->type = Y; 232 233 printf("name: root"); 234 sprintf(msg->name,"root"); 235 getchar(); 236 237 printf("Intput password:"); 238 scanf("%s",msg->data); 239 240 if(send(sockfd, msg, sizeof(MSG), 0) < 0) 241 { 242 printf("fail to send. "); 243 exit(1); 244 } 245 246 if(recv(sockfd, msg, sizeof(MSG), 0) < 0) 247 { 248 printf("fail to recv. "); 249 return -1; 250 } 251 252 if(strncmp(msg->data, "ok", 3) == 0) 253 { 254 printf("ROOT Login ok! "); 255 return 1; 256 } 257 else 258 { 259 printf("%s ",msg->data); 260 } 261 262 263 return 0; 264 } 265 266 int do_query(int sockfd, MSG *msg) 267 { 268 msg->type = Q; 269 puts("-------------------------- "); 270 271 while(1) 272 { 273 printf("Intput word:"); 274 scanf("%s",msg->data); 275 276 //客户端输入结束并返回上一层 277 if(strncmp(msg->data,"#",1) == 0) //比较前1个字符,相同返0 278 break; 279 280 //将要查询的单词发送给服务器 281 if(send(sockfd, msg, sizeof(MSG), 0) < 0) 282 { 283 printf("file to send. "); 284 return -1; 285 } 286 287 //等待接收服务器传递回来的注释单词信息 288 if(recv(sockfd, msg, sizeof(MSG),0) < 0) 289 { 290 printf("file to recv. "); 291 return -1; 292 } 293 printf("%s ",msg->data); 294 } 295 296 return 0; 297 } 298 299 int do_history(int sockfd, MSG *msg) 300 { 301 msg->type = H; 302 303 printf("Intput the user name: "); 304 printf("** name * query date * word ** "); 305 scanf("%s",msg->name); 306 send(sockfd, msg, sizeof(MSG), 0); 307 308 //接收服务器传递回来的历史记录 309 while(1) 310 { 311 recv(sockfd, msg, sizeof(MSG), 0); 312 313 if(msg->data[0] == '