基于C/S架构的网络聊天室

转自:https://blog.csdn.net/xym_smart/article/details/78185944

 

服务器端

 

 
  1. #include <stdio.h>

  2. #include <string.h>

  3. #include <sys/types.h>

  4. #include <sys/socket.h>

  5. #include <arpa/inet.h>

  6. #include <pthread.h>

  7. #include <sqlite3.h>

  8. #include <time.h>

  9. #include <sys/stat.h>

  10. #include <fcntl.h>

  11. #include <unistd.h>

  12. #include "LinkList.h"

  13.  
  14. #define PORT 9999

  15.  
  16. typedef struct _msg

  17. {

  18. int cmd;

  19. int flag;

  20. char username[20];

  21. char password[20];

  22. char msg[1024];

  23. char frname[20];

  24. char toname[20];

  25. char time[50];

  26. char filename[20];

  27. }Msg;

  28.  
  29. sqlite3 *database;

  30.  
  31. Node *head;

  32.  
  33. //初始化套接字,返回监听套接字

  34. int Init_Socket()

  35. {

  36. //创建套接字

  37. int listen_socket = socket(AF_INET, SOCK_STREAM, 0);

  38. if(listen_socket == -1)

  39. {

  40. perror("socket");

  41. return -1;

  42. }

  43.  
  44. //命名套接字

  45. struct sockaddr_in addr;

  46. memset(&addr, 0, sizeof(addr));

  47. addr.sin_family = AF_INET;

  48. addr.sin_port = htons(PORT);

  49. addr.sin_addr.s_addr = htonl(INADDR_ANY);

  50.  
  51. int ret = bind(listen_socket, (struct sockaddr *)&addr, sizeof(addr));

  52. if(ret == -1)

  53. {

  54. perror("bind");

  55. return -1;

  56. }

  57.  
  58. //监听套接字

  59. ret = listen(listen_socket, 5);

  60. if(ret == -1)

  61. {

  62. perror("listen");

  63. return -1;

  64. }

  65.  
  66. printf("等待客户端连接......\n");

  67.  
  68. return listen_socket;

  69. }

  70.  
  71. //处理客户端连接,返回与客户端通信的套接字

  72. int MyAccept(int listen_socket)

  73. {

  74. //接收套接字

  75. struct sockaddr_in client_addr;

  76. int len = sizeof(client_addr);

  77.  
  78. int client_socket = accept(listen_socket, (struct sockaddr *)&client_addr, &len);

  79. if(client_socket == -1)

  80. {

  81. perror("accept");

  82. return -1;

  83. }

  84.  
  85. printf("成功接收一个客户端:%s\n",inet_ntoa(client_addr.sin_addr));

  86.  
  87. return client_socket;

  88. }

  89.  
  90. //注册

  91. void reg(int client_socket, Msg *msg)

  92. {

  93. char *errmsg = NULL;

  94. char **resultp = NULL;

  95. int nrow, ncolumn;

  96. char *sql = "select * from login";

  97. int ret = sqlite3_get_table(database, sql, &resultp, &nrow, &ncolumn, &errmsg);

  98. if (ret != SQLITE_OK)

  99. {

  100. printf ("数据库操作失败:%s\n", errmsg);

  101. return;

  102. }

  103. printf("查找成功\n");

  104.  
  105. int i;

  106. for(i = 0;i < (nrow+1) * ncolumn;i++)

  107. {

  108. if(strcmp(resultp[i],msg->username) == 0)

  109. {

  110. printf("用户已存在\n");

  111. msg->cmd = -1;

  112. write(client_socket,msg,sizeof(Msg));

  113. return;

  114. }

  115. }

  116.  
  117. char buf[100];

  118. sprintf (buf, "insert into login values('%s', '%s')", msg->username,msg->password);

  119. ret = sqlite3_exec(database, buf, NULL, NULL, &errmsg);

  120. if (ret != SQLITE_OK)

  121. {

  122. printf ("数据库操作失败:%s\n", errmsg);

  123. return;

  124. }

  125. printf("%s用户注册成功\n",msg->username);

  126.  
  127. msg->cmd = 1001;

  128.  
  129. write(client_socket,msg,sizeof(Msg));

  130. }

  131.  
  132. //用户登录

  133. void login(int client_socket, Msg *msg)

  134. {

  135. char *errmsg = NULL;

  136. char **resultp = NULL;

  137. int nrow, ncolumn;

  138. char *sql = "select * from login";

  139. int ret = sqlite3_get_table(database, sql, &resultp, &nrow, &ncolumn, &errmsg);

  140. if (ret != SQLITE_OK)

  141. {

  142. printf ("数据库操作失败:%s\n", errmsg);

  143. return;

  144. }

  145. printf("查找成功\n");

  146.  
  147. int i;

  148. for(i = 0;i < (nrow+1) * ncolumn;i++)

  149. {

  150. if(strcmp(resultp[i],msg->username) == 0 && strcmp(resultp[i+1],msg->password) == 0)

  151. {

  152. printf("%s登录成功\n",msg->username);

  153. User user;

  154. strcpy(user.username,msg->username);

  155. user.client_socket = client_socket;

  156. Insert_Last(head,user);

  157. Display(head);

  158. msg->cmd = 1002;

  159. write(client_socket,msg,sizeof(Msg));

  160. return;

  161. }

  162. }

  163.  
  164. msg->cmd = -2;

  165. printf("用户名或密码错误,%s登录失败\n",msg->username);

  166. write(client_socket,msg,sizeof(Msg));

  167. }

  168.  
  169. //显示在线好友列表

  170. void Display_Friend(int client_socket,Msg *msg)

  171. {

  172. Node* tmp = head->next;

  173. while(tmp)

  174. {

  175. strcpy(msg->username,tmp->data.username);

  176. write(client_socket,msg,sizeof(Msg));

  177. tmp = tmp->next;

  178. }

  179. }

  180.  
  181. //群聊

  182. void chat_all(int client_socket,Msg *msg)

  183. {

  184. Node* tmp = head->next;

  185. Node* tmp1 = head->next;

  186.  
  187. while(tmp1)

  188. {

  189. if(tmp1->data.client_socket == client_socket)

  190. {

  191. strcpy(msg->frname,tmp1->data.username);

  192. break;

  193. }

  194. tmp1 = tmp1->next;

  195. }

  196. strcpy(msg->toname,"all");

  197. printf("%s发了一次群消息\n",msg->frname);

  198.  
  199. while(tmp)

  200. {

  201. write(tmp->data.client_socket,msg,sizeof(Msg));

  202. tmp = tmp->next;

  203. }

  204. }

  205.  
  206. //私聊

  207. void chat_along(int client_socket,Msg *msg)

  208. {

  209. Node* tmp1 = head->next;

  210. Node* tmp2 = head->next;

  211.  
  212. while(tmp1)

  213. {

  214. if(tmp1->data.client_socket == client_socket)

  215. {

  216. strcpy(msg->frname,tmp1->data.username);

  217. break;

  218. }

  219. tmp1 = tmp1->next;

  220. }

  221. printf("%s发给%s一条消息\n",msg->frname,msg->toname);

  222.  
  223. while(tmp2)

  224. {

  225. if(strcmp(tmp2->data.username,msg->toname) == 0)

  226. {

  227. msg->flag = 101;

  228. write(tmp2->data.client_socket,msg,sizeof(Msg));

  229. return;

  230. }

  231. tmp2 = tmp2->next;

  232. }

  233.  
  234. if(tmp2 == NULL)

  235. {

  236. msg->flag = -101;

  237. write(client_socket,msg,sizeof(Msg));

  238. printf("您要发送的好友不存在\n");

  239. return;

  240. }

  241. }

  242.  
  243. //注销

  244. void logout(int client_socket,Msg *msg)

  245. {

  246. Node *tmp = head;

  247. while(tmp->next)

  248. {

  249. if(tmp->next->data.client_socket == client_socket)

  250. {

  251. strcpy(msg->frname,tmp->next->data.username);

  252. Node *p = tmp->next;

  253. tmp->next = p->next;

  254. free(p);

  255. break;

  256. }

  257. }

  258.  
  259. char buf[100];

  260. char *errmsg = NULL;

  261. sprintf (buf, "delete from login where username = '%s'",msg->frname);

  262. int ret = sqlite3_exec(database, buf, NULL, NULL, &errmsg);

  263. if (ret != SQLITE_OK)

  264. {

  265. printf ("数据库操作失败:%s\n", errmsg);

  266. return;

  267. }

  268.  
  269. write(client_socket,msg,sizeof(Msg));

  270. }

  271.  
  272. //文件传输

  273. void file_transfer(int client_socket,Msg *msg)

  274. {

  275. Node *tmp1 = head->next;

  276. Node *tmp2 = head->next;

  277.  
  278. while(tmp1)

  279. {

  280. if(tmp1->data.client_socket == client_socket)

  281. {

  282. strcpy(msg->frname,tmp1->data.username);

  283. break;

  284. }

  285. tmp1 = tmp1->next;

  286. }

  287.  
  288. while(tmp2)

  289. {

  290. if(strcmp(tmp2->data.username,msg->toname) == 0)

  291. {

  292. if(strcmp(msg->msg,"over") != 0)

  293. {

  294. msg->flag = 101;

  295. usleep(15);

  296. write(tmp2->data.client_socket,msg,sizeof(Msg));

  297. return;

  298. }

  299. else

  300. {

  301. msg->flag = 102;

  302. printf("%s给%s发了一个文件\n",msg->frname,msg->toname);

  303. write(tmp2->data.client_socket,msg,sizeof(Msg));

  304. return;

  305. }

  306. }

  307. tmp2 = tmp2->next;

  308. }

  309.  
  310. if(tmp2 == NULL)

  311. {

  312. msg->flag = -101;

  313. write(client_socket,msg,sizeof(Msg));

  314. printf("您要发送的好友不存在\n");

  315. return;

  316. }

  317. }

  318.  
  319. //退出

  320. void user_quit(int client_socket,Msg *msg)

  321. {

  322. Node *tmp = head;

  323. while(tmp->next)

  324. {

  325. if(tmp->next->data.client_socket == client_socket)

  326. {

  327. strcpy(msg->frname,tmp->next->data.username);

  328. Node *p = tmp->next;

  329. tmp->next = p->next;

  330. free(p);

  331. break;

  332. }

  333. }

  334. printf("%s退出登录\n",msg->frname);

  335. write(client_socket,msg,sizeof(Msg));

  336. }

  337.  
  338. //处理客户端通信的线程工作函数

  339. void *handle_client(void* v)

  340. {

  341. int client_socket = (int)v;

  342. Msg msg;

  343.  
  344. while(1)

  345. {

  346. // 从客户端读一个结构体数据

  347. int ret = read(client_socket, &msg, sizeof(Msg));

  348. if (ret == -1)

  349. {

  350. perror ("read");

  351. break;

  352. }

  353.  
  354. // 代表客户端退出

  355. if (ret == 0)

  356. {

  357. printf ("客户端退出\n");

  358. break;

  359. }

  360.  
  361. switch (msg.cmd)

  362. {

  363. case 1:// 客户端进行注册

  364. reg(client_socket, &msg);

  365. break;

  366. case 2:// 登录

  367. login(client_socket,&msg);

  368. break;

  369. case 3://在线好友显示

  370. Display_Friend(client_socket,&msg);

  371. break;

  372. case 4://群聊

  373. chat_all(client_socket,&msg);

  374. break;

  375. case 5://私聊

  376. chat_along(client_socket,&msg);

  377. break;

  378. case 6://注销

  379. logout(client_socket,&msg);

  380. break;

  381. case 7://退出

  382. user_quit(client_socket,&msg);

  383. break;

  384. case 8://文件传输

  385. file_transfer(client_socket,&msg);

  386. break;

  387. }

  388. }

  389.  
  390. close(client_socket);

  391. }

  392.  
  393. int main()

  394. {

  395. //打开数据库

  396. int ret = sqlite3_open("chatroom.db", &database);

  397. if (ret != SQLITE_OK)

  398. {

  399. printf ("打开数据库失败\n");

  400. return -1;

  401. }

  402. printf("数据库打开成功\n");

  403.  
  404. //创建用户登录表

  405. char *errmsg = NULL;

  406. char *sql = "create table if not exists login(username TEXT,password TEXT)";

  407. ret = sqlite3_exec(database, sql, NULL, NULL, &errmsg);

  408. if (ret != SQLITE_OK)

  409. {

  410. printf ("数据库操作失败:%s\n", errmsg);

  411. return -1;

  412. }

  413. printf("用户登录表创建成功\n");

  414.  
  415. //创建好友链表

  416. head = Create_List();

  417. if(head == NULL)

  418. {

  419. printf("链表创建失败\n");

  420. return -1;

  421. }

  422. printf("链表创建成功\n");

  423.  
  424. //初始化套接字

  425. int listen_socket = Init_Socket();

  426.  
  427. while(1)

  428. {

  429. //获取与客户端连接的套接字

  430. int client_socket = MyAccept(listen_socket);

  431.  
  432. pthread_t pthread_id;

  433. int ret = pthread_create(&pthread_id, NULL, handle_client,(void*)client_socket);

  434. if(ret != 0)

  435. {

  436. perror("pthread_create");

  437. return -1;

  438. }

  439.  
  440. pthread_detach(pthread_id);

  441. }

  442.  
  443. Clean_List(head);

  444. Destroy(head);

  445. close(listen_socket);

  446. sqlite3_close(database);

  447.  
  448. return 0;

  449. }

 

客户端

 

 
  1. #include <stdio.h>

  2. #include <string.h>

  3. #include <sys/types.h>

  4. #include <sys/socket.h>

  5. #include <arpa/inet.h>

  6. #include <time.h>

  7. #include <sqlite3.h>

  8. #include <string.h>

  9. #include <sys/stat.h>

  10. #include <fcntl.h>

  11. #include <unistd.h>

  12. #include "LinkList.h"

  13.  
  14. typedef struct _msg

  15. {

  16. int cmd;

  17. int flag;

  18. char username[20];

  19. char password[20];

  20. char msg[1024];

  21. char frname[20];

  22. char toname[20];

  23. char time[50];

  24. char filename[20];

  25. }Msg;

  26.  
  27. sqlite3 *database;

  28.  
  29. //与服务器连接,返回与服务器通信的套接字

  30. int MyConnect(char *argv[])

  31. {

  32. int socketfd = socket(AF_INET, SOCK_STREAM, 0);

  33. if(socketfd == -1)

  34. {

  35. perror("socket");

  36. return -1;

  37. }

  38.  
  39. struct sockaddr_in addr;

  40. memset(&addr, 0, sizeof(addr));

  41. addr.sin_family = AF_INET;

  42. addr.sin_port = htons(atoi(argv[2]));

  43. inet_aton(argv[1],&(addr.sin_addr));

  44.  
  45. int ret = connect(socketfd, (struct sockaddr *)&addr, sizeof(addr));

  46. if (ret == -1)

  47. {

  48. perror ("connect");

  49. return -1;

  50. }

  51.  
  52. printf ("成功连上服务器\n");

  53.  
  54. return socketfd;

  55. }

  56.  
  57. void Ask_UserServer(int socketfd);

  58.  
  59. //登录界面

  60. void interface()

  61. {

  62. time_t Time = time(NULL);

  63. char *buf = ctime(&Time);

  64.  
  65. system("clear");

  66. printf("%s",buf);

  67. printf("|****************************|\n");

  68. printf("| 欢迎使用 |\n");

  69. printf("|****************************|\n");

  70. printf("| 1、用户注册 |\n");

  71. printf("| 2、用户登录 |\n");

  72. printf("| 3、退出 |\n");

  73. printf("|****************************|\n");

  74. printf("请输入您要进行的操作:\n");

  75. }

  76.  
  77. //用户界面

  78. void userface()

  79. {

  80. time_t Time = time(NULL);

  81. char *buf = ctime(&Time);

  82.  
  83. system("clear");

  84. printf("%s",buf);

  85. printf("|****************************|\n");

  86. printf("| 主菜单 |\n");

  87. printf("|****************************|\n");

  88. printf("| 1、显示在线好友 |\n");

  89. printf("| 2、群聊 |\n");

  90. printf("| 3、私聊 |\n");

  91. printf("| 4、查看聊天记录 |\n");

  92. printf("| 5、用户注销 |\n");

  93. printf("| 6、文件传输 |\n");

  94. printf("| 7、退出 |\n");

  95. printf("|****************************|\n");

  96. printf("请输入您要进行的操作:\n");

  97. }

  98.  
  99. //在线好友显示

  100. void Display_Friend(int socketfd)

  101. {

  102. Msg msg;

  103. msg.cmd = 3;

  104. write (socketfd, &msg, sizeof(msg));

  105. }

  106.  
  107. //群聊

  108. void chat_all(int socketfd)

  109. {

  110. Msg msg;

  111. msg.cmd = 4;

  112. printf ("请输入要发送的内容: \n");

  113. fgets(msg.msg, 1024, stdin);

  114. write (socketfd, &msg, sizeof(msg));

  115. }

  116.  
  117. //私聊

  118. void chat_along(int socketfd)

  119. {

  120. Msg msg;

  121. msg.cmd = 5;

  122. printf("请输入您要发送的对象:\n");

  123. scanf("%s",msg.toname);

  124. while(getchar() != '\n');

  125.  
  126. printf ("请输入要发送的内容: \n");

  127. fgets(msg.msg, 1024, stdin);

  128.  
  129. write (socketfd, &msg, sizeof(msg));

  130. }

  131.  
  132. //将数据插入本地数据库

  133. void insert_records(sqlite3 *database,Msg msg)

  134. {

  135. char buf[100];

  136. char *errmsg = NULL;

  137. sprintf (buf, "insert into records values('%s', '%s', '%s','%s')",msg.time,msg.frname,msg.toname,msg.msg);

  138. int ret = sqlite3_exec(database, buf, NULL, NULL, &errmsg);

  139. if (ret != SQLITE_OK)

  140. {

  141. printf ("数据库操作失败:%s\n", errmsg);

  142. return;

  143. }

  144. }

  145.  
  146. //查看聊天记录

  147. void get_records(sqlite3 *database)

  148. {

  149. char *errmsg = NULL;

  150. char **resultp = NULL;

  151. int nrow, ncolumn;

  152. char *sql = "select * from records";

  153. int ret = sqlite3_get_table(database, sql, &resultp, &nrow, &ncolumn, &errmsg);

  154. if (ret != SQLITE_OK)

  155. {

  156. printf ("数据库操作失败:%s\n", errmsg);

  157. return;

  158. }

  159.  
  160.  
  161. int i;

  162. for (i = 0; i < (nrow+1)*ncolumn; i++)

  163. {

  164. if (i % ncolumn == 0)

  165. printf ("\n");

  166. printf ("%-26s", resultp[i]);

  167. }

  168. printf ("\n");

  169. }

  170.  
  171. //用户注销

  172. void logout(int socketfd)

  173. {

  174. Msg msg;

  175. msg.cmd = 6;

  176. write (socketfd, &msg, sizeof(msg));

  177. }

  178.  
  179. //文件传输

  180. void file_transfer(int socketfd)

  181. {

  182. Msg msg;

  183. msg.cmd = 8;

  184. printf("请输入您要发送的对象:\n");

  185. scanf("%s",msg.toname);

  186. while(getchar() != '\n');

  187.  
  188. printf("当前文件夹下的文件:\n");

  189. system("ls");

  190. printf ("请输入要发送的文件: \n");

  191. fgets(msg.filename, 20, stdin);

  192.  
  193. char *tmp = msg.filename;

  194. while(tmp)

  195. {

  196. if(*tmp == '\n')

  197. {

  198. *tmp = '\0';

  199. break;

  200. }

  201. tmp++;

  202. }

  203.  
  204. //printf("%s",msg.filename);

  205. FILE *fp1 = fopen(msg.filename, "r");

  206. if (fp1 == NULL)

  207. {

  208. perror ("fopen fp1");

  209. return;

  210. }

  211.  
  212. int ret;

  213. while (ret = fread(msg.msg, sizeof(char), 1023, fp1))

  214. {

  215. msg.msg[1023] = '\0';

  216. //sleep(2);

  217. //printf("%s",msg.msg);

  218. usleep(15);

  219. write(socketfd, &msg, sizeof(msg));

  220. memset(msg.msg,0,1024);

  221. }

  222.  
  223. if(ret == 0 && !feof(fp1))

  224. {

  225. perror ("fread");

  226. return;

  227. }

  228.  
  229. strcpy(msg.msg,"over");

  230. write(socketfd, &msg, sizeof(msg));

  231. fclose(fp1);

  232. }

  233.  
  234. void receive_file(Msg *msg)

  235. {

  236. FILE *fp2 = fopen("2.ppt", "a");

  237. if (fp2 == NULL)

  238. {

  239. perror ("fopen fp2");

  240. return;

  241. }

  242.  
  243. fwrite(msg->msg,sizeof(char),1023,fp2);

  244.  
  245. fclose(fp2);

  246. }

  247.  
  248. //退出登录

  249. void user_quit(int socketfd)

  250. {

  251. Msg msg;

  252. msg.cmd = 7;

  253. write (socketfd, &msg, sizeof(msg));

  254. }

  255.  
  256. //读写分离

  257. void * readMsg(void *v)

  258. {

  259. int socketfd = (int)v;

  260. Msg msg;

  261. while (1)

  262. {

  263. read (socketfd, &msg, sizeof(msg));

  264.  
  265. time_t Time = time(NULL);

  266. char *buf = ctime(&Time);

  267. *(buf+24) = '\0';

  268. strcpy(msg.time,buf);

  269.  
  270. switch(msg.cmd)

  271. {

  272. case 3:

  273. printf("在线好友:%s\n",msg.username);

  274. break;

  275. case 4:

  276. printf("%s %s发了一条群消息: %s\n",msg.time,msg.frname,msg.msg);

  277. insert_records(database,msg);

  278. break;

  279. case 5:

  280. if(msg.flag == 101)

  281. {

  282. printf("%s %s给您发了一条消息:%s",msg.time,msg.frname,msg.msg);

  283. insert_records(database,msg);

  284. }

  285. else if(msg.flag == -101)

  286. printf("您要发送的好友不存在\n");

  287. break;

  288. case 6:

  289. printf("注销成功\n");

  290. printf("按回车键返回......\n");

  291. return;

  292. case 7:

  293. printf("退出成功\n");

  294. printf("按回车键返回......\n");

  295. return;

  296. case 8:

  297. if(msg.flag == 101)

  298. {

  299. receive_file(&msg);

  300. }

  301. else if(msg.flag == 102)

  302. {

  303. printf("%s 给您发送了一个文件\n",msg.frname);

  304. }

  305. else if(msg.flag == -101)

  306. printf("您要发送的好友不存在\n");

  307. break;

  308. }

  309. }

  310. }

  311.  
  312. //注册

  313. void reg(int socketfd)

  314. {

  315. Msg msg;

  316. char username[20];

  317. char password[20];

  318. msg.cmd = 1;

  319. printf("请输入用户名:");

  320. //getchar();

  321. //fgets(username,20,stdin);

  322. scanf("%s",username);

  323. strcpy(msg.username,username);

  324. printf("请输入密码:");

  325. //getchar();

  326. //fgets(password,20,stdin);

  327. scanf("%s",password);

  328. strcpy(msg.password,password);

  329.  
  330. write(socketfd,&msg,sizeof(Msg));

  331.  
  332. read(socketfd,&msg,sizeof(Msg));

  333.  
  334. if(msg.cmd == -1)

  335. {

  336. printf("用户已存在,注册失败\n");

  337. return;

  338. }

  339. else if(msg.cmd == 1001)

  340. printf("注册成功\n");

  341. }

  342.  
  343. //登录

  344. void login(int socketfd)

  345. {

  346. Msg msg;

  347. char username[20];

  348. char password[20];

  349. msg.cmd = 2;

  350. printf("请输入用户名:");

  351. scanf("%s",username);

  352. strcpy(msg.username,username);

  353. printf("请输入密码:");

  354. scanf("%s",password);

  355. strcpy(msg.password,password);

  356.  
  357. write(socketfd,&msg,sizeof(Msg));

  358.  
  359. read(socketfd,&msg,sizeof(Msg));

  360.  
  361. if(msg.cmd == -2)

  362. {

  363. printf("用户名或密码错误,登陆失败\n");

  364. return;

  365. }

  366.  
  367. else if(msg.cmd == 1002)

  368. printf("登录成功\n");

  369.  
  370. pthread_t id;

  371. pthread_create(&id, NULL, readMsg, (void *)socketfd);

  372.  
  373. pthread_detach(id);

  374.  
  375. sleep(2);

  376.  
  377. Ask_UserServer(socketfd);

  378. }

  379.  
  380. //注册、登录服务

  381. void Ask_Server(int socketfd)

  382. {

  383. int choice;

  384. while(1)

  385. {

  386. interface();

  387. scanf("%d",&choice);

  388.  
  389. switch(choice)

  390. {

  391. case 1:

  392. reg(socketfd);

  393. getchar();

  394. sleep(2);

  395. break;

  396. case 2:

  397. login(socketfd);

  398. getchar();

  399. sleep(2);

  400. break;

  401. case 3:

  402. return;

  403. default:

  404. break;

  405. }

  406. }

  407. }

  408.  
  409. //用户服务

  410. void Ask_UserServer(int socketfd)

  411. {

  412. int choice;

  413. while(1)

  414. {

  415. userface();

  416. scanf("%d",&choice);

  417. while(getchar()!= '\n');

  418.  
  419. switch(choice)

  420. {

  421. case 1:

  422. Display_Friend(socketfd);

  423. getchar();

  424. break;

  425. case 2:

  426. chat_all(socketfd);

  427. getchar();

  428. break;

  429. case 3:

  430. chat_along(socketfd);

  431. getchar();

  432. break;

  433. case 4:

  434. get_records(database);

  435. getchar();

  436. break;

  437. case 5:

  438. logout(socketfd);

  439. return;

  440. case 6:

  441. file_transfer(socketfd);

  442. break;

  443. case 7:

  444. user_quit(socketfd);

  445. return;

  446. }

  447. }

  448. }

  449.  
  450. int main(int argc, char *argv[])

  451. {

  452. //打开数据库

  453. int ret = sqlite3_open("chatroom.db", &database);

  454. if (ret != SQLITE_OK)

  455. {

  456. printf ("打开数据库失败\n");

  457. return -1;

  458. }

  459.  
  460. //创建本地聊天记录表

  461. char *errmsg = NULL;

  462. char *sql = "create table if not exists records(time TEXT,frname TEXT,toname TEXT,msg TEXT)";

  463. ret = sqlite3_exec(database, sql, NULL, NULL, &errmsg);

  464. if (ret != SQLITE_OK)

  465. {

  466. printf ("数据库操作失败:%s\n", errmsg);

  467. return -1;

  468. }

  469.  
  470. int socketfd = MyConnect(argv);

  471.  
  472. Ask_Server(socketfd);

  473.  
  474. close(socketfd);

  475. sqlite3_close(database);

  476.  
  477. return 0;

  478. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值