• 10.5作业


    磕磕绊绊还是差不多完成了,tcp多客户端在线词典

    代码:

    数据库导入:有点粗糙,不知道怎么搞成两列,一个单词中间还是空格卧槽难搞

    1. #include
    2. #include
    3. #include
    4. #include
    5. #define ERR_MSG(msg) do{\
    6. fprintf(stderr,"__%d__",__LINE__);\
    7. perror(msg);\
    8. }while(0)
    9. int main(int argc, const char *argv[])
    10. {
    11. //词典数据库初始化
    12. sqlite3 *ppDb;
    13. if(sqlite3_open("../ele_directory.db",&ppDb)!=0){
    14. printf("__%d__:sqlite3_open error\n",__LINE__);
    15. return -1;
    16. }
    17. char sql[128]="create table if not exists directory (word char);";
    18. char *errmsg="";
    19. int sql_res=sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg);
    20. if(sql_res!=0){
    21. printf("__%d__:sqlite3_exec :%s\n",__LINE__,errmsg);
    22. return -1;
    23. }
    24. //导入数据
    25. FILE* fp=fopen("../dict.txt","r");
    26. if(NULL==fp){
    27. ERR_MSG("fopen");
    28. return -1;
    29. }
    30. while(1){
    31. char line[64]="";
    32. bzero(line,sizeof(line));
    33. char* fs_res=fgets(line,sizeof(line),fp);
    34. if(NULL==fs_res){
    35. puts("文件读取完毕");
    36. break;
    37. }
    38. bzero(sql,sizeof(sql));
    39. sprintf(sql,"insert into directory values(\"%s\");",line);
    40. sql_res=sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg);
    41. printf("%s\n",sql);
    42. if(sql_res!=0){
    43. printf("__%d__:sqlite3_exec:%s\n",__LINE__,errmsg);
    44. return -1;
    45. }
    46. }
    47. puts("数据导入完成");
    48. if(sqlite3_close(ppDb)!=0){
    49. printf("__%d__:sqlite3_close error\n",__LINE__);
    50. return -1;
    51. }
    52. return 0;
    53. }

    服务器:

    server.c

    1. /*
    2. * function: 在线词典服务器TCP多线程
    3. * @param [ in]
    4. * @param [out]
    5. * @return
    6. */
    7. #include "head.h"
    8. int main(int argc, const char *argv[])
    9. {
    10. //TCP服务器创建
    11. int sfd=socket(AF_INET,SOCK_STREAM,0);
    12. if(sfd<0){
    13. ERR_MSG("socket");
    14. return -1;
    15. }
    16. //允许端口快速复用
    17. int reuse = 1;
    18. if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    19. {
    20. ERR_MSG("setsockopt");
    21. return -1;
    22. }
    23. printf("允许端口快速复用成功\n");
    24. struct sockaddr_in addr;
    25. addr.sin_family=AF_INET;
    26. addr.sin_port=htons(PORT);
    27. addr.sin_addr.s_addr=inet_addr(IP);
    28. if(bind(sfd,(struct sockaddr*)&addr,sizeof(addr))<0){
    29. ERR_MSG("bind");
    30. return -1;
    31. }
    32. printf("绑定成功\n");
    33. if(listen(sfd,128)<0){
    34. ERR_MSG("listen");
    35. return -1;
    36. }
    37. printf("监听状态\n");
    38. struct sockaddr_in cli_addr;
    39. socklen_t addrlen=sizeof(cli_addr);
    40. pthread_t pth;
    41. while(1){
    42. printf("客户端:%s:%d:连接前\n",\
    43. inet_ntoa(cli_addr.sin_addr),\
    44. ntohs(cli_addr.sin_addr.s_addr));
    45. int cfd=accept(sfd,(struct sockaddr*)&cli_addr,&addrlen);
    46. if(cfd<0){
    47. ERR_MSG("accept");
    48. return -1;
    49. }
    50. printf("客户端:%s:%d:连接成功\n",\
    51. inet_ntoa(cli_addr.sin_addr),\
    52. ntohs(cli_addr.sin_port));
    53. //调用线程
    54. cli_info client;
    55. client.cfd=cfd;
    56. client.cli_addr=cli_addr;
    57. if(pthread_create(&pth,NULL,fun,(void*)&client)<0){
    58. ERR_MSG("pthread_create");
    59. return -1;
    60. }
    61. printf("[%d]线程创建成功\n",cfd);
    62. pthread_detach(pth);//分离线程交由系统回收
    63. }
    64. close(sfd);
    65. return 0;
    66. }

    fun.c

    1. #include "head.h"
    2. void * fun(void*arg){
    3. int cfd=((cli_info*)arg)->cfd;
    4. struct sockaddr_in cli_addr=((cli_info*)arg)->cli_addr;
    5. char buf[128]="";
    6. char user[16]="";
    7. //打开数据库
    8. sqlite3 *ppDb;
    9. if(sqlite3_open("../ele_directory.db",&ppDb)!=0){
    10. printf("__%d__:sqlite3_open error\n",__LINE__);
    11. return NULL;
    12. }
    13. //第一层
    14. //登录判断,(注册判断)1代表登录,2代表注册
    15. socklen_t addrlen=sizeof(cli_addr);
    16. int recv_res=recv(cfd,&buf,sizeof(buf),0);
    17. if(recv_res<0){
    18. ERR_MSG("recv");
    19. return NULL;
    20. }
    21. if(recv_res==0){
    22. printf("客户端:%s:%d:断开连接\n",\
    23. inet_ntoa(cli_addr.sin_addr),\
    24. ntohs(cli_addr.sin_port));
    25. return NULL;
    26. }
    27. printf("11111=%s\n",buf);
    28. if(buf[0]=='1'){
    29. //登录判断
    30. printf("login...\n");
    31. int login_res;
    32. while(login_res=isLogin(ppDb,cfd,&user)){
    33. printf("login_res=%d\n",login_res);
    34. char*l_res=NULL;
    35. if(1==login_res)
    36. l_res="1";
    37. else if(2==login_res)
    38. l_res="2";
    39. else if(3==login_res)
    40. l_res="3";
    41. else
    42. return NULL;
    43. printf("l_res=%s\n",l_res);
    44. int send_res=send(cfd,l_res,sizeof(recv_res),0);
    45. if(send<0){
    46. ERR_MSG("send");
    47. return NULL;
    48. }
    49. }
    50. int send_res=send(cfd,"0",1,0);
    51. if(send<0){
    52. ERR_MSG("send");
    53. return NULL;
    54. }
    55. printf("%s登陆成功\n",user);
    56. }else if(buf[0]=='2'){
    57. //注册判断
    58. printf("register...\n");
    59. int register_res;
    60. while(register_res=isRegister(ppDb,cfd,&user)){
    61. printf("register_res=%d",register_res);
    62. if(register_res!=1)
    63. return NULL;
    64. }
    65. printf("%s注册成功\n",user);
    66. return NULL;
    67. }else{
    68. printf("__%d__unknown error\n",__LINE__);
    69. return NULL;
    70. }
    71. //第二层
    72. //单词查询或者记录查询或退出
    73. while(1){
    74. char sql[128]="";
    75. char* errmsg=NULL;
    76. bzero(buf,sizeof(buf));
    77. recv_res=recv(cfd,&buf,sizeof(buf),0);
    78. if(recv_res<0){
    79. ERR_MSG("recv");
    80. return NULL;
    81. }
    82. if(recv_res==0){
    83. printf("客户端:%s:%d:断开连接\n",\
    84. inet_ntoa(cli_addr.sin_addr),\
    85. ntohs(cli_addr.sin_port));
    86. //账号状态设为未登录
    87. bzero(sql,sizeof(sql));
    88. sprintf(sql,"update account set status=\"1\" where account=\"%s\";",user);
    89. if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=0){
    90. fprintf(stderr, "sqlite3_exec: %s __%d__\n", errmsg, __LINE__);
    91. return NULL;
    92. }
    93. }
    94. if(buf[0]=='3'){
    95. //单词查询
    96. printf("word finding...\n");
    97. findWord(ppDb,cfd,user);
    98. }else if(buf[0]=='4'){
    99. //记录查询
    100. printf("record finding...\n");
    101. findRecord(ppDb,cfd,user);
    102. }else if(buf[0]=='5'){
    103. printf("客户端:%s:退出登录\n",user);
    104. //账号状态设为未登录
    105. bzero(sql,sizeof(sql));
    106. sprintf(sql,"update account set status=\"1\" where account=\"%s\";",user);
    107. if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=0){
    108. fprintf(stderr, "sqlite3_exec: %s __%d__\n", errmsg, __LINE__);
    109. return NULL;
    110. }
    111. return NULL;
    112. }else{
    113. printf("__%d__unknown error\n",__LINE__);
    114. return NULL;
    115. }
    116. }
    117. //关闭数据库
    118. if(sqlite3_close(ppDb)!=0){
    119. printf("__%d__:sqlite3_close error\n",__LINE__);
    120. return NULL;
    121. }
    122. }
    123. int isLogin(sqlite3 *ppDb,int cfd,char (*user)[]){
    124. char account[10]="";
    125. char password[10]="";
    126. char buf[128]="";
    127. //数据库查询,账号是否存在,若存在密码是否正确
    128. int recv_res=recv(cfd,&buf,sizeof(buf),0);
    129. if(recv_res<0){
    130. ERR_MSG("recv");
    131. return -1;
    132. }else if(recv_res==0){
    133. printf("客户端:%s:断开连接\n",*user);
    134. return -1;
    135. }
    136. printf("buf=%s\n",buf);
    137. sscanf(buf,"%s %s",account,password);
    138. char sql[128]="";
    139. char** pres = NULL;
    140. int row, column;
    141. char* errmsg = NULL;
    142. sprintf(sql,"select * from account where account=\"%s\";",account);
    143. if(sqlite3_get_table(ppDb,sql,&pres,&row,&column,&errmsg)!=0){
    144. fprintf(stderr, "sqlite3_get_table: %s __%d__\n", errmsg, __LINE__);
    145. return -1;
    146. }
    147. printf("sql=%s\n",sql);
    148. int exist_res=0;//是否存在
    149. for (int i=0; i<(row+1)*column; i++)
    150. {
    151. printf("%s\t", pres[i]);
    152. if((i+1)%column == 0)
    153. putchar('\n');
    154. exist_res=1;//存在为1
    155. }
    156. printf("exist_res=%d\n",exist_res);
    157. if(0==exist_res){
    158. printf("账号不存在\n");
    159. return 1;//账号不存在返回1
    160. }else{
    161. printf("账号存在,判断密码\n");
    162. bzero(sql,sizeof(sql));
    163. sprintf(sql,"select status from account where account=\"%s\" and password=\"%s\";",account,password);
    164. if(sqlite3_get_table(ppDb,sql,&pres,&row,&column,&errmsg)!=0){
    165. fprintf(stderr, "sqlite3_get_table: %s __%d__\n", errmsg, __LINE__);
    166. return -1;
    167. }
    168. printf("sql=%s\n",sql);
    169. int login_res=0;//是否存在账号密码匹配
    170. for (int i=column; i<(row+1)*column; i++)
    171. {
    172. printf("%s\t", pres[i]);
    173. if((i+1)%column == 0)
    174. putchar('\n');
    175. login_res=1;//存在为1
    176. }
    177. if(0==login_res){
    178. printf("密码错误\n");
    179. return 2;//账号密码错误返回2
    180. }else if(0==strcmp(pres[column],"0")){
    181. printf("账号重复登录\n");
    182. return 3;
    183. }
    184. //账号状态设为已登录
    185. bzero(sql,sizeof(sql));
    186. sprintf(sql,"update account set status=\"0\" where account=\"%s\";",account);
    187. if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=0){
    188. fprintf(stderr, "sqlite3_exec: %s __%d__\n", errmsg, __LINE__);
    189. return -1;
    190. }
    191. printf("密码正确\n");
    192. strcpy(*user,account);
    193. return 0;//登陆成功返回0
    194. }
    195. }
    196. int isRegister(sqlite3 *ppDb,int cfd,char(*user)[]){
    197. char account[10]="";
    198. char password[10]="";
    199. char buf[128]="";
    200. //数据库查询,账号是否存在
    201. bzero(buf,sizeof(buf));
    202. int recv_res=recv(cfd,&buf,sizeof(buf),0);
    203. if(recv_res<0){
    204. ERR_MSG("recv");
    205. return -1;
    206. }else if(recv_res==0){
    207. printf("客户端:%s:断开连接\n",*user);
    208. return -1;
    209. }
    210. printf("buf=%s\n",buf);
    211. sscanf(buf,"%s %s",account,password);
    212. char sql[128]="";
    213. char** pres = NULL;
    214. int row, column;
    215. char* errmsg = NULL;
    216. sprintf(sql,"select * from account where account=\"%s\";",account);
    217. if(sqlite3_get_table(ppDb,sql,&pres,&row,&column,&errmsg)!=0){
    218. fprintf(stderr, "sqlite3_get_table: %s __%d__\n", errmsg, __LINE__);
    219. return -1;
    220. }
    221. printf("sql=%s\n",sql);
    222. int reg_res=0;//是否存在
    223. for (int i=0; i<(row+1)*column; i++)
    224. {
    225. printf("%s\t", pres[i]);
    226. if((i+1)%column == 0)
    227. putchar('\n');
    228. reg_res=1;//存在为1
    229. }
    230. printf("reg_res=%d\n",reg_res);
    231. if(0==reg_res){
    232. //不存在
    233. printf("账号不存在,开始注册:\n");
    234. bzero(sql,sizeof(sql));
    235. sprintf(sql,"insert into account values (\"%s\",\"%s\",\"1\");",account,password);
    236. printf("sql=%s\n",sql);
    237. if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=0){
    238. fprintf(stderr, "sqlite3_get_table: %s __%d__\n", errmsg, __LINE__);
    239. return -1;
    240. }
    241. printf("注册完成\n");
    242. strcpy(*user,account);
    243. char *r_res="0";//注册成功
    244. printf("r_res=%s\n",r_res);
    245. int send_res=send(cfd,r_res,sizeof(recv_res),0);
    246. if(send<0){
    247. ERR_MSG("send");
    248. return -1;
    249. }
    250. return 0;
    251. }else{
    252. printf("账号已存在\n");
    253. char *r_res="1";
    254. printf("r_res=%s\n",r_res);
    255. int send_res=send(cfd,r_res,sizeof(recv_res),0);
    256. if(send<0){
    257. ERR_MSG("send");
    258. return -1;
    259. }
    260. return 1;
    261. }
    262. }
    263. int findWord(sqlite3 *ppDb,int cfd,char*user){
    264. printf("user=%s\n",user);
    265. char buf[128]="";
    266. char word[32]="";
    267. char sql[128]="";
    268. char*errmsg=NULL;
    269. int recv_res=recv(cfd,&word,sizeof(buf),0);
    270. if(recv_res<0){
    271. ERR_MSG("recv");
    272. return -1;
    273. }
    274. if(recv_res==0){
    275. printf("客户端:%s:断开连接\n",user);
    276. //账号状态设为未登录
    277. sprintf(sql,"update account set status=\"1\" where account=\"%s\";",user);
    278. if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=0){
    279. fprintf(stderr, "sqlite3_exec: %s __%d__\n", errmsg, __LINE__);
    280. return -1;
    281. }
    282. }
    283. printf("word=%s\n",word);
    284. char** pres = NULL;
    285. int row, column;
    286. bzero(sql,sizeof(sql));
    287. sprintf(sql,"select * from directory where word like \"%s%\";",word);
    288. if(sqlite3_get_table(ppDb,sql,&pres,&row,&column,&errmsg)!=0){
    289. fprintf(stderr, "sqlite3_get_table: %s __%d__\n", errmsg, __LINE__);
    290. return -1;
    291. }
    292. printf("sql=%s\n",sql);
    293. for (int i=column; i<(row+1)*column; i++)
    294. {
    295. bzero(buf,sizeof(buf));
    296. printf("%s\t", pres[i]);
    297. strcpy(buf,pres[i]);
    298. strcat(buf," ");
    299. if((i+1)%column == 0)
    300. strcat(buf,"\n");
    301. printf("buf=%s\n",buf);
    302. int send_res=send(cfd,buf,sizeof(buf),0);
    303. if(send<0){
    304. ERR_MSG("send");
    305. return -1;
    306. }
    307. if((i+1)%column == 0)
    308. putchar('\n');
    309. }
    310. int send_res=send(cfd,"find over",strlen("find over"),0);
    311. if(send<0){
    312. ERR_MSG("send");
    313. return -1;
    314. }
    315. printf("查找完毕\n");
    316. //记录(word,time,user)
    317. bzero(sql,sizeof(sql));
    318. time_t t=time(NULL);
    319. struct tm *timeinfo=localtime(&t);
    320. char r_time[64]="";
    321. sprintf(r_time,"%d-%d-%d:%d:%d:%d",\
    322. timeinfo->tm_year+1900,timeinfo->tm_mon+1,timeinfo->tm_mday,\
    323. timeinfo->tm_hour,timeinfo->tm_min,timeinfo->tm_sec);
    324. printf("word:%s;time:%s;user:%s\n",word,r_time,user);
    325. printf("sql=%s\n",sql);
    326. sprintf(sql,"insert into record values(\"%s\",\"%s\",\"%s\");",word,r_time,user);
    327. printf("sql=%s\n",sql);
    328. if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=0){
    329. fprintf(stderr, "sqlite3_get_table: %s __%d__\n", errmsg, __LINE__);
    330. return -1;
    331. }
    332. printf("记录完成\n");
    333. return 0;
    334. }
    335. int findRecord(sqlite3 *ppDb,int cfd,char*user){
    336. printf("user=%s\n",user);
    337. char sql[128]="";
    338. char** pres = NULL;
    339. int row, column;
    340. char* errmsg = NULL;
    341. char buf[128]="";
    342. sprintf(sql,"select * from record where user=\"%s\";",user);
    343. if(sqlite3_get_table(ppDb,sql,&pres,&row,&column,&errmsg)!=0){
    344. fprintf(stderr, "sqlite3_get_table: %s __%d__\n", errmsg, __LINE__);
    345. return -1;
    346. }
    347. printf("sql=%s\n",sql);
    348. for (int i=column; i<(row+1)*column; i++)
    349. {
    350. bzero(buf,sizeof(buf));
    351. printf("%s;%ld\t", pres[i],strlen(pres[i]));
    352. strcpy(buf,pres[i]);
    353. strcat(buf," ");
    354. if((i+1)%column == 0)
    355. strcat(buf,"\n");
    356. int send_res=send(cfd,buf,sizeof(buf),0);
    357. if(send<0){
    358. ERR_MSG("send");
    359. return -1;
    360. }
    361. if((i+1)%column == 0)
    362. putchar('\n');
    363. }
    364. printf("查找完毕\n");
    365. int send_res=send(cfd,"find record over",strlen("find record over"),0);
    366. if(send<0){
    367. ERR_MSG("send");
    368. return -1;
    369. }
    370. printf("记录查找完毕\n");
    371. return 0;
    372. }

    head.h

    1. #ifndef __HEAD_H__
    2. #define __HEAD_H__
    3. #define ERR_MSG(msg) do{\
    4. fprintf(stderr,"__%d__",__LINE__);\
    5. perror(msg);\
    6. }while(0)
    7. #define PORT 8888
    8. #define IP "192.168.1.6"
    9. #include
    10. #include
    11. #include
    12. #include
    13. #include
    14. #include
    15. #include
    16. #include
    17. #include
    18. #include
    19. typedef struct clientInfo{
    20. int cfd;
    21. struct sockaddr_in cli_addr;
    22. }cli_info;
    23. void * fun(void*arg);
    24. int isLogin(sqlite3 *ppDb,int cfd,char(*user)[]);
    25. int isRegister(sqlite3 *ppDb,int cfd,char(*user)[]);
    26. int findWord(sqlite3 *ppDb,int cfd,char*user);
    27. int findRecord(sqlite3 *ppDb,int cfd,char*user);
    28. #endif

    客户端:

    client.c

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. //自定义报错提示
    9. #define ERR_MSG(msg) do{\
    10. fprintf(stderr,"__%d__",__LINE__);\
    11. perror(msg);\
    12. }while(0)
    13. #define SER_PORT 8888
    14. #define SER_IP "192.168.1.6"
    15. /*
    16. * function: TCP客户端
    17. * @param [ in]
    18. * @param [out]
    19. * @return
    20. */
    21. int login(int cfd);
    22. int registe(int cfd);
    23. int findWord(int cfd);
    24. int findRecord(int cfd);
    25. int main(int argc, const char *argv[])
    26. {
    27. //1.创建socket套接字,
    28. int cfd=socket(AF_INET,SOCK_STREAM,0);
    29. if(cfd<0){
    30. ERR_MSG("socket");
    31. return -1;
    32. }
    33. puts("socket create");
    34. //允许端口快速复用
    35. int reuse = 1;
    36. if(setsockopt(cfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0)
    37. {
    38. ERR_MSG("setsockopt");
    39. return -1;
    40. }
    41. printf("允许端口快速复用成功\n");
    42. //2.连接服务端connect
    43. struct sockaddr_in addr;
    44. addr.sin_family=AF_INET;
    45. addr.sin_port=htons(SER_PORT);
    46. addr.sin_addr.s_addr=inet_addr(SER_IP);
    47. if(connect(cfd,(struct sockaddr*)&addr,sizeof(addr))<0){
    48. ERR_MSG("connect");
    49. return -1;
    50. }
    51. puts("connect success");
    52. printf("-------------------------------------------------\n");
    53. printf("-------Wecome to eletronic directory system------\n");
    54. printf("-------------------------------------------------\n");
    55. printf("--------------1.登录-----------------------------\n");
    56. printf("--------------2.注册-----------------------------\n");
    57. printf("--------------3.退出-----------------------------\n");
    58. printf("-------------------------------------------------\n");
    59. printf("-------------------------------------------------\n");
    60. printf("-------------------------------------------------\n");
    61. char choice=0;
    62. while(1){
    63. printf("请选择1.登录;2注册;3.退出>>>");
    64. choice=getchar();
    65. while(getchar()!='\n');
    66. switch(choice){
    67. case '1':
    68. //登录
    69. if(5==login(cfd))
    70. return 0;
    71. break;
    72. case '2':
    73. //注册
    74. if(0==registe(cfd))
    75. return 0;
    76. case '3':
    77. //退出
    78. return 0;
    79. default :
    80. printf("错误输入,请重输\n");
    81. break;
    82. }
    83. }
    84. close(cfd);
    85. return 0;
    86. }
    87. int login(int cfd){
    88. char buf[32]="";
    89. char status=0;
    90. int send_res=send(cfd,"1",1,0);
    91. if(send_res<0){
    92. ERR_MSG("send");
    93. return -1;
    94. }
    95. while(1){
    96. printf("请输入账号 密码 (空格分开)>>>");
    97. fgets(buf,sizeof(buf),stdin);
    98. int send_res=send(cfd,buf,sizeof(buf),0);
    99. if(send_res<0){
    100. ERR_MSG("send");
    101. return -1;
    102. }
    103. int recv_res=recv(cfd,&status,sizeof(buf),0);
    104. if(recv_res<0){
    105. ERR_MSG("recv");
    106. return -1;
    107. }else if(recv_res==0){
    108. printf("socket peer has shutdown\n");
    109. return -2;
    110. }
    111. printf("status=%c\n",status);
    112. if('1'==status){
    113. printf("账号不存在\n");
    114. }else if('2'==status){
    115. printf("密码错误\n");
    116. }else if('3'==status){
    117. printf("账号已登录\n");
    118. }else{
    119. printf("登陆成功\n");
    120. break;}
    121. }
    122. printf("-----------------------------------\n");
    123. printf("-------请输入你的操作--------------\n");
    124. printf("---------3.查找单词----------------\n");
    125. printf("---------4.记录查询----------------\n");
    126. printf("---------5.退出--------------------\n");
    127. printf("-----------------------------------\n");
    128. char choice=0;
    129. while(1){
    130. printf("请选择>>>3.查找单词4.记录查询5.退出");
    131. choice=getchar();
    132. while(getchar()!='\n');
    133. switch(choice){
    134. case '3':
    135. //查找单词
    136. findWord(cfd);
    137. break;
    138. case '4':
    139. //查询记录
    140. findRecord(cfd);
    141. break;
    142. case '5':
    143. //退出
    144. send_res=send(cfd,"5",1,0);
    145. if(send_res<0){
    146. ERR_MSG("send");
    147. return -1;
    148. }
    149. return 5;
    150. default :
    151. printf("错误输入,请重输\n");
    152. break;
    153. }
    154. }
    155. return 0;
    156. }
    157. int registe(int cfd){
    158. char buf[32]="";
    159. char status=0;
    160. int send_res=send(cfd,"2",1,0);
    161. if(send_res<0){
    162. ERR_MSG("send");
    163. return -1;
    164. }
    165. while(1){
    166. bzero(buf,sizeof(buf));
    167. printf("请输入账号 密码 (空格分开)>>>");
    168. fgets(buf,sizeof(buf),stdin);
    169. printf("%s",buf);
    170. int send_res=send(cfd,buf,sizeof(buf),0);
    171. if(send_res<0){
    172. ERR_MSG("send");
    173. return -1;
    174. }
    175. int recv_res=recv(cfd,&status,sizeof(buf),0);
    176. if(recv_res<0){
    177. ERR_MSG("recv");
    178. return -1;
    179. }else if(recv_res==0){
    180. printf("socket peer has shutdown\n");
    181. return -2;
    182. }
    183. printf("status=%c\n",status);
    184. if(status=='0'){
    185. printf("注册成功,请重新进入系统\n");
    186. return 0;
    187. }else if(status=='1'){
    188. printf("账号已存在\n");
    189. }
    190. }
    191. }
    192. int findWord(int cfd){
    193. char buf[128]="";
    194. int send_res=send(cfd,"3",1,0);
    195. if(send_res<0){
    196. ERR_MSG("send");
    197. return -1;
    198. }
    199. printf("请输入你要查询的单词(支持模糊查询)>>>");
    200. fgets(buf,sizeof(buf),stdin);
    201. printf("%s",buf);
    202. buf[strlen(buf)-1] = '\0';
    203. send_res=send(cfd,buf,sizeof(buf),0);
    204. if(send_res<0){
    205. ERR_MSG("send");
    206. return -1;
    207. }
    208. while(1){
    209. bzero(buf,sizeof(buf));
    210. int recv_res=recv(cfd,buf,sizeof(buf),0);
    211. if(recv_res<0){
    212. ERR_MSG("recv");
    213. return -1;
    214. }else if(recv_res==0){
    215. printf("socket peer has shutdown\n");
    216. return -2;
    217. }
    218. if(0==strcmp(buf,"find over"))
    219. break;
    220. printf("%s\n",buf);
    221. }
    222. putchar(10);
    223. printf("查找单词完毕\n");
    224. return 0;
    225. }
    226. int findRecord(int cfd){
    227. char buf[128]="";
    228. int send_res=send(cfd,"4",1,0);
    229. if(send_res<0){
    230. ERR_MSG("send");
    231. return -1;
    232. }
    233. while(1){
    234. bzero(buf,sizeof(buf));
    235. int recv_res=recv(cfd,buf,sizeof(buf),0);
    236. if(recv_res<0){
    237. ERR_MSG("recv");
    238. return -1;
    239. }else if(recv_res==0){
    240. printf("socket peer has shutdown\n");
    241. return -2;
    242. }
    243. if(0==strcmp(buf,"find record over"))
    244. break;
    245. printf("%s",buf);
    246. }
    247. putchar(10);
    248. printf("查找记录完毕\n");
    249. return 0;
    250. }

    又是依托屎山代码,好乱,不想去整理了

  • 相关阅读:
    【老生谈算法】matlab实现控制系统稳定性——控制系统
    Hudi(三)集成Flink
    ELF 和 二进制文件的区别
    DlhSoft Gantt Chart Hyper Library for HTML5 Standard Edition
    自动化工具:PyAutoGUI的鼠标与键盘控制,解放双手的利器
    网页设计作业
    ora-00439 未启用 bit-mapped indexes
    计算机毕业设计Java靓车汽车销售网站(源码+mysql数据库+系统+lw文档)
    数字孪生扫除智慧城市“盲点”,赋能社会数字发展
    Webpack与Vite热更新差异对比
  • 原文地址:https://blog.csdn.net/weixin_53762703/article/details/133563372