├── PP_server ├── MsgEx.txt ├── server ├── PP_user.db ├── pp_sqlite3 │ ├── src │ │ ├── pp_sqlite3.o │ │ └── pp_sqlite3.c │ └── Makefile ├── rand_ppid │ ├── src │ │ ├── rand_brith_id.o │ │ └── rand_brith_id.c │ └── Makefile ├── server_main │ ├── src │ │ ├── server_main.o │ │ └── server_main.c │ └── Makefile ├── server_thread │ ├── Makefile │ └── src │ │ ├── server_thread.o │ │ └── server_thread.c ├── scripts │ └── Makefile └── Makefile ├── PP_client ├── a.txt ├── b.txt ├── client ├── dis_menu │ ├── src │ │ ├── dis_menu.o │ │ └── dis_menu.c │ └── Makefile ├── pack_msg │ ├── src │ │ ├── pack_msg.o │ │ └── pack_msg.c │ └── Makefile ├── client_main │ ├── src │ │ ├── client_main.o │ │ └── client_main.c │ └── Makefile ├── client_thread │ ├── src │ │ ├── filerecv.o │ │ ├── client_thread.o │ │ ├── filerecv.c │ │ └── client_thread.c │ └── Makefile ├── input_check │ ├── src │ │ ├── input_check.o │ │ └── input_check.c │ └── Makefile ├── rand_ppid │ ├── src │ │ ├── rand_brith_id.o │ │ └── rand_brith_id.c │ └── Makefile ├── scripts │ └── Makefile └── Makefile ├── .gitattributes ├── .gitignore ├── readme.txt └── include └── PP_ONLINE.h /PP_server/MsgEx.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /PP_client/a.txt: -------------------------------------------------------------------------------- 1 | Linux pp_online file text 2 | 3 | 赵溪楠 南京工程学院 4 | 5 | Linux 网络聊天室文件测试.txt 6 | -------------------------------------------------------------------------------- /PP_client/b.txt: -------------------------------------------------------------------------------- 1 | Linux pp_online file text 2 | 3 | 赵溪楠 南京工程学院 4 | 5 | Linux 网络聊天室文件测试.txt 6 | -------------------------------------------------------------------------------- /PP_client/client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siriuszxn/PP_ONLINE_CHATROOM/HEAD/PP_client/client -------------------------------------------------------------------------------- /PP_server/server: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siriuszxn/PP_ONLINE_CHATROOM/HEAD/PP_server/server -------------------------------------------------------------------------------- /PP_server/PP_user.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siriuszxn/PP_ONLINE_CHATROOM/HEAD/PP_server/PP_user.db -------------------------------------------------------------------------------- /PP_client/dis_menu/src/dis_menu.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siriuszxn/PP_ONLINE_CHATROOM/HEAD/PP_client/dis_menu/src/dis_menu.o -------------------------------------------------------------------------------- /PP_client/pack_msg/src/pack_msg.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siriuszxn/PP_ONLINE_CHATROOM/HEAD/PP_client/pack_msg/src/pack_msg.o -------------------------------------------------------------------------------- /PP_server/pp_sqlite3/src/pp_sqlite3.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siriuszxn/PP_ONLINE_CHATROOM/HEAD/PP_server/pp_sqlite3/src/pp_sqlite3.o -------------------------------------------------------------------------------- /PP_client/client_main/src/client_main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siriuszxn/PP_ONLINE_CHATROOM/HEAD/PP_client/client_main/src/client_main.o -------------------------------------------------------------------------------- /PP_client/client_thread/src/filerecv.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siriuszxn/PP_ONLINE_CHATROOM/HEAD/PP_client/client_thread/src/filerecv.o -------------------------------------------------------------------------------- /PP_client/input_check/src/input_check.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siriuszxn/PP_ONLINE_CHATROOM/HEAD/PP_client/input_check/src/input_check.o -------------------------------------------------------------------------------- /PP_client/rand_ppid/src/rand_brith_id.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siriuszxn/PP_ONLINE_CHATROOM/HEAD/PP_client/rand_ppid/src/rand_brith_id.o -------------------------------------------------------------------------------- /PP_server/rand_ppid/src/rand_brith_id.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siriuszxn/PP_ONLINE_CHATROOM/HEAD/PP_server/rand_ppid/src/rand_brith_id.o -------------------------------------------------------------------------------- /PP_server/server_main/src/server_main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siriuszxn/PP_ONLINE_CHATROOM/HEAD/PP_server/server_main/src/server_main.o -------------------------------------------------------------------------------- /PP_client/client_main/Makefile: -------------------------------------------------------------------------------- 1 | include ../scripts/Makefile 2 | 3 | all : $(Objs) 4 | 5 | clean : 6 | rm -rf $(Objs) 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /PP_client/dis_menu/Makefile: -------------------------------------------------------------------------------- 1 | include ../scripts/Makefile 2 | 3 | all : $(Objs) 4 | 5 | clean : 6 | rm -rf $(Objs) 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /PP_client/input_check/Makefile: -------------------------------------------------------------------------------- 1 | include ../scripts/Makefile 2 | 3 | all : $(Objs) 4 | 5 | clean : 6 | rm -rf $(Objs) 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /PP_client/pack_msg/Makefile: -------------------------------------------------------------------------------- 1 | include ../scripts/Makefile 2 | 3 | all : $(Objs) 4 | 5 | clean : 6 | rm -rf $(Objs) 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /PP_client/rand_ppid/Makefile: -------------------------------------------------------------------------------- 1 | include ../scripts/Makefile 2 | 3 | all : $(Objs) 4 | 5 | clean : 6 | rm -rf $(Objs) 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /PP_server/pp_sqlite3/Makefile: -------------------------------------------------------------------------------- 1 | include ../scripts/Makefile 2 | 3 | all : $(Objs) 4 | 5 | clean : 6 | rm -rf $(Objs) 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /PP_server/rand_ppid/Makefile: -------------------------------------------------------------------------------- 1 | include ../scripts/Makefile 2 | 3 | all : $(Objs) 4 | 5 | clean : 6 | rm -rf $(Objs) 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /PP_server/server_main/Makefile: -------------------------------------------------------------------------------- 1 | include ../scripts/Makefile 2 | 3 | all : $(Objs) 4 | 5 | clean : 6 | rm -rf $(Objs) 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /PP_client/client_thread/Makefile: -------------------------------------------------------------------------------- 1 | include ../scripts/Makefile 2 | 3 | all : $(Objs) 4 | 5 | clean : 6 | rm -rf $(Objs) 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /PP_client/client_thread/src/client_thread.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siriuszxn/PP_ONLINE_CHATROOM/HEAD/PP_client/client_thread/src/client_thread.o -------------------------------------------------------------------------------- /PP_server/server_thread/Makefile: -------------------------------------------------------------------------------- 1 | include ../scripts/Makefile 2 | 3 | all : $(Objs) 4 | 5 | clean : 6 | rm -rf $(Objs) 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /PP_server/server_thread/src/server_thread.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siriuszxn/PP_ONLINE_CHATROOM/HEAD/PP_server/server_thread/src/server_thread.o -------------------------------------------------------------------------------- /PP_server/scripts/Makefile: -------------------------------------------------------------------------------- 1 | CC := gcc 2 | CFLAGS := -Wall -O3 3 | Libs = -lpthread -lsqlite3 4 | Target := server 5 | Source := $(wildcard src/*.c) 6 | Objs := $(patsubst %.c,%.o,$(Source)) 7 | Modules += rand_ppid server_main server_thread pp_sqlite3 8 | AllObjs := $(addsuffix /src/*.o,$(Modules)) 9 | 10 | -------------------------------------------------------------------------------- /PP_client/scripts/Makefile: -------------------------------------------------------------------------------- 1 | CC := gcc 2 | CFLAGS := -Wall -O3 3 | Libs = -lpthread -lsqlite3 4 | Target := client 5 | Source := $(wildcard src/*.c) 6 | Objs := $(patsubst %.c,%.o,$(Source)) 7 | Modules += client_main client_thread dis_menu input_check pack_msg rand_ppid 8 | AllObjs := $(addsuffix /src/*.o,$(Modules)) 9 | 10 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /PP_client/Makefile: -------------------------------------------------------------------------------- 1 | include scripts/Makefile 2 | 3 | modules_make = $(MAKE) -C $(1); 4 | modules_clean = $(MAKE) clean -C $(1); 5 | 6 | .PHONY: all mm mc clean 7 | 8 | all: $(Target) 9 | 10 | mm: 11 | @ $(foreach n,$(Modules),$(call modules_make,$(n))) 12 | mc: 13 | @ $(foreach n,$(Modules),$(call modules_clean,$(n))) 14 | 15 | $(Target) : mm 16 | $(CC) $(CFLAGS) -o $(Target) $(AllObjs) $(Libs) 17 | @ echo $(Target) make done! 18 | 19 | clean : mc 20 | rm -rf $(Target) 21 | @ echo clean done! 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /PP_server/Makefile: -------------------------------------------------------------------------------- 1 | include scripts/Makefile 2 | 3 | modules_make = $(MAKE) -C $(1); 4 | modules_clean = $(MAKE) clean -C $(1); 5 | 6 | .PHONY: all mm mc clean 7 | 8 | all: $(Target) 9 | 10 | mm: 11 | @ $(foreach n,$(Modules),$(call modules_make,$(n))) 12 | mc: 13 | @ $(foreach n,$(Modules),$(call modules_clean,$(n))) 14 | 15 | $(Target) : mm 16 | $(CC) $(CFLAGS) -o $(Target) $(AllObjs) $(Libs) 17 | @ echo $(Target) make done! 18 | 19 | clean : mc 20 | rm -rf $(Target) 21 | @ echo clean done! 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /PP_client/rand_ppid/src/rand_brith_id.c: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | - Copyright (C): 2016 3 | - File name :rand_brith_id.c 4 | - Author :- Zhao - 5 | - Date :2016年01月30日 星期六 21时49分31秒 6 | - Description : 7 | * *******************************************************/ 8 | #include "../../../include/PP_ONLINE.h" 9 | 10 | int rand_brith_id() 11 | { 12 | int i; 13 | int j; 14 | srand((unsigned)time(NULL)); 15 | 16 | for (i = 0; i < 10; i++) 17 | { 18 | j = (((double)rand() / (double)999999) * 999999 + 100001); 19 | } 20 | j = j % 1000000; 21 | 22 | return j; 23 | } 24 | -------------------------------------------------------------------------------- /PP_server/rand_ppid/src/rand_brith_id.c: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | - Copyright (C): 2016 3 | - File name :rand_brith_id.c 4 | - Author :- Zhao - 5 | - Date :2016年01月30日 星期六 21时49分31秒 6 | - Description : 7 | * *******************************************************/ 8 | #include "../../../include/PP_ONLINE.h" 9 | 10 | int rand_brith_id() 11 | { 12 | int i; 13 | int j; 14 | srand((unsigned)time(NULL)); 15 | 16 | for (i = 0; i < 10; i++) 17 | { 18 | j = (((double)rand() / (double)999999) * 999999 + 100001); 19 | } 20 | j = j % 1000000; 21 | 22 | return j; 23 | } 24 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /PP_client/client_thread/src/filerecv.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "../../../include/PP_ONLINE.h" 4 | 5 | 6 | void file_recv(struct _Message *msg) 7 | { 8 | int to_fd; 9 | int bytes_read = msg->count; 10 | int bytes_write; 11 | 12 | char *ptr; 13 | clear_min(); 14 | 15 | printf("\033[6;14H 好友%s向你发送文件%s\n", msg->name, msg->filename); 16 | 17 | //打开或创建文件 18 | if ((to_fd = open(msg->filename, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR)) == -1) 19 | { 20 | fprintf(stderr, "Open %s Error:%s", msg->filename, strerror(errno)); 21 | exit(1); 22 | } 23 | //写文件 24 | ptr = msg->filemsg; 25 | while ((bytes_write = write(to_fd, ptr, bytes_read))) 26 | { 27 | /* 一个致命的错误发生了 */ 28 | if ((bytes_write == -1) && (errno != EINTR)) 29 | { 30 | break; 31 | } 32 | else if (bytes_write == bytes_read) 33 | { 34 | /* 写完了所有读的字节 */ 35 | printf("\033[8;14H 文件%s成功接收完成!", msg->filename); 36 | break; 37 | } 38 | else if (bytes_write > 0) 39 | { 40 | /* 只写了一部分 */ 41 | ptr += bytes_write; 42 | bytes_read -= bytes_write; 43 | } 44 | } 45 | printf("\033[28;72H"); 46 | fflush(stdout); 47 | } 48 | 49 | -------------------------------------------------------------------------------- /readme.txt: -------------------------------------------------------------------------------- 1 | ********************************* 2 | * * 3 | * Linux 网络聊天室PP_ONLINE * 4 | * * 5 | * 这款Linux网络聊天室,使用LinuxC进行编写,采用C/S架构 * 6 | * 通过多线程实现了服务器与多个客户端之间的通信, 采用 * 7 | * Makefile工程管理器进行项目工程管理, 用户界面友好美 * 8 | * 观动态显示;采用轻量级数据库Sqlite3在服务器端记录存 * 9 | * 储所有用户注册时的昵称、密码、密保、密保问题、个性 * 10 | * 签名、以及点赞个数,VIP标志等服务日志,方便管理。 * 11 | * 具体功能包括:注册、登录、忘记密码、修及密码、群聊 * 12 | * 私聊、查看在线用户、设置个性签名、点赞、文件传输、 * 13 | * 查看聊天记录、注册会员等功能、VIP会员还独享禁言、 * 14 | * 解禁、踢人等特殊功能 * 15 | * * 16 | * 系统默认超级用户 ID:100000 * 17 | * 密码:admin (其他默认)admin * 18 | * * 19 | * 操作指令: * 20 | * LOG 登录 REG 注册 * 21 | * REM 忘密 EXT 退出 * 22 | * ACH 群聊 PCH 私聊 * 23 | * ZAN 点赞 SBQ 发送表情 * 24 | * SDY 发送短语 SFS 文件传输 * 25 | * DIS 刷出在线好友 * 26 | * HLP PP简介 BCK 切换用户 * 27 | * SET 设置个性签名 * 28 | * VIP 注册会员 NOS 禁言 * 29 | * CAS 解禁 OUT 踢人 * 30 | * SJL 查看聊天记录 * 31 | * * 32 | ********************************* 33 | -------------------------------------------------------------------------------- /PP_server/server_main/src/server_main.c: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | - Copyright (C): 2016 3 | - File name : sever.c 4 | - Author : - Zhao - 5 | - Date : 2016年01月29日 星期五 16时34分40秒 6 | - Description : 服务器端程序_TCP_SERVER 7 | * *******************************************************/ 8 | #include "../../../include/PP_ONLINE.h" 9 | 10 | int main(int argc, char **argv) 11 | { 12 | int ret; 13 | int sockfd; //服务器套接字 14 | int new_fd; //连接后与客户端的新的套接字 15 | int sin_size; 16 | int opt = 1; 17 | pthread_t id; 18 | sqlite3 *db = NULL; 19 | 20 | struct sockaddr_in server_addr; 21 | struct sockaddr_in client_addr; 22 | 23 | /* 创建一个用户数据库用来存放客户信息 */ 24 | if (SQLITE_OK != sqlite3_open("PP_user.db", &db)) 25 | { 26 | fprintf(stderr, "Cannot open database:%s\n", sqlite3_errmsg(db)); 27 | sqlite3_close(db); 28 | exit(1); 29 | } 30 | else 31 | { 32 | printf("Open database successfully...\n"); 33 | } 34 | 35 | create_server_sql(db); 36 | 37 | /* 调用socket函数创建一个TCP套接口 */ 38 | if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 39 | { 40 | fprintf(stderr, "Socket error:%s\n\a",strerror(errno)); 41 | exit(1); 42 | } 43 | /* 填充服务器端的信息 */ 44 | bzero(&server_addr, sizeof(struct sockaddr_in)); 45 | server_addr.sin_family = AF_INET; 46 | server_addr.sin_addr.s_addr = htonl(INADDR_ANY); 47 | server_addr.sin_port = htons(portnumber); 48 | /* 让服务器退出后不用在等待30s*/ 49 | setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); 50 | 51 | if (bind(sockfd, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr)) == -1) 52 | { 53 | fprintf(stderr, "Bind error:%s\n\a", strerror(errno)); 54 | exit(1); 55 | } 56 | /* 设置最大监听数目 */ 57 | if (listen(sockfd, 30) == -1) 58 | { 59 | fprintf(stderr, "Listen error:%s\n\a",strerror(errno)); 60 | exit(1); 61 | } 62 | /* 主线程负责与客户端建立连接 */ 63 | while (1) 64 | { 65 | sin_size = sizeof(struct sockaddr_in); 66 | /* 阻塞的等待客户端的连接 */ 67 | if ((new_fd = accept(sockfd, (struct sockaddr *)(&client_addr), &sin_size)) == -1) 68 | { 69 | fprintf(stderr,"Accept error:%s\n\a", strerror(errno)); 70 | exit(1); 71 | } 72 | fprintf(stderr, "Server get connection from %s\n", inet_ntoa(client_addr.sin_addr)); 73 | 74 | /* 创建一个子线程,用于处理多客户端连接*/ 75 | ret = pthread_create(&id, NULL, (void *)server_read_msg, (void *)&new_fd); 76 | if (ret) 77 | { 78 | printf("create thread error!\n"); 79 | exit(1); 80 | } 81 | } 82 | close(sockfd); 83 | sqlite3_close(db); 84 | return 0; 85 | } 86 | 87 | -------------------------------------------------------------------------------- /PP_client/input_check/src/input_check.c: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | - Copyright (C): 2016 3 | - File name : input_check.c 4 | - Author : - Zhao - 5 | - Date : 2016年01月30日 星期六 11时24分39秒 6 | - Description : 字符串解析函数 7 | * *******************************************************/ 8 | #include "../../../include/PP_ONLINE.h" 9 | 10 | int input_check(char *cmd) 11 | { 12 | char check[5]; 13 | int i = 0; 14 | 15 | /* 入口参数检查 */ 16 | for (i = 0; i < 3; i++) 17 | { 18 | check[i] = cmd[i]; 19 | } 20 | check[i] = '\0'; 21 | 22 | /* 当命令为点赞时 */ 23 | if (strcmp(check, "ZAN") == 0) 24 | { 25 | return DIANZAN; 26 | } 27 | 28 | /* 当命令为登录时 */ 29 | if (strcmp(check, "LOG") == 0) 30 | { 31 | return LOGIN; 32 | } 33 | 34 | /* 当命令为注册时 */ 35 | if (strcmp(check, "REG") == 0) 36 | { 37 | return REGIST; 38 | } 39 | 40 | /* 当命令为查看在线好友时*/ 41 | if (strcmp(check, "DIS") == 0) 42 | { 43 | return DIS_ONLINE_USER; 44 | } 45 | 46 | /* 当命令为私聊时*/ 47 | if (strcmp(check, "PCH") == 0) 48 | { 49 | return CHATONE; 50 | } 51 | 52 | /* 当命令为群聊时 */ 53 | if (strcmp(check, "ACH") == 0) 54 | { 55 | return CHATALL; 56 | } 57 | 58 | /* 当命令为忘记/修改密码时 */ 59 | if (strcmp(check, "REM") == 0) 60 | { 61 | return RESET_PASSWD; 62 | } 63 | 64 | /* 当命令为帮助时 */ 65 | if (strcmp(check, "HLP") == 0) 66 | { 67 | return HELP; 68 | } 69 | 70 | /* 当命令为返回登录界面时 */ 71 | if (strcmp(check, "BCK") == 0) 72 | { 73 | return BACK_LOGMENU; 74 | } 75 | 76 | /* 当命令为禁言时 */ 77 | if (strcmp(check, "NOS") == 0) 78 | { 79 | return SLENCE; 80 | } 81 | 82 | /* 当命令为解禁时 */ 83 | if (strcmp(check, "CAS") == 0) 84 | { 85 | return PERMITE; 86 | } 87 | 88 | /* 当命令为踢人时 */ 89 | if (strcmp(check, "OUT") == 0) 90 | { 91 | return TICK; 92 | } 93 | 94 | /* 当命令为这种个性签名时 */ 95 | if (strcmp(check, "SET") == 0) 96 | { 97 | return SETSIGNER; 98 | } 99 | 100 | /* 当命令为注册会员时 */ 101 | if (strcmp(check, "VIP") == 0) 102 | { 103 | return VIPMEMBER; 104 | } 105 | 106 | /* 当命令为发送表情时 */ 107 | if (strcmp(check, "SBQ") == 0) 108 | { 109 | return SENDBQ; 110 | } 111 | 112 | /* 当命令为发送短语时 */ 113 | if (strcmp(check, "SDY") == 0) 114 | { 115 | return SENDDY; 116 | } 117 | 118 | /* 当命令为传输文件时 */ 119 | if (strcmp(check, "SFS") == 0) 120 | { 121 | return SENDFILE; 122 | } 123 | 124 | /* 当命令为查看聊天记录时 */ 125 | if (strcmp(check, "SJL") == 0) 126 | { 127 | return DISJILU; 128 | } 129 | if (strcmp(check, "EXT") == 0) 130 | { 131 | system("clear"); 132 | printf("\033[15;40H*感谢使用PP_ONLINE聊天室、您的使用是对我们最大的支持\n"); 133 | sleep(2); 134 | printf("\033[17;40H 下次再见\n"); 135 | sleep(2); 136 | system("clear"); 137 | exit(0); 138 | } 139 | /* 以上命令都不是的时*/ 140 | return WRONG_CMD; 141 | } 142 | -------------------------------------------------------------------------------- /PP_client/client_main/src/client_main.c: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | - Copyright (C): 2016 3 | - File name : client_main.c 4 | - Author : - Zhao - 5 | - Date : 2016年01月29日 星期五 18时40分20秒 6 | - Description : 客户端TCP_client 7 | * *******************************************************/ 8 | #include "../../../include/PP_ONLINE.h" 9 | 10 | int main(int argc, char **argv) 11 | { 12 | int ret; //保存线程的返回值 13 | int sockfd; 14 | int n_write; 15 | 16 | char buffer[50]; //定义一个缓冲区 17 | pthread_t id; //线程ID 18 | Message msg; //定义一个Message结构体 19 | 20 | struct sockaddr_in server_addr; //定义服务器的结构体 21 | 22 | /* main函数入口参数检查 */ 23 | if (argc != 2) 24 | { 25 | fprintf(stderr, "Usage:%s hostname \a\n", argv[0]); 26 | exit(1); 27 | } 28 | /* 调用socket函数创建一个TCP协议套接口 */ 29 | if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) 30 | { 31 | fprintf(stderr, "Socket Error:$%s\a\n", strerror(errno)); 32 | exit(1); 33 | } 34 | /* 填充服务器端的信息 */ 35 | bzero(&server_addr, sizeof(server_addr)); 36 | server_addr.sin_family = AF_INET; 37 | server_addr.sin_port = htons(portnumber); 38 | server_addr.sin_addr.s_addr = inet_addr(argv[1]); 39 | /* 连接服务器 */ 40 | if (connect(sockfd, (struct sockaddr *)(&server_addr),sizeof(struct sockaddr)) == -1) 41 | { 42 | fprintf(stderr,"Connect Error:%s\a\n", strerror(errno)); 43 | exit(1); 44 | } 45 | 46 | //创建一个子线程去不停的读取服务器发回的信息 47 | ret = pthread_create(&id, NULL, (void *)client_read_msg, (void *)&sockfd); 48 | 49 | if (ret != 0) 50 | { 51 | printf("Create pthread error!\n"); 52 | exit(1); 53 | } 54 | 55 | /* 登录界面显示 */ 56 | loading_dis(); 57 | log_dis(); 58 | 59 | //主线程去不停的获取用户输入 60 | while (1) 61 | { 62 | sleep(1); 63 | usleep(50000); 64 | msg.activity = 0; 65 | memset(buffer, 0, sizeof(buffer)); 66 | printf("\033[28;71H \n"); 67 | printf("\033[28;45H*Please input your choice:"); 68 | scanf("%s", buffer); 69 | printf("\033[30;45H "); 70 | fflush(stdout); 71 | //pthread_join(id, NULL); 72 | msg.action = input_check(buffer); //字符串解析将相应的动作状态返回到action中 73 | 74 | /* 如果输入出错则提示并要求用户重新输入 */ 75 | if (msg.action == WRONG_CMD) 76 | { 77 | printf("\033[30;45H*客官您手抖了,再来一次!(^_^)"); 78 | printf("\033[28;45H \n"); 79 | printf("\033[29;0H \n"); 80 | continue; 81 | } 82 | 83 | /* 根据字符串解析出来的action,将信息打包封装 */ 84 | if (pack_msg(msg.action, &msg, sockfd, sizeof(msg)) != 0) 85 | { 86 | continue; 87 | } 88 | 89 | /* 当动作命令为帮助时,显示帮助界面8s, 再跳回主界面 */ 90 | if (msg.action == HELP) 91 | { 92 | clear_min(); 93 | help_dis(); //软件使用说明及注意事项和开发人员信息 94 | continue; 95 | } 96 | 97 | /* 当动作为退出时,关闭客户端进程 */ 98 | if (msg.action == EXIT) 99 | { 100 | exit(0); 101 | } 102 | 103 | /* 当动作为文件读出异常 */ 104 | if (msg.action == SENDFILE_ERROR) 105 | { 106 | continue; 107 | } 108 | 109 | /* 当动作为返回登录界面时 */ 110 | if (msg.action == BACK_LOGMENU) 111 | { 112 | continue; 113 | } 114 | /* 当动作为修改密码时 */ 115 | if (msg.action == RESET_PASSWD) 116 | { 117 | continue; 118 | } 119 | 120 | /* 当动作为VIP注册推出时 */ 121 | if (msg.action == VIPEXIT) 122 | { 123 | continue; 124 | } 125 | 126 | /* 像服务器发送数据包*/ 127 | n_write = write(sockfd, &msg, sizeof(msg)); 128 | if (n_write == -1) 129 | { 130 | printf("the client_main write to server error\n"); 131 | } 132 | /* 注册结束后清空数据包*/ 133 | if (msg.action == REGIST) 134 | { 135 | bzero(&msg.name, sizeof(msg.name)); 136 | bzero(&msg.pp_id, sizeof(msg.pp_id)); 137 | bzero(&msg.to_name, sizeof(msg.to_name)); 138 | bzero(&msg.to_id, sizeof(msg.to_id)); 139 | bzero(&msg.passwd, sizeof(msg.passwd)); 140 | bzero(&msg.passpro, sizeof(msg.passpro)); 141 | bzero(&msg.passkey, sizeof(msg.passkey)); 142 | bzero(&msg.msg, sizeof(msg.msg)); 143 | } 144 | sleep(1); 145 | } 146 | close(sockfd); 147 | exit(0); 148 | } 149 | -------------------------------------------------------------------------------- /include/PP_ONLINE.h: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | - Copyright (C): 2016 3 | - File name :PP_ONLINE.h 4 | - Author :- Zhao - 5 | - Date :2016年01月29日 星期五 20时56分45秒 6 | - Description : 7 | * *******************************************************/ 8 | /* 条件编译 */ 9 | #ifndef PP_ONLINE_H_ 10 | #define PP_ONLINE_H_ 11 | 12 | /* 头文件 */ 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | /* 宏定义 */ 30 | #define LOGIN 0 //宏定义登录action 31 | #define REGIST 1 //宏定义注册action 32 | #define FIND_PASSPRO 2 //宏定义找到密保问题action 33 | #define DIS_ONLINE_USER 3 //宏定义查看在线好友action 34 | #define CHATONE 4 //宏定义私聊action 35 | #define CHATALL 5 //宏定义群聊action 36 | #define RESET_PASSWD 6 //宏定义修改密码action 37 | #define HELP 7 //宏定义帮助action 38 | #define BACK_LOGMENU 8 //宏定义返回登录界面action 39 | #define SLENCE 9 //宏定义禁言action 40 | #define PERMITE 10 //宏定义解禁action 41 | #define TICK 11 //宏定义踢人action 42 | #define EXIT 12 //宏定义退出action 43 | #define SUCCESS 13 //宏定义成功 44 | #define LOGINSUPER 15 //宏定义超级用户 45 | #define SLENCEALREADY 16 //宏定义已经禁言 46 | #define PERMITEALREADY 17 //宏定义已经解禁 47 | #define DIS_SUCCESS 18 //宏定义查看好友结束 48 | #define TICKFAIL 19 //宏定义踢人失败 49 | #define PERMITESUCCESS 20 //宏定义解禁成功给VIP 50 | #define SLENCESUCCESS 21 //宏定义禁言成功给VIP 51 | #define TICKOK 22 //宏定义踢人返回给VIP 52 | #define TICKSUCCESS 23 //宏定义踢人成功 53 | #define PERMITEFAIL 24 //宏定义解禁的对象不存在 54 | #define SLENCEFAIL 25 //宏定义禁言用户不存在 55 | #define CHATONEFAIL 26 //宏定义私聊用户不存在 56 | #define LOGINFAIL 27 //宏定义登录用户已在线 57 | #define PASSKEY_ERROR 28 //宏定义密保答案回答错误 58 | #define RESET_SUCCESS 29 //宏定义密码改动成功 59 | #define SETSIGNER 30 //宏定义设置个性签名 60 | #define VIPMEMBER 31 //宏定义注册会员 61 | #define SENDBQ 32 //宏定义发送表情 62 | #define SENDDY 33 //宏定义发送短语 63 | #define SENDFILE 34 //宏定义发送文件 64 | #define DISJILU 35 //宏定义查看聊天记录 65 | #define VIPEXIT 36 //宏定义VIP注册退出 66 | #define SENDFILE_ERROR 37 //宏定义发送文件失败,对方用户不在线 67 | #define SENDFILETOSELF 38 //宏定义发送文件给自己 68 | #define CHATONETOSELF 39 //宏定义私聊和自己 69 | #define DIANZAN 40 //宏定义点赞 70 | #define DIANZANFAIL 41 //宏定义点赞失败 71 | #define WRONG_CMD 14 //宏定义输入指令出错action 72 | #define USERIDERROR -2 //宏定义ID出错 73 | #define USERPASSERR -3 //宏定义密码出错 74 | #define BUFFSIZE 1024 //数据缓冲区大小 75 | #define portnumber 8000 //端口号8000 76 | 77 | /* 78 | 定义一个结构体,用来封装消息,供服务器客户端通信 79 | */ 80 | typedef struct _Message 81 | { 82 | int count; //文件字符数计数 83 | int action; //服务器接受动作,传输选择协议 84 | int flag; //客户端接受动作 85 | int vip_flag; //超级用户标志位 86 | int activity; //发送表情短语状态切换 87 | int zan; //点赞标志位 88 | int pp_id; //存放PPid号 89 | int to_id; //存放对方PPid号 90 | 91 | char name[20]; //存放当前昵称 92 | char passwd[20]; //存放密码 93 | char signer[25]; //存放个性签名 94 | char passpro[50]; //存放密保问题 95 | char passkey[50]; //存放密保答案 96 | char to_name[20]; //存放对方昵称 97 | char msg[300]; //存放聊天的内容 98 | char filename[20]; //存放文件名 99 | char filemsg[2048]; //存放文件内容 100 | 101 | }Message; 102 | 103 | /* 104 | 定义一个结构体,用来记录当前在线好友 105 | 功能:主要为用户在线链表 106 | */ 107 | struct online 108 | { 109 | int online_flag; //禁言标志位 110 | int online_fd; //连接描述符 111 | int online_id; //在线用户ID 112 | 113 | char online_name[20]; //在线用户姓名 114 | 115 | struct online *next; 116 | }; 117 | 118 | /* 119 | 定义全局变量 120 | */ 121 | struct online *head; 122 | struct online *new_user; 123 | pthread_mutex_t mutex; //互斥锁 124 | sqlite3* db; 125 | 126 | /* 127 | 函数声明 128 | */ 129 | 130 | void log_dis(void); //登录界面动态 131 | void log_dis2(void); //登录界面静态 132 | void reg_dis(void); //注册界面 133 | void help_dis(void); //帮助界面 134 | void user_dis(void); //用户界面 135 | void clear_min(void); //请屏小窗口 136 | void loading_dis(void); //聊天室开机加载动画 137 | void rem_passwd(void); //修改密码界面 138 | void superuser_dis(void); //超级用户界面 139 | int input_check(char *); //字符串解析动作 140 | void file_recv(struct _Message *); //文件读取线程 141 | int pack_msg(int, Message *, int, int);//客户端数据打包 142 | void client_read_msg(void *); //客户端读线程 143 | void server_read_msg(void *); //服务器读线程 144 | int rand_brith_id(void); //6位的随机数产生函数,用来获取qq号 145 | void create_server_sql(sqlite3 *); //创建数据库 146 | int find_id_inbase(sqlite3 *, int); //查询IDs是否重复 147 | void insert_user_to_ppbase(sqlite3 *, struct _Message *); //将好友信息添加到数据库 148 | int check_passwd_inbase(sqlite3 *, int, char *); //登录信息核对 149 | void find_logname_inbase(sqlite3 *, struct _Message *); //根据上线ID得到该用户昵称 150 | void find_passpro_inbase(sqlite3 *, struct _Message *); //根据ID得到该用户的密保问题 151 | void find_passkey_inbase(sqlite3 *, struct _Message *); //根据ID得到该用户的密保答案并判断与用户输入是否一致 152 | void set_vipflag_inbase(sqlite3 *, struct _Message *); //根据ID更新数据库中的VIP_flag 153 | void update_signer_inbase(sqlite3 *, struct _Message *); //根据ID更新数据库中的个性签名 154 | void update_zan_inbase(sqlite3 *, struct _Message *); //根据ID更新数据库中的赞 155 | 156 | 157 | #endif 158 | -------------------------------------------------------------------------------- /PP_server/pp_sqlite3/src/pp_sqlite3.c: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | - Copyright (C): 2016 3 | - File name : pp_sqlite3.c 4 | - Author : - Zhao - 5 | - Date : 2016年01月30日 星期六 01时12分05秒 6 | - Description : 服务器端的数据库操作文件 7 | * *******************************************************/ 8 | #include "../../../include/PP_ONLINE.h" 9 | 10 | //创建数据库 11 | void create_server_sql(sqlite3 *db) 12 | { 13 | int ret = 0; 14 | char *sql = NULL; 15 | char *errmsg = NULL; 16 | 17 | /* 手动在堆上申请一段空间, 用来存储SQL命令 */ 18 | sql = (char *)malloc(sizeof(char) * 256); 19 | if (sql == NULL) 20 | { 21 | printf("create_server_sql sql malloc error!\n"); 22 | exit(1); 23 | } 24 | /* 创建了一个用户信息表 ppusertable */ 25 | ret = sqlite3_exec(db, "create table if not exists ppusertable(pp_id integer primary key, name varchar(20), passwd varchar(20), passpro varchar(50), passkey varchar(50), vip_flag integer, signer varchar(25), zan integer);", NULL, NULL, &errmsg); 26 | if (ret != SQLITE_OK) 27 | { 28 | fprintf(stderr, "Create table err:%s\n", sqlite3_errmsg(db)); 29 | } 30 | /* 首先在表中加入一个超级用户admin,id1000,昵称admin,密码admin,密保admin,密保答案admin */ 31 | sprintf(sql, "insert into ppusertable values(100000, 'admin', 'admin', 'admin', 'admin', 1, 'admin', 0);"); 32 | //printf("%s\n", sql); 33 | sqlite3_exec(db, sql, NULL, NULL, &errmsg); 34 | 35 | /* 释放手动malloc分配 */ 36 | free(sql); 37 | sql = NULL; 38 | } 39 | 40 | //查询ID是否重复 41 | int find_id_inbase(sqlite3 *db, int newid) 42 | { 43 | int ret = 0; 44 | int row = 0; 45 | int column = 0; 46 | 47 | char *sql = NULL; 48 | char *errmsg = NULL; 49 | char **resultSet = NULL; 50 | 51 | /* 给sql在堆空间上开辟100个字节 */ 52 | sql = (char *)malloc(sizeof(char) * 100); 53 | if (sql == NULL) 54 | { 55 | printf("find_id_in_ppbase sql malloc error!\n"); 56 | exit(1); 57 | } 58 | 59 | sprintf(sql, "select * from ppusertable where pp_id = %d;", newid); 60 | printf("sql = %s", sql); 61 | if (SQLITE_OK != sqlite3_get_table(db, sql, &resultSet, &row, &column, &errmsg)) 62 | { 63 | fprintf(stderr, "select err:%s\n", sqlite3_errmsg(db)); 64 | } 65 | /* 如果查询到的行数为0,则说明ID没有重复,新ID可用 */ 66 | if (row > 0) 67 | { 68 | ret = -1; 69 | } 70 | /* 数据库中没有这个ID */ 71 | if (row == 0) 72 | { 73 | ret = 0; 74 | } 75 | printf("###########ret = %d\n",ret); 76 | free(sql); 77 | sql = NULL; 78 | 79 | return ret; 80 | } 81 | 82 | /* 将好友信息加入服务器用户数据库 */ 83 | void insert_user_to_ppbase(sqlite3 *db, struct _Message *msg) 84 | { 85 | char* sql = NULL; 86 | char* errmsg = NULL; 87 | 88 | sql = (char *)malloc(sizeof(char) * 200); 89 | if (sql == NULL) 90 | { 91 | printf("insert_user_to_ppbase sql malloc error!\n"); 92 | exit(1); 93 | } 94 | 95 | sprintf(sql, "insert into ppusertable values(%d, '%s', '%s', '%s', '%s', 0, 'admin','0');", msg->pp_id, msg->name, msg->passwd, msg->passpro, msg->passkey); 96 | if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) 97 | { 98 | printf("ADD ppuser information failed!\n"); 99 | } 100 | else 101 | { 102 | printf("ADD ppuser successfully……\n"); 103 | } 104 | /* 对于手动申请的堆空间,使用完后要释放! */ 105 | free(sql); 106 | sql = NULL; 107 | } 108 | /* 登录信息在数据库中核对 */ 109 | int check_passwd_inbase(sqlite3 *db, int inputid, char *inputpasswd) 110 | { 111 | printf("inputid = %d\n", inputid); 112 | int ret = find_id_inbase(db, inputid); 113 | int row = 0; 114 | int column = 0; 115 | 116 | char* sql = NULL; 117 | char* errmsg = NULL; 118 | char** resultSet = NULL; 119 | 120 | if (ret == 0) 121 | { 122 | return USERIDERROR; //ID输入错误 123 | } 124 | //ret = -1说明数据库中有这个ID 125 | if (ret == -1) 126 | { 127 | sql = (char *)malloc(sizeof(char *) * 100); 128 | if (sql == NULL) 129 | { 130 | printf("check passwd sql malloc error!\n"); 131 | exit(1); 132 | } 133 | sprintf(sql, "select passwd from ppusertable where pp_id = %d;",inputid); 134 | //printf("check_passwd_sql = %s\n",sql); 135 | if (SQLITE_OK != sqlite3_get_table(db, sql, &resultSet, &row, &column, &errmsg)) 136 | { 137 | fprintf(stderr,"select passwd error!\n"); 138 | exit(1); 139 | } 140 | if (strcmp(inputpasswd, resultSet[1 * column + 0]) == 0) 141 | { 142 | free(sql); 143 | sql = NULL; 144 | return SUCCESS; 145 | } 146 | else 147 | { 148 | free(sql); 149 | sql = NULL; 150 | return USERPASSERR; 151 | } 152 | } 153 | return 0; 154 | } 155 | /* 通过ID得到数据库中用户昵称 */ 156 | void find_logname_inbase(sqlite3 *db, struct _Message *msg) 157 | { 158 | int row = 0; 159 | int column = 0; 160 | 161 | char* sql = NULL; 162 | char* errmsg = NULL; 163 | char** resultSet = NULL; 164 | 165 | sql = (char *)malloc(sizeof(char *) * 200); 166 | if (sql == NULL) 167 | { 168 | printf("find_logname sql malloc error!\n"); 169 | exit(1); 170 | } 171 | 172 | sprintf(sql, "select name from ppusertable where pp_id = %d;",msg->pp_id); 173 | 174 | printf("find_logname sql = %s\n", sql); 175 | 176 | if (SQLITE_OK != sqlite3_get_table(db, sql, &resultSet, &row, &column, &errmsg)) 177 | { 178 | fprintf(stderr,"select passwd error!\n"); 179 | exit(1); 180 | } 181 | printf("row = %d\n", row); 182 | if (row > 0) 183 | { 184 | strcpy(msg->name, resultSet[1 * column + 0]); 185 | memset(sql, 0, sizeof(sql)); 186 | sprintf(sql, "select vip_flag from ppusertable where pp_id = %d;", msg->pp_id); 187 | 188 | if (SQLITE_OK != sqlite3_get_table(db, sql, &resultSet, &row, &column, &errmsg)) 189 | { 190 | fprintf(stderr, "select vip_flag error!\n"); 191 | exit(1); 192 | } 193 | printf("LOGINSUPER vip_flag =%d\n", *resultSet[1 * column + 0]); 194 | //如果查到该用户的vip_flag == 1,则为超级用户 195 | if (*resultSet[1 * column + 0] - '0'== 1) 196 | { 197 | msg->flag = LOGINSUPER; 198 | } 199 | else 200 | { 201 | msg->flag = LOGIN; 202 | } 203 | memset(sql, 0, sizeof(sql)); 204 | sprintf(sql, "select signer from ppusertable where pp_id = %d;", msg->pp_id); 205 | if (SQLITE_OK != sqlite3_get_table(db, sql, &resultSet, &row, &column, &errmsg)) 206 | { 207 | fprintf(stderr, "select signer error!\n"); 208 | exit(1); 209 | } 210 | 211 | if (row > 0) 212 | { 213 | strcpy(msg->signer, resultSet[1 * column + 0]); 214 | printf("msg->signer = %s\n", msg->signer); 215 | } 216 | else 217 | { 218 | printf("select signer inbase error!\n"); 219 | exit(1); 220 | } 221 | memset(sql, 0, sizeof(sql)); 222 | sprintf(sql, "select zan from ppusertable where pp_id = %d;", msg->pp_id); 223 | 224 | if (SQLITE_OK != sqlite3_get_table(db, sql, &resultSet, &row, &column, &errmsg)) 225 | { 226 | fprintf(stderr, "select vip_flag error!\n"); 227 | exit(1); 228 | } 229 | printf("zan =%d\n", *resultSet[1 * column + 0] - '0'); 230 | 231 | msg->zan = *resultSet[1 * column + 0] - '0'; 232 | free(sql); 233 | sql = NULL; 234 | } 235 | else 236 | { 237 | printf("find_logname error!\n"); 238 | } 239 | } 240 | /* 通过ID得到数据库中密保问题 */ 241 | void find_passpro_inbase(sqlite3 *db, struct _Message *msg) 242 | { 243 | int row = 0; 244 | int column = 0; 245 | 246 | char* sql = NULL; 247 | char* errmsg = NULL; 248 | char** resultSet = NULL; 249 | 250 | sql = (char *)malloc(sizeof(char *) * 150); 251 | if (sql == NULL) 252 | { 253 | printf("check passwd sql malloc error!\n"); 254 | exit(1); 255 | } 256 | sprintf(sql, "select passpro from ppusertable where pp_id = %d;",msg->pp_id); 257 | 258 | if (SQLITE_OK != sqlite3_get_table(db, sql, &resultSet, &row, &column, &errmsg)) 259 | { 260 | fprintf(stderr,"select passwd error!\n"); 261 | exit(1); 262 | } 263 | if (row > 0) 264 | { 265 | strcpy(msg->passpro, resultSet[1 * column + 0]); 266 | free(sql); 267 | sql = NULL; 268 | } 269 | else 270 | { 271 | printf("find_passpro error!\n"); 272 | } 273 | } 274 | 275 | /* 通过ID得到数据库密保答案。如果密保回答正确,则修改密码*/ 276 | void find_passkey_inbase(sqlite3 *db, struct _Message *msg) 277 | { 278 | int row = 0; 279 | int column = 0; 280 | 281 | char* sql = NULL; 282 | char* errmsg = NULL; 283 | char** resultSet = NULL; 284 | 285 | sql = (char *)malloc(sizeof(char *) * 150); 286 | if (sql == NULL) 287 | { 288 | printf("check passwd sql malloc error!\n"); 289 | exit(1); 290 | } 291 | sprintf(sql, "select passkey from ppusertable where pp_id = %d;",msg->pp_id); 292 | 293 | if (SQLITE_OK != sqlite3_get_table(db, sql, &resultSet, &row, &column, &errmsg)) 294 | { 295 | fprintf(stderr,"select passwd error!\n"); 296 | exit(1); 297 | } 298 | if (row > 0) 299 | { 300 | //密保答案回答正确 301 | //可以修改密码 302 | if (strcmp(msg->passkey, resultSet[1 * column + 0]) == 0) 303 | { 304 | msg->flag = RESET_SUCCESS; 305 | memset(sql, 0, sizeof(sql)); 306 | sprintf(sql, "update ppusertable set passwd = '%s' where pp_id = %d;", msg->passwd, msg->pp_id); 307 | if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) 308 | { 309 | printf("updata passwd fail\n"); 310 | } 311 | else 312 | { 313 | printf("updata passwd success\n"); 314 | } 315 | } 316 | else 317 | { 318 | //密保答案错误 319 | msg->flag = PASSKEY_ERROR; 320 | } 321 | free(sql); 322 | sql = NULL; 323 | } 324 | else 325 | { 326 | printf("find_passkey error!\n"); 327 | } 328 | } 329 | 330 | /* 通过ID更新数据库中用户的vipflag */ 331 | void set_vipflag_inbase(sqlite3 *db, struct _Message *msg) 332 | { 333 | char* sql = NULL; 334 | char* errmsg = NULL; 335 | 336 | sql = (char *)malloc(sizeof(char *) * 150); 337 | if (sql == NULL) 338 | { 339 | printf("check passwd sql malloc error!\n"); 340 | exit(1); 341 | } 342 | 343 | sprintf(sql, "update ppusertable set vip_flag = %d where pp_id = %d;", msg->vip_flag, msg->pp_id); 344 | if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) 345 | { 346 | printf("updata vipflag fail\n"); 347 | } 348 | else 349 | { 350 | printf("updata vipflag success\n"); 351 | } 352 | free(sql); 353 | sql = NULL; 354 | } 355 | 356 | /* 通过ID更新数据库中用户的zan*/ 357 | void update_zan_inbase(sqlite3 *db, struct _Message *msg) 358 | { 359 | int zan = 0; 360 | 361 | int row = 0; 362 | int column = 0; 363 | 364 | char* sql = NULL; 365 | char* errmsg = NULL; 366 | char** resultSet = NULL; 367 | 368 | sql = (char *)malloc(sizeof(char *) * 150); 369 | if (sql == NULL) 370 | { 371 | printf("check passwd sql malloc error!\n"); 372 | exit(1); 373 | } 374 | memset(sql, 0, sizeof(sql)); 375 | sprintf(sql, "select zan from ppusertable where pp_id = %d;", msg->to_id); 376 | 377 | if (SQLITE_OK != sqlite3_get_table(db, sql, &resultSet, &row, &column, &errmsg)) 378 | { 379 | fprintf(stderr, "select vip_flag error!\n"); 380 | exit(1); 381 | } 382 | printf("zan =%d\n", *resultSet[1 * column + 0] - '0'); 383 | //如果查到该用户的vip_flag == 1,则为超级用户 384 | zan = (*resultSet[1 * column + 0] - '0'); 385 | zan = zan + 1; 386 | 387 | memset(sql, 0, sizeof(sql)); 388 | sprintf(sql, "update ppusertable set zan = %d where pp_id = %d;", zan, msg->pp_id); 389 | if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) 390 | { 391 | printf("updata zan fail\n"); 392 | } 393 | else 394 | { 395 | printf("updata zan success\n"); 396 | } 397 | msg->zan = zan; 398 | 399 | free(sql); 400 | sql = NULL; 401 | } 402 | 403 | /* 通过ID更新数据库中用户的个性签名*/ 404 | void update_signer_inbase(sqlite3 *db, struct _Message *msg) 405 | { 406 | char* sql = NULL; 407 | char* errmsg = NULL; 408 | 409 | sql = (char *)malloc(sizeof(char *) * 150); 410 | if (sql == NULL) 411 | { 412 | printf("check passwd sql malloc error!\n"); 413 | exit(1); 414 | } 415 | 416 | sprintf(sql, "update ppusertable set signer = '%s' where pp_id = %d;", msg->signer, msg->pp_id); 417 | if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, &errmsg)) 418 | { 419 | printf("updata signer fail\n"); 420 | } 421 | else 422 | { 423 | printf("updata signer success\n"); 424 | } 425 | free(sql); 426 | sql = NULL; 427 | } 428 | 429 | -------------------------------------------------------------------------------- /PP_client/client_thread/src/client_thread.c: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | - Copyright (C): 2016 3 | - File name : client_thread.c 4 | - Author : - Zhao - 5 | - Date : 2016年01月30日 星期六 00时53分13秒 6 | - Description : 客户端线程处理函数,不断的读取服务器的输入 7 | * *******************************************************/ 8 | #include "../../../include/PP_ONLINE.h" 9 | 10 | void client_read_msg(void *arg) 11 | { 12 | int fd = *((int *)arg); 13 | int n_read; 14 | 15 | int count = 0; 16 | 17 | struct _Message *msg = (struct _Message *)malloc(sizeof(struct _Message)); 18 | 19 | while (1) 20 | { 21 | memset(msg, 0, sizeof(struct _Message)); 22 | //阻塞式不停的从服务器读取传回的数据 23 | n_read = read(fd, msg, sizeof(struct _Message)); 24 | if (n_read == 0) 25 | { 26 | close(fd); 27 | pthread_exit(NULL); 28 | } 29 | 30 | //根据msg->flag执行相应的操作 31 | switch(msg->flag) 32 | { 33 | /* 当服务器返回普通用户登录成功动作 */ 34 | case LOGIN: 35 | { 36 | printf("\033[25;45H \n"); 37 | printf("\033[25;45H*恭喜您已成功登录!\n"); 38 | sleep(1); 39 | user_dis(); 40 | printf("\033[4;103H %s\n", msg->name); 41 | printf("\033[6;103H %d\n", msg->pp_id); 42 | printf("\033[6;114H%d\n", msg->zan); 43 | //如果该用户设置过个性签名,则登录时显示该用户设置的个性签名 44 | if (strcmp("admin", msg->signer) != 0) 45 | { 46 | printf("\033[8;95H \n"); 47 | printf("\033[8;95H %s\n", msg->signer); 48 | } 49 | break; 50 | } 51 | 52 | /* 当服务器返回超级用户登录成功动作 */ 53 | case LOGINSUPER: 54 | { 55 | printf("\033[25;45H \n"); 56 | printf("\033[25;45H*超级用户已成功登录!\n"); 57 | sleep(1); 58 | superuser_dis(); 59 | printf("\033[4;103H %s(VIP)\n", msg->name); 60 | printf("\033[6;103H %d\n", msg->pp_id); 61 | printf("\033[6;114H%d\n", msg->zan); 62 | //如果该用户设置过个性签名,则登录时显示该用户设置的个性签名 63 | if (strcmp("admin", msg->signer) != 0) 64 | { 65 | printf("\033[8;95H \n"); 66 | printf("\033[8;95H %s\n", msg->signer); 67 | } 68 | break; 69 | } 70 | 71 | /* 当服务器返回登录出错的动作时 */ 72 | case LOGINFAIL: 73 | { 74 | printf("\033[25;45H \n"); 75 | printf("\033[25;45H*您的帐号已经在别的地方登录!\n"); 76 | printf("\033[14;63H \n"); 77 | printf("\033[17;63H \n"); 78 | break; 79 | } 80 | 81 | /* 当服务器返回登录ID出错动作 */ 82 | case USERIDERROR: 83 | { 84 | printf("\033[25;45H \n"); 85 | printf("\033[25;45H*您输入的ID号有错误!\n"); 86 | printf("\033[14;63H \n"); 87 | printf("\033[17;63H \n"); 88 | break; 89 | } 90 | 91 | /* 当服务器返回登录密码出错动作 */ 92 | case USERPASSERR: 93 | { 94 | printf("\033[25;45H \n"); 95 | printf("\033[25;45H*您输入的密码错误!\n"); 96 | printf("\033[14;63H \n"); 97 | printf("\033[17;63H \n"); 98 | break; 99 | } 100 | 101 | /* 当服务器返回注册动作 */ 102 | case REGIST: 103 | { 104 | printf("\033[25;45H \n"); 105 | printf("\033[25;45H*welcome to PP_ONLINE,you ppid is %d\n", msg->pp_id); 106 | break; 107 | } 108 | 109 | /* 当服务器返回查看在线好友信息 */ 110 | case DIS_ONLINE_USER: 111 | { 112 | /* 如果重新执行查询命令则清除显示好友区域 */ 113 | if (count == 0) 114 | { 115 | printf("\033[10;86H \n"); 116 | printf("\033[11;86H \n"); 117 | printf("\033[12;86H \n"); 118 | printf("\033[13;86H \n"); 119 | printf("\033[14;86H \n"); 120 | printf("\033[15;86H \n"); 121 | printf("\033[16;86H \n"); 122 | printf("\033[17;86H \n"); 123 | } 124 | 125 | count++; 126 | /* 收到查看好友结束命令则清0计数 */ 127 | if (msg->action == DIS_SUCCESS) 128 | { 129 | count = 0; 130 | } 131 | 132 | printf("\033[10;86H*在线好友如下:\n"); 133 | if (count == 1) 134 | { 135 | if (msg->pp_id == msg->to_id) 136 | { 137 | printf("\033[11;86H*NAME:%s\n", msg->to_name); 138 | printf("\033[11;102H*PPID:%d(自己)\n", msg->to_id); 139 | } 140 | else 141 | { 142 | printf("\033[11;86H*NAME:%s\n", msg->to_name); 143 | printf("\033[11;102H*PPID:%d\n", msg->to_id); 144 | } 145 | } 146 | else if (count == 2) 147 | { 148 | if (msg->pp_id == msg->to_id) 149 | { 150 | printf("\033[12;86H*NAME:%s\n", msg->to_name); 151 | printf("\033[12;102H*PPID:%d(自己)\n", msg->to_id); 152 | } 153 | else 154 | { 155 | printf("\033[12;86H*NAME:%s\n", msg->to_name); 156 | printf("\033[12;102H*PPID:%d\n", msg->to_id); 157 | } 158 | } 159 | else if (count == 3) 160 | { 161 | if (msg->pp_id == msg->to_id) 162 | { 163 | printf("\033[13;86H*NAME:%s\n", msg->to_name); 164 | printf("\033[13;102H*PPID:%d(自己)\n", msg->to_id); 165 | } 166 | else 167 | { 168 | printf("\033[13;86H*NAME:%s\n", msg->to_name); 169 | printf("\033[13;102H*PPID:%d\n", msg->to_id); 170 | } 171 | } 172 | else if (count == 4) 173 | { 174 | if (msg->pp_id == msg->to_id) 175 | { 176 | printf("\033[14;86H*NAME:%s\n", msg->to_name); 177 | printf("\033[14;102H*PPID:%d(自己)\n", msg->to_id); 178 | } 179 | else 180 | { 181 | printf("\033[14;86H*NAME:%s\n", msg->to_name); 182 | printf("\033[14;102H*PPID:%d\n", msg->to_id); 183 | } 184 | } 185 | else if (count == 5) 186 | { 187 | if (msg->pp_id == msg->to_id) 188 | { 189 | printf("\033[15;86H*NAME:%s\n", msg->to_name); 190 | printf("\033[15;102H*PPID:%d(自己)\n", msg->to_id); 191 | } 192 | else 193 | { 194 | printf("\033[15;86H*NAME:%s\n", msg->to_name); 195 | printf("\033[15;102H*PPID:%d\n", msg->to_id); 196 | } 197 | } 198 | else if (count == 6) 199 | { 200 | if (msg->pp_id == msg->to_id) 201 | { 202 | printf("\033[16;86H*NAME:%s\n", msg->to_name); 203 | printf("\033[16;102H*PPID:%d(自己)\n", msg->to_id); 204 | } 205 | else 206 | { 207 | printf("\033[16;86H*NAME:%s\n", msg->to_name); 208 | printf("\033[16;102H*PPID:%d\n", msg->to_id); 209 | } 210 | } 211 | else if (count == 7) 212 | { 213 | if (msg->pp_id == msg->to_id) 214 | { 215 | printf("\033[17;86H*NAME:%s\n", msg->to_name); 216 | printf("\033[17;102H*PPID:%d(自己)\n", msg->to_id); 217 | } 218 | else 219 | { 220 | printf("\033[17;86H*NAME:%s\n", msg->to_name); 221 | printf("\033[17;102H*PPID:%d\n", msg->to_id); 222 | } 223 | } 224 | else if (count == 8) 225 | { 226 | count = 0; 227 | printf("\033[18;86H*您的好友已达上限!\n"); 228 | } 229 | break; 230 | } 231 | 232 | /* 当服务器返回私聊信息时 */ 233 | case CHATONE: 234 | { 235 | clear_min(); 236 | 237 | if (msg->activity == 1) 238 | { 239 | printf("\033[6;15H*%s 对你发送表情:%s", msg->name, msg->msg); 240 | printf("\033[28;72H"); 241 | msg->activity = 0; 242 | fflush(stdout); 243 | 244 | break; 245 | } 246 | else if (msg->activity == 2) 247 | { 248 | 249 | printf("\033[6;15H*%s 对你发送短语:%s", msg->name, msg->msg); 250 | printf("\033[28;72H"); 251 | msg->activity = 0; 252 | fflush(stdout); 253 | 254 | break; 255 | } 256 | else 257 | { 258 | printf("\033[5;15H*%s 悄悄对你说:%s", msg->name, msg->msg); 259 | printf("\033[28;72H"); 260 | fflush(stdout); 261 | break; 262 | } 263 | } 264 | 265 | /* 当服务器返回私聊用户不存在信息时 */ 266 | case CHATONEFAIL: 267 | { 268 | clear_min(); 269 | printf("\033[5;15H*您想要私聊的用户不存在/已下线\n"); 270 | break; 271 | } 272 | 273 | /* 当服务器返回群聊信息时 */ 274 | case CHATALL: 275 | { 276 | clear_min(); 277 | if (msg->activity == 1) 278 | { 279 | printf("\033[6;15H*%s 发送表情:%s", msg->name, msg->msg); 280 | printf("\033[28;72H"); 281 | msg->activity = 0; 282 | fflush(stdout); 283 | break; 284 | } 285 | else if (msg->activity == 2) 286 | { 287 | printf("\033[6;15H*%s 发送短语:%s", msg->name, msg->msg); 288 | printf("\033[28;72H"); 289 | msg->activity = 0; 290 | fflush(stdout); 291 | break; 292 | } 293 | else 294 | { 295 | printf("\033[5;15H*%s 对大家说:%s", msg->name, msg->msg); 296 | printf("\033[28;72H"); 297 | fflush(stdout); 298 | break; 299 | } 300 | } 301 | 302 | /* 当服务器返回禁言信息时 */ 303 | case SLENCE: 304 | { 305 | clear_min(); 306 | printf("\033[5;15H*您已被禁言,不能发言!"); 307 | printf("\033[28;72H"); 308 | fflush(stdout); 309 | 310 | break; 311 | } 312 | 313 | /* 当服务器返回禁言成功消息给VIP时 */ 314 | case SLENCESUCCESS: 315 | { 316 | clear_min(); 317 | printf("\033[6;15H该用户已经成功被禁言!\n"); 318 | printf("\033[29;0H\n"); 319 | break; 320 | } 321 | 322 | /* 当服务器返回已经禁言的信息时 */ 323 | case SLENCEALREADY: 324 | { 325 | clear_min(); 326 | printf("\033[5;15H*该用户已经被禁言过了!\n"); 327 | printf("\033[29;0H\n"); 328 | break; 329 | } 330 | 331 | /* 当服务器返回禁言用户不在线时 */ 332 | case SLENCEFAIL: 333 | { 334 | clear_min(); 335 | printf("\033[6;15H*您想禁言的用户不存在/已下线,谢谢\n"); 336 | printf("\033[29;0H\n"); 337 | break; 338 | } 339 | 340 | /* 当服务器返回解禁信息时 */ 341 | case PERMITE: 342 | { 343 | clear_min(); 344 | printf("\033[5;15H*您已被解禁,可以发言了哦!"); 345 | printf("\033[28;72H"); 346 | fflush(stdout); 347 | break; 348 | } 349 | 350 | /* 当服务器返回解禁成功消息给VIP时 */ 351 | case PERMITESUCCESS: 352 | { 353 | clear_min(); 354 | printf("\033[6;15H*该用户已经成功被解禁!\n"); 355 | printf("\033[29;0H\n"); 356 | break; 357 | } 358 | 359 | /* 当服务器返回已经解禁的信息时 */ 360 | case PERMITEALREADY: 361 | { 362 | clear_min(); 363 | printf("\033[5;15H(^_^)该用户已经被解禁过了!\n"); 364 | printf("\033[29;0H\n"); 365 | break; 366 | } 367 | 368 | /* 当服务器返回解禁用户不存在时 */ 369 | case PERMITEFAIL: 370 | { 371 | clear_min(); 372 | printf("\033[6;15H*您想解禁的用户不存在/已下线,谢谢\n"); 373 | break; 374 | } 375 | 376 | /* 当服务器返回踢人成功信息时 */ 377 | case TICKSUCCESS: 378 | { 379 | clear_min(); 380 | printf("\033[5;15H*(*_*)很抱歉,您被管理员踢下线了\n"); 381 | printf("\033[6;20H*请重新登录!\n"); 382 | printf("\033[30;45H\n"); 383 | sleep(2); 384 | exit(0); 385 | break; 386 | } 387 | 388 | /* 当服务器返回踢人成功消息给VIP时 */ 389 | case TICKOK: 390 | { 391 | clear_min(); 392 | printf("\033[6;15H*该用户已经成功被踢下线\n"); 393 | printf("\033[29;0H\n"); 394 | break; 395 | } 396 | 397 | /* 当服务器返回踢人失败信息时 */ 398 | case TICKFAIL: 399 | { 400 | clear_min(); 401 | printf("\033[6;15H*你想要踢出的好友不存在/已下线,谢谢\n"); 402 | printf("\033[29;0H\n"); 403 | break; 404 | } 405 | 406 | /* 当服务器返回查找密保问题信息时 */ 407 | case FIND_PASSPRO: 408 | { 409 | printf("\033[18;47H \n"); 410 | printf("\033[18;52H%s?\n",msg->passpro); 411 | break; 412 | } 413 | /* 当服务器返回密保答案错误时 */ 414 | case PASSKEY_ERROR: 415 | { 416 | printf("\033[20;52H \n"); 417 | printf("\033[22;51H \n"); 418 | printf("\033[22;51H*密保答案错误,请重试\n"); 419 | break; 420 | } 421 | /* 当服务器返回修改密码成功时*/ 422 | case RESET_SUCCESS: 423 | { 424 | printf("\033[20;52H \n"); 425 | printf("\033[22;51H \n"); 426 | log_dis2(); 427 | printf("\033[26;45H*密码修改成功!"); 428 | printf("\033[28;72H"); 429 | fflush(stdout); 430 | break; 431 | } 432 | 433 | /* 当服务器返回订制VIPok时 */ 434 | case VIPMEMBER: 435 | { 436 | clear_min(); 437 | 438 | printf("\033[4;103H %s(VIP)\n", msg->to_name); 439 | printf("\033[18;16H*禁言(NOS) *解禁(CAS) *踢人(OUT)\n"); 440 | printf("\033[29;0H\n"); 441 | break; 442 | } 443 | 444 | /* 当服务器返回设置个性签名时 */ 445 | case SETSIGNER: 446 | { 447 | printf("\033[14;14H *恭喜您,您的个性已成功到帐* \n"); 448 | printf("\033[8;95H \n"); 449 | printf("\033[8;95H %s\n", msg->signer); 450 | sleep(1); 451 | 452 | clear_min(); 453 | printf("\033[29;0H\n"); 454 | break; 455 | } 456 | 457 | case DIANZANFAIL: 458 | { 459 | clear_min(); 460 | 461 | printf("\033[6;14H *您要赞的好友不存在或已下线\n"); 462 | break; 463 | } 464 | 465 | case DIANZAN: 466 | { 467 | clear_min(); 468 | printf("\033[6;14H *好友%s对你点赞 +1 ", msg->name); 469 | printf("\033[6;114H "); 470 | printf("\033[6;114H%d", msg->zan); 471 | printf("\033[28;72H"); 472 | fflush(stdout); 473 | 474 | break; 475 | } 476 | 477 | /* 服务器返回私聊和自己时 */ 478 | case CHATONETOSELF: 479 | { 480 | printf("\033[6;14H *对不起,您不能和自己聊天!\n"); 481 | sleep(1); 482 | clear_min(); 483 | printf("\033[29;0H\n"); 484 | break; 485 | } 486 | 487 | /* 当服务器返回发送文件失败时 */ 488 | case SENDFILE_ERROR: 489 | { 490 | printf("\033[11;14H \n"); 491 | printf("\033[11;14H *好友%d不存在或者已下线!\n", msg->to_id); 492 | sleep(1); 493 | 494 | clear_min(); 495 | printf("\033[29;0H\n"); 496 | break; 497 | } 498 | 499 | /* 当服务器返回发送文件给自己时 */ 500 | case SENDFILETOSELF: 501 | { 502 | printf("\033[11;14H *不可以发送文件给自己\n"); 503 | sleep(1); 504 | clear_min(); 505 | printf("\033[29;0H\n"); 506 | break; 507 | } 508 | 509 | /* 当服务器返回发送文件时 */ 510 | case SENDFILE: 511 | { 512 | file_recv(msg); 513 | break; 514 | } 515 | /* 查看聊天记录 */ 516 | case DISJILU: 517 | { 518 | int j = 0; 519 | int count = 0; 520 | char *ptr = msg->filemsg; 521 | char *sub = msg->filemsg; 522 | char *pna = msg->name; 523 | char *src; 524 | char name[20]; 525 | char action[20]; 526 | char to_name[20]; 527 | char msg[300]; 528 | char time[50]; 529 | char recvbuf[2048]; 530 | //解析接收到的数据 531 | //然后显示 532 | clear_min(); 533 | printf("\033[5;14H *聊天记录* \n"); 534 | while(*sub != '\0') 535 | { 536 | if (*sub == '$') 537 | { 538 | *sub = '\0'; 539 | strcpy(recvbuf, ptr); 540 | ptr = ptr + strlen(recvbuf) + 2; 541 | //对一个聊天记录进行解析 542 | src = recvbuf; 543 | while (*src != '#') 544 | { 545 | name[j++] = *src; 546 | src++; 547 | } 548 | name[j] = '\0'; 549 | j = 0; 550 | src++; 551 | while (*src != '#') 552 | { 553 | action[j++] = *src; 554 | src++; 555 | } 556 | action[j] = '\0'; 557 | j = 0; 558 | src++; 559 | while (*src != '#') 560 | { 561 | to_name[j++] = *src; 562 | src++; 563 | } 564 | to_name[j] = '\0'; 565 | j = 0; 566 | src++; 567 | while (*src != '#') 568 | { 569 | msg[j++] = *src; 570 | src++; 571 | } 572 | msg[j] = '\0'; 573 | j = 0; 574 | src++; 575 | while (*src != '\0') 576 | { 577 | time[j++] = *src; 578 | src++; 579 | } 580 | time[j] = '\0'; 581 | j = 0; 582 | //如果这条聊天记录和该用户有关则显示 583 | if ((strcmp(pna, name) == 0) || (strcmp(pna, to_name) == 0)) 584 | { 585 | count++; 586 | if (strcmp(action, "CHATONE") == 0) 587 | { 588 | switch(count) 589 | { 590 | case 1: 591 | { 592 | printf("\033[6;14HTIME:%s", time); 593 | printf("\033[7;14H%s对%s悄悄说:%s", name, to_name, msg); 594 | break; 595 | } 596 | case 2: 597 | { 598 | printf("\033[8;14HTIME:%s", time); 599 | printf("\033[9;14H%s对%s悄悄说:%s", name, to_name, msg); 600 | break; 601 | } 602 | case 3: 603 | { 604 | printf("\033[10;14HTIME:%s", time); 605 | printf("\033[11;14H%s对%s悄悄说:%s", name, to_name, msg); 606 | break; 607 | } 608 | case 4: 609 | { 610 | printf("\033[12;14HTIME:%s", time); 611 | printf("\033[13;14H%s对%s悄悄说:%s", name, to_name, msg); 612 | break; 613 | } 614 | case 5: 615 | { 616 | printf("\033[14;14HTIME:%s", time); 617 | printf("\033[15;14H%s对%s悄悄说:%s", name, to_name, msg); 618 | break; 619 | } 620 | default: 621 | { 622 | count = 0; 623 | sleep(2); 624 | clear_min(); 625 | break; 626 | } 627 | } 628 | fflush(stdout); 629 | } 630 | else if (strcmp(action, "CHATALL") == 0) 631 | { 632 | switch(count) 633 | { 634 | case 1: 635 | { 636 | printf("\033[6;14HTIME:%s", time); 637 | printf("\033[7;14H%s对大家说:%s", name, msg); 638 | break; 639 | } 640 | case 2: 641 | { 642 | printf("\033[8;14HTIME:%s", time); 643 | printf("\033[9;14H%s对大家说%s", name, msg); 644 | break; 645 | } 646 | case 3: 647 | { 648 | printf("\033[10;14HTIME:%s", time); 649 | printf("\033[11;14H%s对大家说%s", name, msg); 650 | break; 651 | } 652 | case 4: 653 | { 654 | printf("\033[12;14HTIME:%s", time); 655 | printf("\033[13;14H%s对大家说%s", name, msg); 656 | break; 657 | } 658 | case 5: 659 | { 660 | printf("\033[14;14HTIME:%s", time); 661 | printf("\033[15;14H%s对大家说%s", name, msg); 662 | break; 663 | } 664 | default: 665 | { 666 | count = 0; 667 | sleep(2); 668 | clear_min(); 669 | break; 670 | } 671 | } 672 | fflush(stdout); 673 | } 674 | } 675 | } 676 | sub++; 677 | } 678 | printf("\033[28;72H"); 679 | fflush(stdout); 680 | break; 681 | } 682 | } 683 | } 684 | } 685 | 686 | 687 | 688 | 689 | -------------------------------------------------------------------------------- /PP_client/pack_msg/src/pack_msg.c: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | - Copyright (C): 2016 3 | - File name : pack_msg.c 4 | - Author : - Zhao - 5 | - Date : 2016年01月30日 星期六 13时22分29秒 6 | - Description : 将数据打包 7 | - input : 动作action,指向msg的数据结构指针*msg, 8 | - output : 9 | - return : 成功返回0 10 | * *******************************************************/ 11 | #include "../../../include/PP_ONLINE.h" 12 | 13 | int pack_msg(int action, Message *msg, int sockfd, int msgbytes) 14 | { 15 | int i; 16 | int n_write; //实际写入的字节数 17 | 18 | char buffer[BUFFSIZE]; //缓冲区 19 | memset(buffer, 0, BUFFSIZE); 20 | 21 | /* 根据action动作选项选择执行 */ 22 | switch(action) 23 | { 24 | /* 当命令为登录 */ 25 | case LOGIN: 26 | { 27 | printf("\033[14;63H"); 28 | scanf("%d", &msg->pp_id); 29 | 30 | /* 获取输入密码 */ 31 | while (1) 32 | { 33 | memset(buffer, 0, BUFFSIZE); 34 | printf("\033[25;45H \n"); 35 | printf("\033[17;63H"); 36 | scanf("%s", buffer); 37 | 38 | for (i = 0; buffer[i] != '\0'; i++) 39 | { 40 | msg->passwd[i] = buffer[i]; 41 | } 42 | msg->passwd[i] = '\0'; 43 | 44 | break; 45 | } 46 | break; 47 | } 48 | 49 | /* 当命令为注册时 */ 50 | case REGIST: 51 | { 52 | memset(buffer, 0, BUFFSIZE); 53 | reg_dis(); //跳到注册界面 54 | /* 循环,直到用户输入正确的用户名,错误则重新输入 */ 55 | while (1) 56 | { 57 | printf("\033[10;57H \n"); 58 | printf("\033[25;59H \n"); 59 | printf("\033[10;57H"); 60 | fgets(buffer, BUFFSIZE, stdin); 61 | for (i = 0; buffer[i] != '\n'; i++) 62 | { 63 | msg->name[i]= buffer[i]; 64 | } 65 | if (strlen(buffer) == 1) 66 | { 67 | printf("\033[25;59H*用户名不可以为空哦!\n"); 68 | continue; 69 | } 70 | if (i >= 20) 71 | { 72 | printf("\033[25;59H*您输入的用户名过长!\n"); 73 | continue; 74 | } 75 | msg->name[i] = '\0'; 76 | printf("\033[10;85HOK^_^\n"); 77 | break; 78 | } 79 | char temp[20]; /* 临时变量,用来比较两次密码是否相同 */ 80 | memset(temp, 0, sizeof(temp)); 81 | memset(buffer, 0, sizeof(buffer)); 82 | /* 循环体,检查两次密码输入是否正确 */ 83 | while ((strcmp(msg->passwd, temp) != 0) || (strlen(msg->passwd) == 0)) 84 | { 85 | while (1) 86 | { 87 | printf("\033[13;57H \n"); 88 | printf("\033[13;57H"); 89 | fgets(buffer, BUFFSIZE, stdin); 90 | printf("\033[25;59H \n"); 91 | for (i = 0; buffer[i] != '\n'; i++) 92 | { 93 | msg->passwd[i] = buffer[i]; 94 | } 95 | if (strlen(buffer) == 1) 96 | { 97 | printf("\033[25;59H*密码不能为空哦!\n"); 98 | continue; 99 | } 100 | if (i >= 20) 101 | { 102 | printf("\033[25;59H*您输入的密码过长!\n"); 103 | continue; 104 | } 105 | msg->passwd[i] = '\0'; 106 | strcpy(temp, msg->passwd); 107 | printf("\033[13;85HOK^_^\n"); 108 | printf("\033[16;57H \n"); 109 | printf("\033[16;57H"); 110 | 111 | memset(buffer, 0, BUFFSIZE); 112 | fgets(buffer, BUFFSIZE, stdin); 113 | 114 | printf("\033[25;59H \n"); 115 | for (i = 0; buffer[i] != '\n'; i++) 116 | { 117 | msg->passwd[i] = buffer[i]; 118 | } 119 | msg->passwd[i] = '\0'; 120 | if (strcmp(msg->passwd, temp) != 0) 121 | { 122 | printf("\033[25;59H*两次输入不一致\n"); 123 | printf("\033[16;57H \n"); 124 | printf("\033[13;85H \n"); 125 | continue; 126 | } 127 | else 128 | { 129 | printf("\033[25;59H*两次密码比对正确!\n"); 130 | printf("\033[16;85HOK^_^\n"); 131 | break; 132 | } 133 | } 134 | } 135 | /* 循环,输入密保问题,保证格式正确 */ 136 | memset(buffer, 0, BUFFSIZE); 137 | while (1) 138 | { 139 | printf("\033[19;57H \n"); 140 | printf("\033[19;57H"); 141 | fgets(buffer, BUFFSIZE, stdin); 142 | printf("\033[25;59H \n"); 143 | for (i = 0; buffer[i] != '\n'; i++) 144 | { 145 | msg->passpro[i]= buffer[i]; 146 | } 147 | if (strlen(buffer) == 1) 148 | { 149 | printf("\033[25;59H*密保问题不可以为空哦!\n"); 150 | continue; 151 | } 152 | if (i >= 50) 153 | { 154 | printf("\033[25;59H*您输入的密保问题过长!\n"); 155 | continue; 156 | } 157 | msg->passpro[i] = '\0'; 158 | printf("\033[19;85HOK^_^\n"); 159 | break; 160 | } 161 | /* 循环,并且确保密保答案输入正确 */ 162 | memset(buffer, 0, BUFFSIZE); 163 | while (1) 164 | { 165 | printf("\033[22;57H \n"); 166 | printf("\033[22;57H"); 167 | fgets(buffer, BUFFSIZE, stdin); 168 | printf("\033[25;59H \n"); 169 | for (i = 0; buffer[i] != '\n'; i++) 170 | { 171 | msg->passkey[i]= buffer[i]; 172 | } 173 | if (strlen(buffer) == 1) 174 | { 175 | printf("\033[25;59H*密保答案不可以为空哦!\n"); 176 | continue; 177 | } 178 | if (i >= 20) 179 | { 180 | printf("\033[25;59H*您输入的密保答案过长!\n"); 181 | continue; 182 | } 183 | msg->passkey[i] = '\0'; 184 | printf("\033[22;85HOK^_^\n"); 185 | printf("\033[25;50H*恭喜您注册成功!,正在生成PP号\n"); 186 | printf("\033[26;56H loading......\n"); 187 | sleep(2); 188 | log_dis2(); 189 | break; 190 | } 191 | break; 192 | } 193 | 194 | /* 当命令为查看在线好友时 */ 195 | case DIS_ONLINE_USER: 196 | { 197 | msg->action = DIS_ONLINE_USER; 198 | break; 199 | } 200 | 201 | /* 当命令为私聊时 */ 202 | case CHATONE: 203 | { 204 | msg->to_id = 0; 205 | msg->action = CHATONE; 206 | clear_min(); 207 | 208 | printf("\033[5;15H*请输入聊天好友ID:"); 209 | scanf("%d", &msg->to_id); 210 | 211 | printf("\033[7;15H*你对%d悄悄说:", msg->to_id); 212 | scanf("%s", msg->msg); 213 | 214 | break; 215 | } 216 | 217 | /* 当命令为群聊时 */ 218 | case CHATALL: 219 | { 220 | msg->action = CHATALL; 221 | clear_min(); 222 | 223 | printf("\033[6;15H*你对大家说:"); 224 | scanf("%s", msg->msg); 225 | break; 226 | } 227 | 228 | /* 当命令为修改密码时 */ 229 | case RESET_PASSWD: 230 | { 231 | memset(buffer, 0, sizeof(buffer)); 232 | //跳转到安全中心界面 233 | rem_passwd(); 234 | //获取需要修改密码的PP号 235 | printf("\033[14;53H"); 236 | scanf("%d", &msg->pp_id); 237 | msg->action = FIND_PASSPRO; 238 | //根据PP号去数据库中查找该用户的密保问题 239 | n_write = write(sockfd, msg, msgbytes); 240 | //printf("RESET——PASSWD find_passpro write = %d", n_write); 241 | if (n_write == -1) 242 | { 243 | printf("reset_passwd write ppid error!\n"); 244 | exit(1); 245 | } 246 | sleep(1); 247 | while(1) 248 | { 249 | //获取密保问题的答案 250 | printf("\033[20;53H"); 251 | scanf("%s", msg->passkey); 252 | printf("\033[22;51H \n"); 253 | if (strlen(msg->passkey) == 1) 254 | { 255 | printf("\033[22;52H*密保答案不可以为空哦!\n"); 256 | continue; 257 | } 258 | break; 259 | } 260 | //获取新设置的PP号密码 261 | while (1) 262 | { 263 | printf("\033[16;47H \n"); 264 | printf("\033[18;47H \n"); 265 | printf("\033[18;52H请设置新密码\n"); 266 | printf("\033[20;53H \n"); 267 | memset(buffer, 0, BUFFSIZE); 268 | printf("\033[20;53H"); 269 | fgets(buffer, BUFFSIZE, stdin); 270 | for (i = 0; buffer[i] != '\n'; i++) 271 | { 272 | msg->passwd[i] = buffer[i]; 273 | } 274 | if (strlen(buffer) == 1) 275 | { 276 | continue; 277 | } 278 | if (i >= 20) 279 | { 280 | printf("\033[22;51H*您输入的密码过长!\n"); 281 | continue; 282 | } 283 | msg->passwd[i] = '\0'; 284 | break; 285 | } 286 | msg->action = RESET_PASSWD; 287 | //将密保问题和密保答案发送到服务器修改 288 | n_write = write(sockfd, msg, msgbytes); 289 | if (n_write == -1) 290 | { 291 | printf("reset_passwd write ppid error!\n"); 292 | exit(1); 293 | } 294 | break; 295 | } 296 | 297 | /* 当命令为禁言时 */ 298 | case SLENCE: 299 | { 300 | clear_min(); 301 | printf("\033[5;15H*请输入您想要禁言的好友ID:"); 302 | scanf("%d", &msg->to_id); 303 | break; 304 | } 305 | 306 | /* 当命令为解禁时 */ 307 | case PERMITE: 308 | { 309 | clear_min(); 310 | printf("\033[5;15H*请输入您想要解禁的好友ID:"); 311 | scanf("%d", &msg->to_id); 312 | 313 | break; 314 | } 315 | 316 | /* 当命令为踢人时 */ 317 | case TICK: 318 | { 319 | clear_min(); 320 | printf("\033[5;15H*请输入您想要踢出的好友ID:"); 321 | scanf("%d", &msg->to_id); 322 | break; 323 | } 324 | 325 | /* 当命令为切换用户时 */ 326 | case BACK_LOGMENU: 327 | { 328 | n_write = write(sockfd, msg, msgbytes); 329 | if (n_write == -1) 330 | { 331 | printf("reset_passwd write ppid error!\n"); 332 | exit(1); 333 | } 334 | log_dis2(); 335 | break; 336 | } 337 | /* 当命令为设置个性签名时 */ 338 | case SETSIGNER: 339 | { 340 | clear_min(); 341 | 342 | printf("\033[6;14H *秀出你的个性* \n"); 343 | printf("\033[7;14H \n"); 344 | printf("\033[8;14H 你的个性签名由你做主 \n"); 345 | printf("\033[9;14H \n"); 346 | printf("\033[10;14H Please input your signer \n"); 347 | printf("\033[11;14H _____________________________ \n"); 348 | printf("\033[12;14H | | \n"); 349 | printf("\033[13;14H ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n"); 350 | printf("\033[14;14H \n"); 351 | 352 | while (1) 353 | { 354 | printf("\033[12;24H"); 355 | fgets(buffer, BUFFSIZE, stdin); 356 | for (i = 0; buffer[i] != '\n'; i++) 357 | { 358 | msg->signer[i]= buffer[i]; 359 | } 360 | if (strlen(buffer) == 1) 361 | { 362 | continue; 363 | } 364 | msg->signer[i] = '\0'; 365 | break; 366 | } 367 | break; 368 | } 369 | 370 | /* 当命令为注册会员时 */ 371 | case VIPMEMBER: 372 | { 373 | int randnum = 0; 374 | int fernum = 0; 375 | int momnum = 0; 376 | clear_min(); 377 | 378 | printf("\033[5;14H 欢迎订制PP_ONLINE会员 \n"); 379 | printf("\033[6;14H \n"); 380 | printf("\033[7;14H 聊天因你而精彩,资费15元/月 \n"); 381 | printf("\033[8;14H \n"); 382 | printf("\033[9;14H 请输入“根据系统提示”按月订制会员 \n"); 383 | printf("\033[10;14H 输入“12345”退出订制 \n"); 384 | printf("\033[11;14H 支付密码: \n"); 385 | printf("\033[12;14H _____________________________ \n"); 386 | printf("\033[13;14H | | \n"); 387 | printf("\033[14;14H ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n"); 388 | printf("\033[15;14H \n"); 389 | 390 | while (1) 391 | { 392 | printf("\033[11;14H 支付密码: \n"); 393 | printf("\033[13;24H \n"); 394 | printf("\033[11;31H5s\n"); 395 | sleep(1); 396 | printf("\033[11;31H4s\n"); 397 | sleep(1); 398 | printf("\033[11;31H3s\n"); 399 | sleep(1); 400 | printf("\033[11;31H2s\n"); 401 | sleep(1); 402 | printf("\033[11;31H1s\n"); 403 | sleep(1); 404 | randnum = rand_brith_id(); 405 | printf("\033[11;31H%d\n", randnum); 406 | printf("\033[13;24H"); 407 | scanf("%d", &fernum); 408 | //退出注册 409 | if (fernum == 12345) 410 | { 411 | printf("\033[15;14H *很遗憾,会员与您失之交臂!\n"); 412 | sleep(1); 413 | clear_min(); 414 | msg->action = VIPEXIT; 415 | break; 416 | } 417 | else 418 | { 419 | //支付密码输入正确 420 | if (randnum == fernum) 421 | { 422 | printf("\033[11;21H订制月数: \n"); 423 | printf("\033[13;24H \n"); 424 | printf("\033[13;24H"); 425 | scanf("%d", &momnum); 426 | if (momnum <= 0 || momnum >12) 427 | { 428 | printf("\033[15;14H *您所订制的月份超出限制\n"); 429 | momnum = 0; 430 | fernum = 0; 431 | sleep(1); 432 | printf("\033[15;14H \n"); 433 | continue; 434 | } 435 | else 436 | { 437 | printf("\033[15;14H *恭喜您已成为VIP、付款¥ %d\n", momnum * 15); 438 | msg->vip_flag = 1; 439 | sleep(1); 440 | break; 441 | } 442 | } 443 | else 444 | { 445 | printf("\033[15;14H *支付密码输入错误,请重试!\n"); 446 | sleep(1); 447 | printf("\033[15;14H \n"); 448 | continue; 449 | } 450 | } 451 | } 452 | break; 453 | } 454 | 455 | /* 当命令为发送表情时*/ 456 | case SENDBQ: 457 | { 458 | int num; 459 | clear_min(); 460 | 461 | printf("\033[5;14H PP_ONLINE表情包、聊得就是表情 \n"); 462 | printf("\033[6;14H \n"); 463 | printf("\033[7;14H \n"); 464 | printf("\033[8;14H \n"); 465 | printf("\033[9;14H \n"); 466 | printf("\033[10;14H \n"); 467 | printf("\033[11;14H 1、 (-_-!) 2、(~>_<~) 3、($_$) \n"); 468 | printf("\033[12;14H 4、 (T^T) 5、(+_+) 6、(-_-#) \n"); 469 | printf("\033[13;14H 7、 (~o~) 8、(n_n) 9、(ToT) \n"); 470 | printf("\033[14;14H 10、(*^_^*) 11、(*o*) 12、(^w^) \n"); 471 | 472 | printf("\033[7;14H 请输入私发PP号(输入12345可群发) \n"); 473 | printf("\033[8;14H 好友ID:"); 474 | scanf("%d", &msg->to_id); 475 | if (msg->to_id == 12345) 476 | { 477 | //群聊 478 | msg->action = CHATALL; 479 | } 480 | else 481 | { 482 | //私聊 483 | msg->action = CHATONE; 484 | } 485 | fflush(stdin); 486 | while(1) 487 | { 488 | printf("\033[9;14H 表情编号:"); 489 | scanf("%d",&num); 490 | if (num >= 1 && num <=12) 491 | { 492 | switch(num) 493 | { 494 | case 1: strcpy(msg->msg, "(-_-!)"); break; 495 | case 2: strcpy(msg->msg, "(->_<-)"); break; 496 | case 3: strcpy(msg->msg, "($_$)"); break; 497 | case 4: strcpy(msg->msg, "(T^T)"); break; 498 | case 5: strcpy(msg->msg, "(+_+)"); break; 499 | case 6: strcpy(msg->msg, "(-_-#)"); break; 500 | case 7: strcpy(msg->msg, "(~o~)"); break; 501 | case 8: strcpy(msg->msg, "(n_n)"); break; 502 | case 9: strcpy(msg->msg, "(ToT)"); break; 503 | case 10: strcpy(msg->msg, "(*^_^*)"); break; 504 | case 11: strcpy(msg->msg, "(*_*)"); break; 505 | case 12: strcpy(msg->msg, "(^w^)"); break; 506 | 507 | } 508 | msg->activity = 1; 509 | sleep(1); 510 | clear_min(); 511 | break; 512 | } 513 | else 514 | { 515 | printf("\033[15;14H *表情编号不再范围内,请重试 \n"); 516 | fflush(stdout); 517 | continue; 518 | } 519 | } 520 | break; 521 | } 522 | 523 | /* 当命令为发送短语时 */ 524 | case SENDDY: 525 | { 526 | int num; 527 | clear_min(); 528 | 529 | printf("\033[5;14H PP_ONLINE表情包、聊得就是表情 \n"); 530 | printf("\033[6;14H \n"); 531 | printf("\033[7;14H \n"); 532 | printf("\033[8;14H \n"); 533 | printf("\033[9;14H \n"); 534 | printf("\033[10;14H 1、外面侯着,老夫在上课 \n"); 535 | printf("\033[11;14H 2、我有四个兄弟,叫东北F4 \n"); 536 | printf("\033[12;14H 3、快点啊,等的我花都谢了 \n"); 537 | printf("\033[13;14H 4、您所呼叫的用户已牛逼,sorry... \n"); 538 | printf("\033[14;14H 5、哟,是哪个妖风把您给上线了 \n"); 539 | printf("\033[15;14H 6、看我小眼神行事,哈哈 \n"); 540 | 541 | printf("\033[7;14H 请输入私发PP号(输入12345可群发) \n"); 542 | printf("\033[8;14H 好友ID:"); 543 | scanf("%d", &msg->to_id); 544 | if (msg->to_id == 12345) 545 | { 546 | //群聊 547 | msg->action = CHATALL; 548 | } 549 | else 550 | { 551 | //私聊 552 | msg->action = CHATONE; 553 | } 554 | fflush(stdin); 555 | while(1) 556 | { 557 | printf("\033[9;14H 短语编号:"); 558 | scanf("%d",&num); 559 | if (num >= 1 && num <= 6) 560 | { 561 | 562 | switch(num) 563 | { 564 | case 1: strcpy(msg->msg, "外面侯着,老夫在上课"); break; 565 | case 2: strcpy(msg->msg, "我有四个兄弟,叫东北F4"); break; 566 | case 3: strcpy(msg->msg, "快点啊,等的我花都谢了"); break; 567 | case 4: strcpy(msg->msg, "您所呼叫的用户已牛逼,sorry..."); break; 568 | case 5: strcpy(msg->msg, "哟,是哪个妖风把您给上线了"); break; 569 | case 6: strcpy(msg->msg, "看我小眼神行事,哈哈"); break; 570 | } 571 | msg->activity = 2; 572 | sleep(1); 573 | clear_min(); 574 | break; 575 | } 576 | else 577 | { 578 | printf("\033[15;14H *短语编号不再范围内,请重试 \n"); 579 | fflush(stdout); 580 | continue; 581 | } 582 | } 583 | break; 584 | } 585 | 586 | /* 当命令为发送文件 */ 587 | case SENDFILE: 588 | { 589 | int i = 0; 590 | int sum = 0; 591 | int flag = 0; 592 | int count = 0; 593 | int from_fd; 594 | int bytes_read; 595 | 596 | char check[30] = {0}; 597 | char *ptr = check; 598 | //获取要传送的好友ID 599 | //获取想要让对方得到的文件名bb 600 | //打开本地文件aa 601 | //将文件中的内容写到结构体数组中 602 | //然后发送给服务器 603 | //服务器再把该内容转发给对方,对方收到信号之后,再本地创建一个bb文件并把文件内容写入 604 | clear_min(); 605 | 606 | printf("\033[5;14H *文件传输* \n"); 607 | printf("\033[6;14H \n"); 608 | printf("\033[7;14H 请输入接受文件对方ID: "); 609 | scanf("%d", &msg->to_id); 610 | while (1) 611 | { 612 | printf("\033[9;14H 请输入要传输的文件名: "); 613 | scanf("%s", msg->filename); 614 | if ((from_fd = open(msg->filename, O_RDONLY)) == -1) 615 | { 616 | printf("\033[11;14H *%s文件不存在!\n", msg->filename); 617 | sleep(1); 618 | flag = 1; 619 | clear_min(); 620 | break; 621 | } 622 | while (1) 623 | { 624 | printf("\033[11;14H 是否要重命名该文件(yes/no): "); 625 | scanf("%s", check); 626 | printf("\033[13;14H \n"); 627 | if (strcmp(check, "yes") == 0) 628 | { 629 | //循环获取直到用户输入正确的文件名 630 | while (1) 631 | { 632 | memset(check, 0, 30); 633 | ptr = check; 634 | printf("\033[13;14H 新的文件名(*.txt):"); 635 | scanf("%s", check); 636 | printf("\033[15;14H \n"); 637 | sum = 0; 638 | while (*ptr != '.') 639 | { 640 | sum++; 641 | if (sum == 29) 642 | { 643 | printf("\033[15;14H该文件不可用,请注意格式.txt\n"); 644 | break; 645 | } 646 | ptr++; 647 | } 648 | if (sum == 29) 649 | { 650 | sum = 0; 651 | continue; 652 | } 653 | if (strcmp(ptr, ".txt") == 0) 654 | { 655 | strcpy(msg->filename, check); 656 | break; 657 | } 658 | else 659 | { 660 | printf("\033[15;14H该文件不可用,请注意格式.txt\n"); 661 | } 662 | } 663 | break; 664 | } 665 | else if (strcmp(check, "no") == 0) 666 | { 667 | break; 668 | } 669 | else 670 | { 671 | printf("\033[13;14H 您输入的指令有误,请重试!\n"); 672 | } 673 | } 674 | break; 675 | } 676 | if (flag == 1) 677 | { 678 | break; 679 | } 680 | //从from_fd中读取直到遇到,到msg->filemsg中 681 | while ((bytes_read = read(from_fd, msg->filemsg, 1024))) 682 | { 683 | /* 一个致命的错误发生了 */ 684 | if ((bytes_read == -1) && (errno != EINTR)) 685 | { 686 | printf("\033[11;14H *文件读出异常中断!\n"); 687 | msg->action = SENDFILE_ERROR; 688 | break; 689 | } 690 | } 691 | while (msg->filemsg[i++] != '\0') 692 | { 693 | count++; 694 | } 695 | msg->count = count; 696 | printf("\033[15;14H *文件%s已经成功发送!\n", msg->filename); 697 | 698 | sleep(1); 699 | clear_min(); 700 | close(from_fd); 701 | 702 | break; 703 | } 704 | 705 | /* 当命令为查看聊天记录时 */ 706 | case DISJILU: 707 | { 708 | msg->action = DISJILU; 709 | break; 710 | } 711 | 712 | /* 当命令为点赞时 */ 713 | case DIANZAN: 714 | { 715 | clear_min(); 716 | 717 | printf("\033[6;14H *请输入你想点赞的好友ID:"); 718 | scanf("%d", &msg->to_id); 719 | break; 720 | } 721 | 722 | default: 723 | { 724 | break; 725 | } 726 | } 727 | return 0; 728 | } 729 | 730 | -------------------------------------------------------------------------------- /PP_client/dis_menu/src/dis_menu.c: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | - Copyright (C): 2016 3 | - File name : dis_menu.c 4 | - Author : - Zhao - 5 | - Date : 2016年01月25日 星期一 20时36分09秒 6 | - update Time : 2016年02月21日 星期日 12时58分58秒 7 | - Description : 界面程序 8 | - 版本 : 版本2 9 | * *******************************************************/ 10 | #include "../../../include/PP_ONLINE.h" 11 | 12 | #define SIZE 200000 13 | #define LOGSLEEP 150000 14 | #define LOGSLEEP2 7000 15 | 16 | void loading_dis() 17 | { 18 | system("clear"); 19 | printf("\033[12;50H*\n"); 20 | usleep(SIZE); 21 | printf("\033[12;52H用\n"); 22 | usleep(SIZE); 23 | printf("\033[12;54H心\n"); 24 | usleep(SIZE); 25 | printf("\033[12;56H去\n"); 26 | usleep(SIZE); 27 | printf("\033[12;58H做\n"); 28 | usleep(SIZE); 29 | printf("\033[12;60H! \n"); 30 | usleep(SIZE); 31 | printf("\033[12;50H \n"); 32 | usleep(SIZE); 33 | printf("\033[12;52H \n"); 34 | usleep(SIZE); 35 | printf("\033[12;54H \n"); 36 | usleep(SIZE); 37 | printf("\033[12;56H \n"); 38 | usleep(SIZE); 39 | printf("\033[12;58H \n"); 40 | usleep(SIZE); 41 | printf("\033[12;60H \n"); 42 | usleep(SIZE); 43 | printf("\033[14;62H代\n"); 44 | usleep(SIZE); 45 | printf("\033[14;64H码\n"); 46 | usleep(SIZE); 47 | printf("\033[14;66H因\n"); 48 | usleep(SIZE); 49 | printf("\033[14;68H你\n"); 50 | usleep(SIZE); 51 | printf("\033[14;70H而\n"); 52 | usleep(SIZE); 53 | printf("\033[14;72H精\n"); 54 | usleep(SIZE); 55 | printf("\033[14;74H彩\n"); 56 | usleep(SIZE); 57 | printf("\033[14;76H*\n"); 58 | usleep(SIZE); 59 | printf("\033[14;62H \n"); 60 | usleep(SIZE); 61 | printf("\033[14;64H \n"); 62 | usleep(SIZE); 63 | printf("\033[14;66H \n"); 64 | usleep(SIZE); 65 | printf("\033[14;68H \n"); 66 | usleep(SIZE); 67 | printf("\033[14;70H \n"); 68 | usleep(SIZE); 69 | printf("\033[14;72H \n"); 70 | usleep(SIZE); 71 | printf("\033[14;74H \n"); 72 | usleep(SIZE); 73 | printf("\033[14;76H \n"); 74 | usleep(SIZE); 75 | printf("\033[12;50H*用心去做!\n"); 76 | printf("\033[14;62H代码因你而精彩*\n"); 77 | printf("\033[16;50H____________________________\n"); 78 | printf("\033[17;49H|* |\n"); 79 | printf("\033[18;50H^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"); 80 | printf("\033[19;50H PP_ONLINE Linux网络聊天室\n"); 81 | printf("\033[20;65HLoading...%%8 \n"); 82 | sleep(1); 83 | printf("\033[17;53H***\n"); 84 | printf("\033[20;76H37\n"); 85 | sleep(1); 86 | printf("\033[17;59H******\n"); 87 | printf("\033[20;76H80\n"); 88 | sleep(1); 89 | printf("\033[17;71H***\n"); 90 | printf("\033[20;76H100\n"); 91 | sleep(1); 92 | } 93 | /* 登录界面 */ 94 | void log_dis() 95 | { 96 | system("clear"); 97 | printf("\033[6;42H __\n"); 98 | usleep(LOGSLEEP2); 99 | printf("\033[6;42H ____\n"); 100 | usleep(LOGSLEEP2); 101 | printf("\033[6;42H ______\n"); 102 | usleep(LOGSLEEP2); 103 | printf("\033[6;42H ________\n"); 104 | usleep(LOGSLEEP2); 105 | printf("\033[6;42H __________\n"); 106 | usleep(LOGSLEEP2); 107 | printf("\033[6;42H ____________\n"); 108 | usleep(LOGSLEEP2); 109 | printf("\033[6;42H ______________\n"); 110 | usleep(LOGSLEEP2); 111 | printf("\033[6;42H ________________\n"); 112 | usleep(LOGSLEEP2); 113 | printf("\033[6;42H __________________\n"); 114 | usleep(LOGSLEEP2); 115 | printf("\033[6;42H ____________________\n"); 116 | usleep(LOGSLEEP2); 117 | printf("\033[6;42H ______________________\n"); 118 | usleep(LOGSLEEP2); 119 | printf("\033[6;42H ________________________\n"); 120 | usleep(LOGSLEEP2); 121 | printf("\033[6;42H __________________________\n"); 122 | usleep(LOGSLEEP2); 123 | printf("\033[6;42H ____________________________\n"); 124 | usleep(LOGSLEEP2); 125 | printf("\033[6;42H ______________________________\n"); 126 | usleep(LOGSLEEP2); 127 | printf("\033[6;42H ________________________________\n"); 128 | usleep(LOGSLEEP2); 129 | printf("\033[6;42H __________________________________\n"); 130 | usleep(LOGSLEEP2); 131 | printf("\033[6;42H ____________________________________\n"); 132 | usleep(LOGSLEEP2); 133 | printf("\033[6;42H ______________________________________\n"); 134 | usleep(LOGSLEEP2); 135 | printf("\033[6;42H ________________________________________\n"); 136 | usleep(LOGSLEEP2); 137 | printf("\033[6;42H __________________________________________\n"); 138 | usleep(LOGSLEEP2); 139 | printf("\033[6;42H ____________________________________________\n"); 140 | usleep(LOGSLEEP2); 141 | printf("\033[6;42H ______________________________________________\n"); 142 | usleep(LOGSLEEP2); 143 | printf("\033[6;42H ________________________________________________\n"); 144 | usleep(LOGSLEEP2); 145 | printf("\033[6;42H __________________________________________________\n"); 146 | usleep(LOGSLEEP); 147 | printf("\033[7;42H ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n"); 148 | usleep(LOGSLEEP); 149 | printf("\033[7;42H|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|\n"); 150 | usleep(LOGSLEEP); 151 | printf("\033[8;42H| |\n"); 152 | usleep(LOGSLEEP); 153 | printf("\033[9;42H| |\n"); 154 | usleep(LOGSLEEP); 155 | printf("\033[10;42H| |\n"); 156 | usleep(LOGSLEEP); 157 | printf("\033[11;42H*__\n"); 158 | usleep(LOGSLEEP2); 159 | printf("\033[11;42H*____\n"); 160 | usleep(LOGSLEEP2); 161 | printf("\033[11;42H*______\n"); 162 | usleep(LOGSLEEP2); 163 | printf("\033[11;42H*________\n"); 164 | usleep(LOGSLEEP2); 165 | printf("\033[11;42H*__________\n"); 166 | usleep(LOGSLEEP2); 167 | printf("\033[11;42H*____________\n"); 168 | usleep(LOGSLEEP2); 169 | printf("\033[11;42H*______________\n"); 170 | usleep(LOGSLEEP2); 171 | printf("\033[11;42H*________________\n"); 172 | usleep(LOGSLEEP2); 173 | printf("\033[11;42H*__________________\n"); 174 | usleep(LOGSLEEP2); 175 | printf("\033[11;42H*____________________\n"); 176 | usleep(LOGSLEEP2); 177 | printf("\033[11;42H*______________________\n"); 178 | usleep(LOGSLEEP2); 179 | printf("\033[11;42H*________________________\n"); 180 | usleep(LOGSLEEP2); 181 | printf("\033[11;42H*__________________________\n"); 182 | usleep(LOGSLEEP2); 183 | printf("\033[11;42H*____________________________\n"); 184 | usleep(LOGSLEEP2); 185 | printf("\033[11;42H*______________________________\n"); 186 | usleep(LOGSLEEP2); 187 | printf("\033[11;42H*________________________________\n"); 188 | usleep(LOGSLEEP2); 189 | printf("\033[11;42H*__________________________________\n"); 190 | usleep(LOGSLEEP2); 191 | printf("\033[11;42H*____________________________________\n"); 192 | usleep(LOGSLEEP2); 193 | printf("\033[11;42H*______________________________________\n"); 194 | usleep(LOGSLEEP2); 195 | printf("\033[11;42H*________________________________________\n"); 196 | usleep(LOGSLEEP2); 197 | printf("\033[11;42H*__________________________________________\n"); 198 | usleep(LOGSLEEP2); 199 | printf("\033[11;42H*____________________________________________\n"); 200 | usleep(LOGSLEEP2); 201 | printf("\033[11;42H*______________________________________________\n"); 202 | usleep(LOGSLEEP2); 203 | printf("\033[11;42H*________________________________________________*\n"); 204 | usleep(LOGSLEEP); 205 | printf("\033[12;42H ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ \n"); 206 | usleep(LOGSLEEP); 207 | printf("\033[12;42H|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|\n"); 208 | usleep(LOGSLEEP); 209 | printf("\033[13;42H| |\n"); 210 | usleep(LOGSLEEP); 211 | printf("\033[14;42H| |\n"); 212 | usleep(LOGSLEEP); 213 | printf("\033[15;42H| |\n"); 214 | usleep(LOGSLEEP); 215 | printf("\033[16;42H| |\n"); 216 | usleep(LOGSLEEP); 217 | printf("\033[17;42H| |\n"); 218 | usleep(LOGSLEEP); 219 | printf("\033[18;42H| |\n"); 220 | usleep(LOGSLEEP); 221 | printf("\033[19;42H| |\n"); 222 | usleep(LOGSLEEP); 223 | printf("\033[20;42H| |\n"); 224 | usleep(LOGSLEEP); 225 | printf("\033[21;42H*————————————————————————————————————————————————*\n"); 226 | usleep(LOGSLEEP); 227 | printf("\033[13;42H| ________ |\n"); 228 | usleep(LOGSLEEP); 229 | printf("\033[14;42H| | | |\n"); 230 | usleep(LOGSLEEP); 231 | printf("\033[15;42H| | | |\n"); 232 | usleep(LOGSLEEP); 233 | printf("\033[16;42H| | | |\n"); 234 | usleep(LOGSLEEP); 235 | printf("\033[16;42H| |______| |\n"); 236 | usleep(LOGSLEEP); 237 | printf("\033[15;42H| |(^**^)| |\n"); 238 | usleep(LOGSLEEP); 239 | printf("\033[13;42H| ________ ___________________________ |\n"); 240 | usleep(LOGSLEEP); 241 | printf("\033[14;42H| | | | | |\n"); 242 | usleep(LOGSLEEP); 243 | printf("\033[15;42H| |(^**^)| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ |\n"); 244 | usleep(LOGSLEEP); 245 | printf("\033[16;42H| |______| ___________________________ |\n"); 246 | usleep(LOGSLEEP); 247 | printf("\033[17;42H| | | |\n"); 248 | usleep(LOGSLEEP); 249 | printf("\033[18;42H| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ |\n"); 250 | usleep(LOGSLEEP); 251 | printf("\033[9;42H| PP_ONLINE 聊天室 |\n"); 252 | usleep(LOGSLEEP); 253 | printf("\033[14;57HID号\n"); 254 | usleep(LOGSLEEP); 255 | printf("\033[17;57H密码\n"); 256 | usleep(LOGSLEEP); 257 | printf("\033[20;42H|*登录(LOG) |\n"); 258 | usleep(LOGSLEEP); 259 | printf("\033[20;42H|*登录(LOG) *退出(EXT) |\n"); 260 | usleep(LOGSLEEP); 261 | printf("\033[20;42H|*登录(LOG) *注册(REG) *退出(EXT) |\n"); 262 | usleep(LOGSLEEP); 263 | printf("\033[20;42H|*登录(LOG) *注册(REG) *忘密(REM) *退出(EXT) |\n"); 264 | } 265 | void log_dis2() 266 | { 267 | system("clear"); 268 | printf("\033[6;42H __________________________________________________\n"); 269 | printf("\033[7;42H|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|\n"); 270 | printf("\033[8;42H| |\n"); 271 | printf("\033[9;42H| PP_ONLINE 聊天室 |\n"); 272 | printf("\033[10;42H| |\n"); 273 | printf("\033[11;42H*________________________________________________*\n"); 274 | printf("\033[12;42H|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|\n"); 275 | printf("\033[13;42H| ________ ___________________________ |\n"); 276 | printf("\033[14;42H| | | ID号| | |\n"); 277 | printf("\033[15;42H| |(^**^)| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ |\n"); 278 | printf("\033[16;42H| |______| ___________________________ |\n"); 279 | printf("\033[17;42H| 密码| | |\n"); 280 | printf("\033[18;42H| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ |\n"); 281 | printf("\033[19;42H| |\n"); 282 | printf("\033[20;42H|*登录(LOG) *注册(REG) *忘密(REM) *退出(EXT) |\n"); 283 | printf("\033[21;42H*————————————————————————————————————————————————*\n"); 284 | } 285 | 286 | /* 帮助界面 */ 287 | void help_dis() 288 | { 289 | printf("\033[5;14H * 谢谢使用PP_ONLINE聊天室* \n"); 290 | usleep(SIZE); 291 | printf("\033[6;14H 写给用户的话 \n"); 292 | usleep(SIZE); 293 | printf("\033[7;14H 在这里: \n"); 294 | usleep(SIZE); 295 | printf("\033[8;14H 你可以和好友说悄悄话、你的空间由你做主\n"); 296 | usleep(SIZE); 297 | printf("\033[9;14H 你可以和大家群聊、爆料、吐嘈、扯八卦 \n"); 298 | usleep(SIZE); 299 | printf("\033[10;14H 你可以发送表情、短语、传输文件,轻松聊\n"); 300 | usleep(SIZE); 301 | printf("\033[11;14H 你还可以加入PP_ONLINE会员 \n"); 302 | usleep(SIZE); 303 | printf("\033[12;14H 独享禁言、解禁、踢人等隐藏功能 \n"); 304 | usleep(SIZE); 305 | printf("\033[13;14H 开发人员信息 \n"); 306 | usleep(SIZE); 307 | printf("\033[14;14H 赵溪楠 \n"); 308 | usleep(SIZE); 309 | printf("\033[15;14H PP_ONLINE V3.0 \n"); 310 | sleep(6); 311 | printf("\033[5;14H \n"); 312 | printf("\033[6;14H \n"); 313 | printf("\033[7;14H \n"); 314 | printf("\033[8;14H \n"); 315 | printf("\033[9;14H \n"); 316 | printf("\033[10;14H \n"); 317 | printf("\033[11;14H \n"); 318 | printf("\033[12;14H \n"); 319 | printf("\033[13;14H \n"); 320 | printf("\033[14;14H \n"); 321 | printf("\033[15;14H \n"); 322 | } 323 | /* 注册界面 */ 324 | void reg_dis() 325 | { 326 | system("clear"); 327 | printf("\033[5;38H __________________________________________________________\n"); 328 | usleep(LOGSLEEP); 329 | printf("\033[6;38H|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|\n"); 330 | usleep(LOGSLEEP); 331 | printf("\033[7;38H|* 欢迎注册PP_ONLINE *|\n"); 332 | usleep(LOGSLEEP); 333 | printf("\033[8;38H| |\n"); 334 | usleep(LOGSLEEP); 335 | printf("\033[9;38H| ____________________________ |\n"); 336 | usleep(LOGSLEEP); 337 | printf("\033[10;38H| 昵 称 : | | |\n"); 338 | usleep(LOGSLEEP); 339 | printf("\033[11;38H| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |\n"); 340 | usleep(LOGSLEEP); 341 | printf("\033[12;38H| ____________________________ |\n"); 342 | usleep(LOGSLEEP); 343 | printf("\033[13;38H| 密 码 : | | |\n"); 344 | usleep(LOGSLEEP); 345 | printf("\033[14;38H| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |\n"); 346 | usleep(LOGSLEEP); 347 | printf("\033[15;38H| ____________________________ |\n"); 348 | usleep(LOGSLEEP); 349 | printf("\033[16;38H| 再输密码: | | |\n"); 350 | usleep(LOGSLEEP); 351 | printf("\033[17;38H| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |\n"); 352 | usleep(LOGSLEEP); 353 | printf("\033[18;38H| ____________________________ |\n"); 354 | usleep(LOGSLEEP); 355 | printf("\033[19;38H| 密保问题: | | |\n"); 356 | usleep(LOGSLEEP); 357 | printf("\033[20;38H| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |\n"); 358 | usleep(LOGSLEEP); 359 | printf("\033[21;38H| ____________________________ |\n"); 360 | usleep(LOGSLEEP); 361 | printf("\033[22;38H| 密保答案: | | |\n"); 362 | usleep(LOGSLEEP); 363 | printf("\033[23;38H| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |\n"); 364 | usleep(LOGSLEEP); 365 | printf("\033[24;38H| |\n"); 366 | usleep(LOGSLEEP); 367 | printf("\033[25;38H| |\n"); 368 | usleep(LOGSLEEP); 369 | printf("\033[26;38H| |\n"); 370 | usleep(LOGSLEEP); 371 | printf("\033[27;38H*________________________________________________________*\n\n"); 372 | usleep(LOGSLEEP); 373 | } 374 | 375 | /* 修改/忘记密码界面 */ 376 | void rem_passwd() 377 | { 378 | system("clear"); 379 | printf("\033[7;45H ____________________________________________\n"); 380 | usleep(LOGSLEEP); 381 | printf("\033[8;45H|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^|\n"); 382 | usleep(LOGSLEEP); 383 | printf("\033[9;45H|^^^^^^^^ PP_ONLINE ^^^^^^^^|\n"); 384 | usleep(LOGSLEEP); 385 | printf("\033[10;45H| 安全服务中心 |\n"); 386 | usleep(LOGSLEEP); 387 | printf("\033[11;45H| |\n"); 388 | usleep(LOGSLEEP); 389 | printf("\033[12;45H| 请输入你的PP号 |\n"); 390 | usleep(LOGSLEEP); 391 | printf("\033[13;45H| _______________________________ |\n"); 392 | usleep(LOGSLEEP); 393 | printf("\033[14;45H| | | |\n"); 394 | usleep(LOGSLEEP); 395 | printf("\033[15;45H| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |\n"); 396 | usleep(LOGSLEEP); 397 | printf("\033[16;45H| 请回答密保问题 |\n"); 398 | usleep(LOGSLEEP); 399 | printf("\033[17;45H| |\n"); 400 | usleep(LOGSLEEP); 401 | printf("\033[18;45H| |\n"); 402 | usleep(LOGSLEEP); 403 | printf("\033[19;45H| _______________________________ |\n"); 404 | usleep(LOGSLEEP); 405 | printf("\033[20;45H| | | |\n"); 406 | usleep(LOGSLEEP); 407 | printf("\033[21;45H| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |\n"); 408 | usleep(LOGSLEEP); 409 | printf("\033[22;45H| |\n"); 410 | usleep(LOGSLEEP); 411 | printf("\033[23;45H| |\n"); 412 | usleep(LOGSLEEP); 413 | printf("\033[24;45H| 设置(REM) 返回(BCK) |\n"); 414 | usleep(LOGSLEEP); 415 | printf("\033[25;45H*__________________________________________*\n"); 416 | } 417 | /* 普通用户界面V2.0 做改变 */ 418 | void user_dis() 419 | { 420 | time_t timep; 421 | time(&timep); 422 | system("clear"); 423 | printf("\033[2;12H _____________________________________________________________________________________________________________\n"); 424 | printf("\033[3;12H| | | _______ |\n"); 425 | printf("\033[4;12H|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^| PP_NEWS…… | | | NAME: |\n"); 426 | printf("\033[5;12H| | | |(^_^)| |\n"); 427 | printf("\033[6;12H| | 今日天气 | |_____| PPID: 赞: |\n"); 428 | printf("\033[7;12H| | | |\n"); 429 | printf("\033[8;12H| | 南京:晴 | 个性签名: 这个人很懒,什么都没留下 |\n"); 430 | printf("\033[9;12H| | 10~24度(微风) *------------------*\n"); 431 | printf("\033[10;12H| | | |\n"); 432 | printf("\033[11;12H| | 适宜:出游、散步| |\n"); 433 | printf("\033[12;12H| | ________ | |\n"); 434 | printf("\033[13;12H| | /________\\ | |\n"); 435 | printf("\033[14;12H| | | | | |\n"); 436 | printf("\033[15;12H| | | 加 | | |\n"); 437 | printf("\033[16;12H|___________________________________________________| | 多 | | |\n"); 438 | printf("\033[17;12H* * | 宝 | | |\n"); 439 | printf("\033[18;12H| 隐藏功能:仅PP会员(超级用户)可见.VIP专用 | |________| | |\n"); 440 | printf("\033[19;12H*___________________________________________________* \\________/ *____________________________________*\n"); 441 | printf("\033[20;12H| | | |\n"); 442 | printf("\033[21;12H| *群聊(ACH) *私聊(PCH) *点赞(ZAN) | 金罐加多宝 |*刷出在线好友(DIS) *PP简介(HLP) |\n"); 443 | printf("\033[22;12H| | 畅饮人生…… | |\n"); 444 | printf("\033[23;12H| *发送表情(SBQ) *发送短语(SDY) | |*修改密码(REM) *切换帐号(BCK)|\n"); 445 | printf("\033[24;12H| | (^_^) | |\n"); 446 | printf("\033[25;12H| *文件传输(SFS) *查看聊天记录(SJL) | |*设置个性签名(SET) *注册会员(VIP)|\n"); 447 | printf("\033[26;12H*___________________________________________________*________________*____________________________________*\n\n"); 448 | printf("\033[3;23HTIME: %s\n",asctime(gmtime(&timep))); 449 | } 450 | /* 超级用户登录界面 */ 451 | void superuser_dis() 452 | { 453 | time_t timep; 454 | time(&timep); 455 | system("clear"); 456 | printf("\033[2;12H _____________________________________________________________________________________________________________\n"); 457 | printf("\033[3;12H| | | _______ |\n"); 458 | printf("\033[4;12H|^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^| PP_NEWS…… | | | NAME: |\n"); 459 | printf("\033[5;12H| | | |(^_^)| |\n"); 460 | printf("\033[6;12H| | 今日天气 | |_____| PPID: 赞: |\n"); 461 | printf("\033[7;12H| | | |\n"); 462 | printf("\033[8;12H| | 南京:晴 | 个性签名: 这个人很懒,什么都没留下 |\n"); 463 | printf("\033[9;12H| | 10~24度(微风) *------------------*\n"); 464 | printf("\033[10;12H| | | |\n"); 465 | printf("\033[11;12H| | 适宜:出游、散步| |\n"); 466 | printf("\033[12;12H| | ________ | |\n"); 467 | printf("\033[13;12H| | /________\\ | |\n"); 468 | printf("\033[14;12H| | | | | |\n"); 469 | printf("\033[15;12H| | | 加 | | |\n"); 470 | printf("\033[16;12H|___________________________________________________| | 多 | | |\n"); 471 | printf("\033[17;12H* * | 宝 | | |\n"); 472 | printf("\033[18;12H| *禁言(NOS) *解禁(CAS) *踢人(OUT) | |________| | |\n"); 473 | printf("\033[19;12H*___________________________________________________* \\________/ *____________________________________*\n"); 474 | printf("\033[20;12H| | | |\n"); 475 | printf("\033[21;12H| *群聊(ACH) *私聊(PCH) *点赞(ZAN) | 金罐加多宝 |*刷出在线好友(DIS) *帮助(HLP) |\n"); 476 | printf("\033[22;12H| | 畅饮人生…… | |\n"); 477 | printf("\033[23;12H| *发送表情(SBQ) *发送短语(SDY) | |*修改密码(REM) *切换帐号(BCK)|\n"); 478 | printf("\033[24;12H| | (^_^) | |\n"); 479 | printf("\033[25;12H| *文件传输(SFS) *查看聊天记录(SJL) | |*设置个性签名(SET) *注册会员(VIP)|\n"); 480 | printf("\033[26;12H*___________________________________________________*________________*____________________________________*\n\n"); 481 | printf("\033[3;23HTIME: %s\n",asctime(gmtime(&timep))); 482 | } 483 | /* 清屏小窗口*/ 484 | void clear_min() 485 | { 486 | printf("\033[5;14H \n"); 487 | printf("\033[6;14H \n"); 488 | printf("\033[7;14H \n"); 489 | printf("\033[8;14H \n"); 490 | printf("\033[9;14H \n"); 491 | printf("\033[10;14H \n"); 492 | printf("\033[11;14H \n"); 493 | printf("\033[12;14H \n"); 494 | printf("\033[13;14H \n"); 495 | printf("\033[14;14H \n"); 496 | printf("\033[15;14H \n"); 497 | } 498 | 499 | 500 | -------------------------------------------------------------------------------- /PP_server/server_thread/src/server_thread.c: -------------------------------------------------------------------------------- 1 | /********************************************************* 2 | - Copyright (C): 2016 3 | - File name : server_thread.c 4 | - Author : - Zhao - 5 | - Date : 2016年01月29日 星期五 17时22分05秒 6 | - Description : 服务器的线程的处理函数 7 | * *******************************************************/ 8 | #include "../../../include/PP_ONLINE.h" 9 | 10 | void server_read_msg(void *arg) 11 | { 12 | int ret; //用来接受返回值 13 | int from_fd; //聊天记录文件描述符 14 | 15 | int fd = *((int *)arg); 16 | 17 | struct _Message *msg = (struct _Message *)malloc(sizeof(struct _Message)); 18 | if (msg == NULL) 19 | { 20 | printf("server_msg_read msg malloc error!\n"); 21 | exit(1); 22 | } 23 | 24 | int n_read; 25 | int n_write; 26 | 27 | /* 打开数据库 */ 28 | if (SQLITE_OK != sqlite3_open("PP_user.db", &db)) 29 | { 30 | fprintf(stderr, "Cannot open database:%s\n", sqlite3_errmsg(db)); 31 | sqlite3_close(db); 32 | exit(1); 33 | } 34 | /* 打开聊天记录文件,如果不存在则创建它,且每次可以自动在后面添加 */ 35 | /*if ((from_fd = open("MsgEx.txt", O_RDWR|O_CREAT, S_IRUSR|S_IWUSR)) == -1) 36 | { 37 | fprintf(stderr, "Open %s Error:%s\n", "MsgEx.txt", strerror(errno)); 38 | exit(1); 39 | }*/ 40 | 41 | while (1) 42 | { 43 | memset(msg, 0, sizeof(struct _Message)); 44 | printf("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n"); 45 | n_read = read(fd, msg, sizeof(struct _Message)); 46 | 47 | 48 | /* 打开聊天记录文件,如果不存在则创建它,且每次可以自动在后面添加 */ 49 | if ((from_fd = open("MsgEx.txt", O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR)) == -1) 50 | { 51 | fprintf(stderr, "Open %s Error:%s\n", "MsgEx.txt", strerror(errno)); 52 | exit(1); 53 | } 54 | 55 | //如果客户端ctrl+c退出,则服务器将该用户踢下线 56 | if (msg->action == BACK_LOGMENU) 57 | { 58 | n_read = 0; 59 | } 60 | printf("n_read = %d\n",n_read); 61 | if (n_read == 0) 62 | { 63 | struct online* exit_fd1 = head; 64 | struct online* exit_fd2 = exit_fd1; 65 | /* 定位要删除的节点 */ 66 | while (exit_fd1 != NULL) 67 | { 68 | if (exit_fd1->online_fd == fd) 69 | { 70 | if (exit_fd1 == head) 71 | { 72 | head = exit_fd1->next; 73 | } 74 | else 75 | { 76 | exit_fd2->next = exit_fd1->next; 77 | } 78 | free(exit_fd1); 79 | exit_fd1 = NULL; 80 | break; 81 | 82 | } 83 | exit_fd2 = exit_fd1; 84 | exit_fd1 = exit_fd1->next; 85 | } 86 | 87 | close(fd); 88 | pthread_exit(NULL); 89 | } 90 | 91 | printf("server_read msg->action = %d\n", msg->action); 92 | switch(msg->action) 93 | { 94 | /* 如果读到登录命令 */ 95 | case LOGIN: 96 | { 97 | struct online *login = head; 98 | int n_write; 99 | //通过客户传过来的结构体信息 100 | //到数据库中寻找ID,检查输入密码是否正确 101 | printf("____________85_____________\n"); 102 | printf("msg->pp_id = %d\n", msg->pp_id); 103 | ret = check_passwd_inbase(db, msg->pp_id, msg->passwd); 104 | 105 | printf("ret = %d\n", ret); 106 | //ID号输入错误 107 | if (ret == USERIDERROR) 108 | { 109 | msg->flag = USERIDERROR; 110 | 111 | n_write = write(fd, msg, sizeof(struct _Message)); 112 | if (n_write == -1) 113 | { 114 | printf("The USERIDERROR write to client error!\n"); 115 | exit(1); 116 | } 117 | } 118 | //密码输入错误 119 | else if (ret == USERPASSERR) 120 | { 121 | msg->flag = USERPASSERR; 122 | 123 | n_write = write(fd, msg, sizeof(struct _Message)); 124 | if (n_write == -1) 125 | { 126 | printf("The USERPASSERR write to client error!\n"); 127 | exit(1); 128 | } 129 | 130 | } 131 | else if (ret == SUCCESS) 132 | { 133 | while (login != NULL && (login->online_id != msg->pp_id)) 134 | { 135 | login = login->next; 136 | } 137 | //如果登录的账户已经在线,则返回登录失败的信息,不能重复登录 138 | if (login != NULL && (login->online_id == msg->pp_id)) 139 | { 140 | msg->flag = LOGINFAIL; 141 | n_write = write(fd, msg, sizeof(struct _Message)); 142 | if (n_write == -1) 143 | { 144 | printf("The loginfail write error!\n"); 145 | exit(1); 146 | } 147 | break; 148 | } 149 | /* 150 | 否则登录成功,为新用户分配节点并且加入链表 151 | */ 152 | //根据ID查找出用户昵称 153 | find_logname_inbase(db, msg); 154 | /* 为新节点分配空间 */ 155 | new_user = (struct online *)malloc(sizeof(struct online)); 156 | if (new_user == NULL) 157 | { 158 | printf("new_user malloc error!\n"); 159 | exit(1); 160 | } 161 | memset(new_user, 0, sizeof(struct online)); 162 | //然后将新的在线用户结点插入链表 163 | new_user->online_flag = 1; 164 | new_user->online_fd = fd; 165 | new_user->online_id = msg->pp_id; 166 | strcpy(new_user->online_name, msg->name); 167 | new_user->next = head; 168 | head = new_user; 169 | 170 | n_write = write(fd, msg, sizeof(struct _Message)); 171 | printf("insert ask n_write = %d\n",n_write); 172 | if (n_write == -1) 173 | { 174 | printf("The insert_user write to client error!\n"); 175 | exit(1); 176 | } 177 | } 178 | break; 179 | } 180 | 181 | /* 如果读到注册命令 */ 182 | case REGIST: 183 | { 184 | while (1) 185 | { 186 | /* 使用随机数函数获得随即PP号 */ 187 | msg->pp_id = rand_brith_id(); 188 | printf("msg->pp_id rand is %d\n", msg->pp_id); 189 | //将id传入数据库查找是否有重复,-1代表有重复,0代表ID可用 190 | ret = find_id_inbase(db, msg->pp_id); 191 | printf("find_id_inbase ret = %d", ret); 192 | if (0 == ret) 193 | { 194 | /* 判断随即pp号无重复,则把用户信息写入数据库 */ 195 | insert_user_to_ppbase(db, msg); 196 | break; 197 | } 198 | } 199 | //服务器动作记录,表示返回信息为注册 200 | msg->flag = REGIST; 201 | /* 将结构体信息发回客户端 */ 202 | n_write = write(fd, msg, sizeof(struct _Message)); 203 | if (n_write == -1) 204 | { 205 | printf("the server REGIST write to client error!\n"); 206 | exit(1); 207 | } 208 | 209 | break; 210 | } 211 | 212 | /* 如果读到查看在线好友命令 */ 213 | case DIS_ONLINE_USER: 214 | { 215 | printf("--------------查看在线好友------------------\n"); 216 | struct online *dis = head; 217 | /* 遍历在线好友链表实现,将在线好友的ID和姓名依次写回客户端 */ 218 | while (dis != NULL) 219 | { 220 | msg->to_id = dis->online_id; 221 | strcpy(msg->to_name, dis->online_name); 222 | msg->flag = DIS_ONLINE_USER; 223 | 224 | n_write = write(fd, msg, sizeof(struct _Message)); 225 | if (n_write == -1) 226 | { 227 | printf("write dis_online_user error!\n"); 228 | exit(1); 229 | } 230 | //sleep(1); 231 | dis = dis->next; 232 | } 233 | msg->action = DIS_SUCCESS; 234 | n_write = write(fd, msg, sizeof(struct _Message)); 235 | if (n_write == -1) 236 | { 237 | printf("DIS_USER_SUCESS_WRITE error!\n"); 238 | exit(1); 239 | } 240 | 241 | break; 242 | } 243 | 244 | /* 如果读到私聊命令 */ 245 | case CHATONE: 246 | { 247 | int i; 248 | int j; 249 | int check = 0; 250 | int bytes_write; 251 | int bytes_read; 252 | 253 | char *ptr; 254 | char timew[30] = {0}; 255 | char chatbuffer[1024]; 256 | 257 | time_t timewrite; 258 | time(&timewrite); 259 | strcpy(timew, asctime(gmtime(&timewrite))); 260 | 261 | struct online *chartone = head; 262 | printf("****************CHATONE******************\n"); 263 | msg->flag = CHATONE; 264 | //找到发起聊天的用户的name 265 | while(chartone != NULL) 266 | { 267 | if (chartone->online_fd == fd) 268 | { 269 | memset(msg->name, 0, sizeof(msg->name)); 270 | strcpy(msg->name, chartone->online_name); 271 | if (chartone->online_id == msg->to_id) 272 | { 273 | msg->flag = CHATONETOSELF; 274 | check = 1; 275 | } 276 | break; 277 | } 278 | chartone = chartone->next; 279 | } 280 | if (check == 1) 281 | { 282 | n_write = write(fd, msg, sizeof(struct _Message)); 283 | if (n_write == -1) 284 | { 285 | printf("write chatonetoself error!\n"); 286 | exit(1); 287 | } 288 | break; 289 | } 290 | //如果私聊用户自己被禁言则 291 | if (chartone->online_flag == 0) 292 | { 293 | msg->flag = SLENCE; 294 | n_write = write(fd, msg, sizeof(struct _Message)); 295 | if (n_write == -1) 296 | { 297 | printf("write chartone self slence error!\n"); 298 | exit(1); 299 | } 300 | break; 301 | } 302 | 303 | chartone = head; 304 | //定位私聊用户 305 | while((chartone != NULL) && (msg->to_id != chartone->online_id)) 306 | { 307 | chartone = chartone->next; 308 | } 309 | //如果私聊用户不存在则返回信息给发起聊天的人 310 | if (chartone == NULL) 311 | { 312 | msg->flag = CHATONEFAIL; 313 | n_write = write(fd, msg, sizeof(struct _Message)); 314 | if (n_write == -1) 315 | { 316 | printf("write dis_online_user error!\n"); 317 | exit(1); 318 | } 319 | break; 320 | } 321 | //如果在在线链表中找到用户输入匹配ID,并且该用户没有被禁言 322 | if ((msg->to_id == chartone->online_id) && (chartone->online_flag == 1)) 323 | { 324 | //则把数据包转发给对方用户 325 | memset(msg->to_name, 0, sizeof(msg->to_name)); 326 | strcpy(msg->to_name, chartone->online_name); 327 | 328 | for (i = 0; i <= 10; i++) 329 | { 330 | ptr = chatbuffer; 331 | memset(chatbuffer, 0, 1024); 332 | switch(i) 333 | { 334 | case 0: 335 | { 336 | strcpy(chatbuffer, msg->name); 337 | break; 338 | } 339 | case 1: 340 | { 341 | strcpy(chatbuffer, "#"); 342 | break; 343 | } 344 | case 2: 345 | { 346 | strcpy(chatbuffer, "CHATONE"); 347 | break; 348 | } 349 | case 3: 350 | { 351 | strcpy(chatbuffer, "#"); 352 | break; 353 | } 354 | case 4: 355 | { 356 | strcpy(chatbuffer, msg->to_name); 357 | break; 358 | } 359 | case 5: 360 | { 361 | strcpy(chatbuffer, "#"); 362 | break; 363 | } 364 | case 6: 365 | { 366 | strcpy(chatbuffer, msg->msg); 367 | break; 368 | } 369 | case 7: 370 | { 371 | strcpy(chatbuffer, "#"); 372 | break; 373 | } 374 | case 8: 375 | { 376 | strcpy(chatbuffer, timew); 377 | for (j = 0; chatbuffer[j] != '\n'; j++) 378 | { 379 | ; 380 | } 381 | chatbuffer[j] = '\0'; 382 | break; 383 | } 384 | case 9: 385 | { 386 | strcpy(chatbuffer, "$"); 387 | break; 388 | } 389 | case 10: 390 | { 391 | strcpy(chatbuffer, "\n"); 392 | break; 393 | } 394 | } 395 | bytes_read = strlen(chatbuffer); 396 | while ((bytes_write = write(from_fd, chatbuffer, bytes_read))) 397 | { 398 | if ((bytes_write == -1) && (errno != EINTR)) 399 | { 400 | break; 401 | } 402 | else if (bytes_write == strlen(msg->name)) 403 | { 404 | //写完了所有字节,继续写下一个数据,数据之间已#分隔 405 | break; 406 | } 407 | else if (bytes_write > 0) 408 | { 409 | ptr += bytes_write; 410 | bytes_read -= bytes_write; 411 | } 412 | } 413 | } 414 | close(from_fd); 415 | 416 | n_write = write(chartone->online_fd, msg, sizeof(struct _Message)); 417 | if (n_write == -1) 418 | { 419 | printf("write dis_online_user error!\n"); 420 | exit(1); 421 | } 422 | break; 423 | } 424 | } 425 | 426 | /* 如果读到群聊命令 */ 427 | case CHATALL: 428 | { 429 | printf("---------------CHATALL---------------------\n"); 430 | int i; 431 | int j; 432 | int check = 0; 433 | int bytes_write; 434 | int bytes_read; 435 | 436 | char *ptr; 437 | char timew[30] = {0}; 438 | char chatbuffer[1024]; 439 | 440 | time_t timewrite; 441 | time(&timewrite); 442 | strcpy(timew, asctime(gmtime(&timewrite))); 443 | 444 | struct online *chartall = head; 445 | msg->flag = CHATALL; 446 | 447 | //遍历链表,先找到自己判断自己有没有被禁言,如果被禁言则跳出case 448 | while (chartall != NULL) 449 | { 450 | if (chartall->online_fd == fd) 451 | { 452 | //找到发起聊天的用户的name 453 | strcpy(msg->name, chartall->online_name); 454 | 455 | if (chartall->online_flag == 0) 456 | { 457 | msg->flag = SLENCE; 458 | n_write = write(fd, msg, sizeof(struct _Message)); 459 | if (n_write == -1) 460 | { 461 | printf("write slence error!\n"); 462 | exit(1); 463 | } 464 | check = 1; 465 | break; 466 | } 467 | } 468 | chartall = chartall->next; 469 | } 470 | if (check == 1) 471 | { 472 | break; 473 | } 474 | 475 | //循环将数据写入文件 476 | for (i = 0; i <= 10; i++) 477 | { 478 | ptr = chatbuffer; 479 | memset(chatbuffer, 0, 1024); 480 | switch(i) 481 | { 482 | case 0: 483 | { 484 | strcpy(chatbuffer, msg->name); 485 | break; 486 | } 487 | case 1: 488 | { 489 | strcpy(chatbuffer, "#"); 490 | break; 491 | } 492 | case 2: 493 | { 494 | strcpy(chatbuffer, "CHATALL"); 495 | break; 496 | } 497 | case 3: 498 | { 499 | strcpy(chatbuffer, "#"); 500 | break; 501 | } 502 | case 4: 503 | { 504 | strcpy(chatbuffer, "大家"); 505 | break; 506 | } 507 | case 5: 508 | { 509 | strcpy(chatbuffer, "#"); 510 | break; 511 | } 512 | case 6: 513 | { 514 | strcpy(chatbuffer, msg->msg); 515 | break; 516 | } 517 | case 7: 518 | { 519 | strcpy(chatbuffer, "#"); 520 | break; 521 | } 522 | case 8: 523 | { 524 | strcpy(chatbuffer, timew); 525 | for (j = 0; chatbuffer[j] != '\n'; j++) 526 | { 527 | ; 528 | } 529 | chatbuffer[j] = '\0'; 530 | break; 531 | } 532 | case 9: 533 | { 534 | strcpy(chatbuffer, "$"); 535 | break; 536 | } 537 | case 10: 538 | { 539 | strcpy(chatbuffer, "\n"); 540 | break; 541 | } 542 | } 543 | bytes_read = strlen(chatbuffer); 544 | while ((bytes_write = write(from_fd, chatbuffer, bytes_read))) 545 | { 546 | if ((bytes_write == -1) && (errno != EINTR)) 547 | { 548 | break; 549 | } 550 | else if (bytes_write == strlen(msg->name)) 551 | { 552 | //写完了所有字节,继续写下一个数据,数据之间已#分隔 553 | break; 554 | } 555 | else if (bytes_write > 0) 556 | { 557 | ptr += bytes_write; 558 | bytes_read -= bytes_write; 559 | } 560 | } 561 | } 562 | close(from_fd); 563 | 564 | chartall = head; 565 | while (chartall != NULL) 566 | { 567 | //如果是本人群发,则自己不发送 568 | if (chartall->online_fd == fd) 569 | { 570 | chartall = chartall->next; 571 | continue; 572 | } 573 | 574 | //其他在线用户全部发送! 575 | n_write = write(chartall->online_fd, msg, sizeof(struct _Message)); 576 | if (n_write == -1) 577 | { 578 | printf("write chartall error!\n"); 579 | exit(1); 580 | } 581 | 582 | chartall = chartall->next; 583 | } 584 | break; 585 | } 586 | 587 | /* 如果读到查找密保命令 */ 588 | case FIND_PASSPRO: 589 | { 590 | printf("__________________查找密保问题___________________\n"); 591 | //根据msg->PP_ID 到数据库中查找密保问题 592 | //然后将密保问题写回客户机 593 | msg->flag = FIND_PASSPRO; 594 | find_passpro_inbase(db, msg); 595 | n_write = write(fd, msg, sizeof(struct _Message)); 596 | if (n_write == -1) 597 | { 598 | printf("write passpro error!\n"); 599 | exit(1); 600 | } 601 | break; 602 | } 603 | 604 | /* 如果读到修改密码命令 */ 605 | case RESET_PASSWD: 606 | { 607 | struct online *resok1 = head; 608 | struct online *resok2 = resok1; 609 | 610 | printf("_________________修改密码___________________\n"); 611 | //在数据库中比对密保问题回答是否正确 612 | //如果正确则修改密码,并将成功修改信息写客户机 613 | //如果密保问题回答不正确则返回信息给客户机 614 | find_passkey_inbase(db, msg); 615 | n_write = write(fd, msg, sizeof(struct _Message)); 616 | if (n_write == -1) 617 | { 618 | printf("write passkey error!\n"); 619 | exit(1); 620 | } 621 | //如果该用户修改密码成功,则下线 622 | if (msg->flag == RESET_SUCCESS) 623 | { 624 | 625 | /* 定位要删除的节点 */ 626 | while ((resok1 != NULL) && (resok1->online_id != msg->pp_id)) 627 | { 628 | resok2 = resok1; 629 | resok1 = resok1->next; 630 | } 631 | //修改密码的用户不在线 632 | if (resok1 == NULL) 633 | { 634 | break; 635 | } 636 | 637 | //修改密码的用户在线则强制下线,踢出 638 | if (resok1->online_id == msg->pp_id) 639 | { 640 | if (resok1 == head) 641 | { 642 | head = resok1->next; 643 | } 644 | else 645 | { 646 | resok2->next = resok1->next; 647 | } 648 | free(resok1); 649 | resok1 = NULL; 650 | break; 651 | } 652 | } 653 | break; 654 | } 655 | 656 | /* 如果读到禁言命令 */ 657 | case SLENCE: 658 | { 659 | printf("------------禁言--------------\n"); 660 | struct online *slence = head; 661 | while (slence != NULL && slence->online_id != msg->to_id) 662 | { 663 | slence = slence->next; 664 | } 665 | if (slence == NULL) 666 | { 667 | //发送禁言用户不存在/已下线消息给VIP用户 668 | msg->flag = SLENCEFAIL; 669 | n_write = write(fd, msg, sizeof(struct _Message)); 670 | if (n_write == -1) 671 | { 672 | printf("write tickfail error!\n"); 673 | exit(1); 674 | } 675 | break; 676 | } 677 | //如果该用户已经被禁言则不能再次禁言 678 | if (slence->online_flag == 0) 679 | { 680 | msg->flag = SLENCEALREADY; 681 | n_write = write(fd, msg, sizeof(struct _Message)); 682 | if (n_write == -1) 683 | { 684 | printf("write fermite error!\n"); 685 | exit(1); 686 | } 687 | break; 688 | } 689 | //如果当前在线的用户ID与输入的ID相同,则禁言它 690 | if (slence->online_id == msg->to_id) 691 | { 692 | slence->online_flag = 0; 693 | //发送禁言消息给被禁言的用户 694 | msg->flag = SLENCE; 695 | n_write = write(slence->online_fd, msg, sizeof(struct _Message)); 696 | if (n_write == -1) 697 | { 698 | printf("write slence error!\n"); 699 | exit(1); 700 | } 701 | //发送禁言成功消息给VIP用户 702 | msg->flag = SLENCESUCCESS; 703 | n_write = write(fd, msg, sizeof(struct _Message)); 704 | if (n_write == -1) 705 | { 706 | printf("write slence error!\n"); 707 | exit(1); 708 | } 709 | } 710 | break; 711 | } 712 | 713 | /* 如果读到解禁命令 */ 714 | case PERMITE: 715 | { 716 | printf("----------------------解禁-------------------\n"); 717 | struct online *permite = head; 718 | 719 | while (permite != NULL && permite->online_id != msg->to_id) 720 | { 721 | permite = permite->next; 722 | } 723 | 724 | if (permite == NULL) 725 | { 726 | printf("asdfasdf\n"); 727 | //发送解禁不存在给消息给解禁VIP 728 | msg->flag = PERMITEFAIL; 729 | n_write = write(fd, msg, sizeof(struct _Message)); 730 | if (n_write == -1) 731 | { 732 | printf("write permintefail error!\n"); 733 | exit(1); 734 | } 735 | break; 736 | } 737 | //如果该用户已经解禁则不能再次解禁 738 | if (permite->online_flag == 1) 739 | { 740 | msg->flag = PERMITEALREADY; 741 | n_write = write(fd, msg, sizeof(struct _Message)); 742 | if (n_write == -1) 743 | { 744 | printf("write fermite error!\n"); 745 | exit(1); 746 | } 747 | break; 748 | } 749 | //如果当前在线的用户ID与输入的ID相同,则解禁它 750 | if (permite->online_id == msg->to_id) 751 | { 752 | //发送解禁消息给被解禁的用户 753 | msg->flag = PERMITE; 754 | permite->online_flag = 1; 755 | n_write = write(permite->online_fd, msg, sizeof(struct _Message)); 756 | if (n_write == -1) 757 | { 758 | printf("write fermite error!\n"); 759 | exit(1); 760 | } 761 | //发送解禁成功的消息给解禁VIP用户 762 | msg->flag = PERMITESUCCESS; 763 | n_write = write(fd, msg, sizeof(struct _Message)); 764 | if (n_write == -1) 765 | { 766 | printf("write fermite error!\n"); 767 | exit(1); 768 | } 769 | } 770 | break; 771 | } 772 | 773 | /* 如果读到踢人命令 */ 774 | case TICK: 775 | { 776 | printf("____________________踢人_____________________\n"); 777 | struct online *tick1 = head; 778 | struct online *tick2 = tick1; 779 | /* 定位要删除的节点 */ 780 | while ((tick1 != NULL) && (tick1->online_id != msg->to_id)) 781 | { 782 | tick2 = tick1; 783 | tick1 = tick1->next; 784 | } 785 | if (tick1 == NULL) 786 | { 787 | 788 | //发送踢人不存在给消息给踢人VIP 789 | msg->flag = TICKFAIL; 790 | n_write = write(fd, msg, sizeof(struct _Message)); 791 | if (n_write == -1) 792 | { 793 | printf("write tickfail error!\n"); 794 | exit(1); 795 | } 796 | break; 797 | } 798 | 799 | /* 如果找到要删除的节点 */ 800 | if (tick1->online_id == msg->to_id) 801 | { 802 | //发送踢人消息给被踢的用户 803 | msg->flag = TICKSUCCESS; 804 | n_write = write(tick1->online_fd, msg, sizeof(struct _Message)); 805 | if (n_write == -1) 806 | { 807 | printf("write ticksuccess error!\n"); 808 | exit(1); 809 | } 810 | //发送踢人成功的消息给踢人VIP 811 | msg->flag = TICKOK; 812 | n_write = write(fd, msg, sizeof(struct _Message)); 813 | if (n_write == -1) 814 | { 815 | printf("write ticksuccess error!\n"); 816 | exit(1); 817 | } 818 | if (tick1 == head) 819 | { 820 | head = tick1->next; 821 | } 822 | else 823 | { 824 | tick2->next = tick1->next; 825 | } 826 | free(tick1); 827 | tick1 = NULL; 828 | break; 829 | } 830 | } 831 | 832 | /* 如果读到注册会员 */ 833 | case VIPMEMBER: 834 | { 835 | printf("_________________注册会员___________________\n"); 836 | //首先,根据注册的fd找到ID号,和姓名 837 | struct online *vipmember = head; 838 | while(vipmember != NULL) 839 | { 840 | if (vipmember->online_fd == fd) 841 | { 842 | strcpy(msg->to_name, vipmember->online_name); 843 | msg->pp_id = vipmember->online_id; 844 | break; 845 | } 846 | vipmember = vipmember->next; 847 | } 848 | //通过ID号在数据库中将vip_flag 改为 1 849 | //设置msg->flag = VIPMENBER 850 | set_vipflag_inbase(db, msg); 851 | msg ->flag = VIPMEMBER; 852 | 853 | n_write = write(fd, msg, sizeof(struct _Message)); 854 | if (n_write == -1) 855 | { 856 | printf("write vipmember error!\n"); 857 | exit(1); 858 | } 859 | 860 | break; 861 | } 862 | 863 | /* 如果读到设置个性签名 */ 864 | case SETSIGNER: 865 | { 866 | printf("________________设置个性签名___________________\n"); 867 | //首先,根据注册的fd找到ID号,和姓名 868 | struct online *setsigner = head; 869 | while(setsigner != NULL) 870 | { 871 | if (setsigner->online_fd == fd) 872 | { 873 | msg->pp_id = setsigner->online_id; 874 | break; 875 | } 876 | setsigner = setsigner->next; 877 | } 878 | //通过ID号更新数据库中的个性签名 879 | //设置msg->flag为SETSIGNER 880 | update_signer_inbase(db, msg); 881 | msg->flag = SETSIGNER; 882 | 883 | n_write = write(fd, msg, sizeof(struct _Message)); 884 | if (n_write == -1) 885 | { 886 | printf("write vipmember error!\n"); 887 | exit(1); 888 | } 889 | break; 890 | } 891 | /* 如果读到点赞 */ 892 | case DIANZAN: 893 | { 894 | printf("________________点赞___________________\n"); 895 | //首先,根据注册的fd找到ID号,和姓名 896 | struct online *dianzan = head; 897 | while (dianzan != NULL) 898 | { 899 | if (dianzan->online_fd == fd) 900 | { 901 | strcpy(msg->name, dianzan->online_name); 902 | break; 903 | } 904 | dianzan = dianzan->next; 905 | } 906 | dianzan = head; 907 | 908 | while (dianzan != NULL && dianzan->online_id != msg->to_id) 909 | { 910 | dianzan = dianzan->next; 911 | } 912 | if (dianzan == NULL) 913 | { 914 | msg->flag = DIANZANFAIL; 915 | n_write = write(fd, msg, sizeof(struct _Message)); 916 | if (n_write == -1) 917 | { 918 | printf("write dianzan error!\n"); 919 | } 920 | break; 921 | } 922 | if (dianzan->online_id == msg->to_id) 923 | { 924 | update_zan_inbase(db, msg); 925 | msg->flag = DIANZAN; 926 | 927 | n_write = write(dianzan->online_fd, msg, sizeof(struct _Message)); 928 | if (n_write == -1) 929 | { 930 | printf("write dianzan error!\n"); 931 | break; 932 | } 933 | } 934 | break; 935 | } 936 | 937 | /* 如果读到文件传输 */ 938 | case SENDFILE: 939 | { 940 | printf("____________________文件传输___________________\n"); 941 | //如果读到文件传输,则先根据对方ID判断该用户是否在线 942 | //如果该用户在线的话,则将该msg转发给该用户 943 | struct online *sendfile = head; 944 | //根据fd获得发送方用户的姓名 945 | while ((sendfile != NULL) && (sendfile->online_fd != fd)) 946 | { 947 | sendfile = sendfile->next; 948 | } 949 | if (sendfile == NULL) 950 | { 951 | printf("sendfile server error!\n"); 952 | } 953 | if (sendfile->online_fd == fd) 954 | { 955 | strcpy(msg->name, sendfile->online_name); 956 | //如果是自己发个自己则不能发送 957 | if (sendfile->online_id == msg->to_id) 958 | { 959 | msg->flag = SENDFILETOSELF; 960 | 961 | n_write = write(fd, msg, sizeof(struct _Message)); 962 | if (n_write == -1) 963 | { 964 | printf("write to fd sendfileerror\n"); 965 | exit(1); 966 | } 967 | break; 968 | } 969 | } 970 | 971 | sendfile = head; 972 | //遍历在线用户链表找到接受方好友ID 973 | while ((sendfile != NULL) && (sendfile->online_id != msg->to_id)) 974 | { 975 | sendfile = sendfile->next; 976 | } 977 | //该用户不存在或者已经下线 978 | if (sendfile == NULL) 979 | { 980 | msg->flag = SENDFILE_ERROR; 981 | 982 | n_write = write(fd, msg, sizeof(struct _Message)); 983 | if (n_write == -1) 984 | { 985 | printf("write to fd sendfileerror\n"); 986 | exit(1); 987 | } 988 | break; 989 | } 990 | //该用户在线,则将数据包直接转发给该用户 991 | if (sendfile->online_id == msg->to_id) 992 | { 993 | msg->flag = SENDFILE; 994 | printf("%s\n", msg->filemsg); 995 | n_write = write(sendfile->online_fd, msg, sizeof(struct _Message)); 996 | if (n_write == -1) 997 | { 998 | printf("write to fd sendfileerror\n"); 999 | exit(1); 1000 | } 1001 | } 1002 | break; 1003 | } 1004 | ///////////////// 1005 | /* 查看聊天记录 */ 1006 | case DISJILU: 1007 | { 1008 | int i = 0; 1009 | int count = 0; 1010 | int to_fd; 1011 | int bytes_read; 1012 | 1013 | struct online *disjilu = head; 1014 | //找到该用户的姓名 1015 | while (disjilu != NULL) 1016 | { 1017 | if (fd == disjilu->online_fd) 1018 | { 1019 | strcpy(msg->name, disjilu->online_name); 1020 | break; 1021 | } 1022 | disjilu = disjilu->next; 1023 | } 1024 | 1025 | if ((to_fd = open("MsgEx.txt", O_RDONLY)) == -1) 1026 | { 1027 | fprintf(stderr, "Open %s Error:%s\n", "MsgEx.txt", strerror(errno)); 1028 | exit(1); 1029 | } 1030 | 1031 | while ((bytes_read = read(to_fd, msg->filemsg, 2048))) 1032 | { 1033 | if ((bytes_read == -1) && (errno != EINTR)) 1034 | { 1035 | printf("查看聊天记录异常!\n"); 1036 | break; 1037 | } 1038 | } 1039 | while (msg->filemsg[i++] != '\0') 1040 | { 1041 | count++; 1042 | } 1043 | msg->count = count; 1044 | close(to_fd); 1045 | msg->flag = DISJILU; 1046 | 1047 | n_write = write(fd, msg, sizeof(struct _Message)); 1048 | if (n_write == -1) 1049 | { 1050 | printf("write disjilu error!\n"); 1051 | exit(1); 1052 | } 1053 | 1054 | break; 1055 | } 1056 | default: 1057 | { 1058 | break; 1059 | } 1060 | } 1061 | } 1062 | } 1063 | --------------------------------------------------------------------------------