├── Makefile ├── README.md ├── backup ├── client.c ├── common.c ├── common.h └── service.c ├── client ├── Client ├── Makefile ├── account_per.c ├── account_per.o ├── account_ser.c ├── account_ser.o ├── account_u.c ├── account_u.o ├── chat.c ├── chat.o ├── client.c ├── client.h ├── client.h.gch ├── client.o ├── common │ ├── common.c │ ├── common.h │ ├── common.o │ ├── struct.h │ └── tags ├── file.c ├── file.o ├── tags ├── temp ├── temp1 ├── temp1.c └── test.c ├── common ├── common.c ├── common.h ├── shan.c ├── shiyi.c ├── struct.h └── tags ├── gtk ├── Makefile ├── study ├── study.c └── study.o └── service ├── EntityKey.c ├── EntityKey.o ├── Makefile ├── Service ├── account.c ├── account.o ├── chat.c ├── chat.o ├── common ├── common.c ├── common.h ├── common.o └── struct.h ├── file ├── list.h ├── service.c ├── service.h ├── service.o └── tags /Makefile: -------------------------------------------------------------------------------- 1 | #!Makefile 2 | 3 | PROGRAM = Service 4 | 5 | C_SOURCES = $(shell find . -name "*.c") 6 | C_OBJECTS = $(patsubst %.c, %.o, $(C_SOURCES)) 7 | 8 | CC = gcc 9 | 10 | C_FLAGS = -DDEBUG -c -Wall -Wextra -ggdb -I. 11 | 12 | all: $(C_OBJECTS) 13 | @echo compile... 14 | $(CC) $(C_OBJECTS) -o $(PROGRAM) 15 | 16 | .c.o: 17 | @echo link$< ... 18 | $(CC) $(C_FLAGS) $< -o $@ 19 | 20 | .PHONY:clean 21 | clean: 22 | $(RM) $(C_OBJECTS) Service 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 聊天室 2 | 3 | 实现Linux环境下的简单聊天室,基于c/s (客户端client/服务端server)模型,实现不同客户端之间通信。 4 | 5 | | Author | Sequin小红九| 6 | | :----------: | -----------| 7 | | Email | SequinYF@xiyoulinux.org | 8 | |[![csdn-logo]][csdn]|http://blog.csdn.net/sequin_yf| 9 | 10 | 11 | ## 开发环境 12 | 13 | Linux开发环境,所有的API基于LinuxC(所有操作尽量使用LinuxC系统调用) 14 | 15 | | 编译器 | gcc| 16 | | ---------- | -----------| 17 | | 编辑器 | Vim | 18 | 19 | ## 安装说明 20 | 21 | ``` 22 | $ git clone https://github.com/SequinYF/CHATROOM_C 23 | $ cd CHATROOM_C 24 | $ make 25 | ``` 26 | 27 | ## 实现功能 28 | 29 | ### 基础功能 30 | 1. 登录、注册 31 | 2. 好友管理(查看、添加、删除) 32 | 3. 聊天管理(私聊、群聊) 33 | 4. 聊天记录功能,日志功能(操作日志、错误日志)。 34 | 5. 消息提醒(好友上线、消息) 35 | 36 | ### 拓展技术 37 | - 使用多路复用技术(select) 38 | - 文件的传输、离线消息 39 | - 数据加密(密码、消息) 40 | -------------------------------------------------------------------------------- /backup/client.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : Catherine199787@outlook.com 4 | * Last modified : 2016-08-03 15:24 5 | * Filename : client.c 6 | * Description : client 7 | * *****************************************************************************/ 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include"./common/my_recv.h" 18 | #include 19 | 20 | #define INVALID_USERINFO 'n' 21 | #define VALID_USERINFO 'y' 22 | #define BUFSIZE 1024 23 | 24 | //获取用户输入存入到buf 25 | int get_userinfo(char *buf, int len){ 26 | int i; 27 | int c; 28 | 29 | if(buf == NULL){ 30 | return -1; 31 | } 32 | 33 | i = 0; 34 | while(((c = getchar()) != '\n') && (c != EOF) && (i < len -2)){ 35 | buf[i++] = c; 36 | } 37 | buf[i++] = '\n'; 38 | buf[i++] = '\0'; 39 | 40 | return 0; 41 | } 42 | 43 | //输入用户名 44 | void input_userinfo(int conn_fd, const char *string){ 45 | char input_buf[32]; 46 | char recv_buf[BUFSIZE]; 47 | int flag_userinfo; 48 | 49 | // 50 | do{ 51 | printf("%s:",string); 52 | if (get_userinfo(input_buf, 32) < 0){ 53 | printf("error return from get_userinfo\n"); 54 | exit(1); 55 | } 56 | 57 | if(send(conn_fd, input_buf, strlen(input_buf), 0 ) < 0){ 58 | my_err("send",__LINE__); 59 | } 60 | 61 | //从连接套接字上读取一次数据 62 | if(my_recv(conn_fd, recv_buf,sizeof(recv_buf)) < 0){ 63 | printf("data is too long\n"); 64 | exit(1); 65 | } 66 | 67 | if(recv_buf[0] == VALID_USERINFO){ 68 | flag_userinfo = VALID_USERINFO; 69 | } 70 | else{ 71 | printf("%s error, input again,",string); 72 | flag_userinfo = INVALID_USERINFO; 73 | } 74 | }while(flag_userinfo == INVALID_USERINFO); 75 | } 76 | 77 | int main(int argc, char *argv[]){ 78 | int i; 79 | int ret; 80 | int conn_fd; 81 | int serv_port; 82 | struct sockaddr_in serv_addr; 83 | char recv_buf[BUFSIZE]; 84 | 85 | //检查参数个数 86 | if(argc != 5){ 87 | printf("Usage:[-p] [serv_port] [-a] [serv_address]\n"); 88 | exit(1); 89 | } 90 | 91 | //初始化服务器地址结构 92 | memset(&serv_addr, 0, sizeof(struct sockaddr_in)); 93 | serv_addr.sin_family = AF_INET; 94 | 95 | //从命令行获取服务器的端口与地址 96 | for( i = 0; i < argc; i++){ 97 | if(strcmp("-p", argv[i]) == 0){ 98 | serv_port = atoi(argv[i+1]); 99 | if(serv_port < 0 || serv_port > 65535){ 100 | printf("invalid serv_addr.sin_port\n"); 101 | exit(1); 102 | } 103 | else{ 104 | serv_addr.sin_port = htons(serv_port); 105 | } 106 | continue; 107 | } 108 | 109 | if(strcmp("-a", argv[i]) == 0){ 110 | if(inet_aton(argv[i+1], &serv_addr.sin_addr) == 0){ 111 | printf("invalid server ip address\n"); 112 | exit(1); 113 | } 114 | continue; 115 | } 116 | } 117 | 118 | //检测是否少输入了某项参数 119 | if(serv_addr.sin_port == 0 || serv_addr.sin_addr.s_addr == 0){ 120 | printf("Usage:[-p] [serv_add.sinz_port] [-a] [serv_address]\n"); 121 | exit(1); 122 | } 123 | 124 | //创建一个TCP套接字 125 | conn_fd = socket(AF_INET, SOCK_STREAM, 0); 126 | if(conn_fd < 0){ 127 | my_err("socket", __LINE__); 128 | } 129 | 130 | //向服务器发送连接请求 131 | if(connect(conn_fd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) < 0){ 132 | my_err("connect", __LINE__); 133 | } 134 | 135 | //输入用户名密码 136 | input_userinfo(conn_fd, "username"); 137 | input_userinfo(conn_fd, "password"); 138 | //读取还用信息并打印 139 | if((ret = my_recv(conn_fd, recv_buf, sizeof(recv_buf))) < 0){ 140 | printf("data is too long\n"); 141 | exit(1); 142 | } 143 | for(i = 0; i < ret; i++){ 144 | printf("%c", recv_buf[i]); 145 | } 146 | printf("\n"); 147 | 148 | close(conn_fd); 149 | 150 | return 0; 151 | } 152 | -------------------------------------------------------------------------------- /backup/common.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-05 08:49 5 | * Filename : common.c 6 | * Description : 7 | * *****************************************************************************/ 8 | #include "struct.h" 9 | #include"common.h" 10 | 11 | #define INVALID_INPUT 'n' 12 | #define VALID_INPUT 'y' 13 | 14 | char l_getc(){ 15 | char ch; 16 | scanf("%c",&ch); 17 | while(ch == '\n'){ 18 | scanf("%c",&ch); 19 | } 20 | return ch; 21 | } 22 | 23 | //Linux没有getch() 24 | 25 | int getch(void){ 26 | int c= 0; 27 | struct termios org_opts, new_opts; 28 | int res = 0; 29 | 30 | //将之前的设置备份 31 | res = tcgetattr(STDIN_FILENO, &org_opts); 32 | assert(res == 0); 33 | 34 | //设置新的设定 35 | memcpy(&new_opts, &org_opts, sizeof(new_opts)); 36 | new_opts.c_lflag &= ~(ICANON |ECHO | ECHOE | ECHOK |ECHONL | ECHOKE | ICRNL); 37 | tcsetattr(STDIN_FILENO, TCSANOW, &new_opts); 38 | c= getchar(); 39 | 40 | //恢复之前的设定 41 | res = tcsetattr(STDIN_FILENO, TCSANOW, &org_opts); 42 | assert(res == 0); 43 | return c; 44 | } 45 | 46 | //密码输入 47 | 48 | void getpwd(int maxlen, char *pwd){ 49 | int j= 0; 50 | char c; 51 | 52 | while((j < maxlen-2) && (c = getch()) != '\n'){ 53 | if(c != 127){ 54 | printf("*"); 55 | pwd[j++] = c; 56 | } 57 | else { 58 | if(j > 0){ 59 | j = j - 1; 60 | printf("\b \b"); 61 | } 62 | } 63 | } 64 | pwd[j++] = '\n'; 65 | pwd[j++] = '\0'; 66 | 67 | puts(" "); 68 | } 69 | 70 | void cl_stdin(){ 71 | char ch; 72 | while(( ch = getchar()) != '\n' && ch != EOF); 73 | } 74 | 75 | //合法输入 76 | int input_check(int input_len, char * input_buf){ 77 | char c; 78 | int i = 0; 79 | if(input_buf == NULL){ 80 | my_err("input check", __LINE__); 81 | } 82 | 83 | while((c = getchar()) != '\n' && (c != EOF) && (i < input_len-2)){ 84 | input_buf[i++] = c; 85 | } 86 | 87 | input_buf[i++] = '\0'; 88 | input_buf[i++] = '\n'; 89 | 90 | return i; 91 | } 92 | 93 | 94 | 95 | //自定义错误函数 96 | void my_err(const char* err_string, int line){ 97 | fprintf(stderr, "line:%d \n", line); 98 | perror(err_string); 99 | exit(1); 100 | } 101 | 102 | //从套接字上读取一次数据(以'\n'为结束标志) 103 | 104 | int my_recv(int conn_fd, char *data_buf, int len){ 105 | static char recv_buf[BUFSIZE]; 106 | static char *pread; 107 | static int len_remain = 0; 108 | int i; 109 | 110 | memset(recv_buf, 0, BUFSIZE); 111 | //如果自定义缓冲区中没有数据,则从套接字读取数据 112 | if(len_remain <= 0){ 113 | len_remain = recv(conn_fd, recv_buf,sizeof(recv_buf), 0); 114 | if(len_remain < 0){ 115 | my_err("recv", __LINE__); 116 | } 117 | else if(len_remain == 0){ 118 | return 0; 119 | } 120 | pread = recv_buf; //重新初始化pread指针 121 | } 122 | 123 | //从定义缓冲区中读取一次数据 124 | for(i = 0; *pread != '\n'; i++){ 125 | if(i > len){ 126 | return -1; 127 | } 128 | data_buf[i] = *pread++; 129 | len_remain--; 130 | } 131 | 132 | data_buf[i] = '\0'; 133 | //去除结束标志 134 | len_remain--; 135 | pread++; 136 | 137 | return i; 138 | } 139 | 140 | 141 | //文件写入函数 142 | 143 | int file_write(int fd, const void * buf, int len){ 144 | int we; 145 | 146 | we = write(fd , buf, len); 147 | if(we != len){ 148 | we = 0; 149 | my_err("c_write", __LINE__); 150 | } 151 | 152 | return we; 153 | } 154 | 155 | //获取文件长度 156 | int file_len(int fd){ 157 | int length; 158 | if(lseek(fd ,0 ,SEEK_END)== -1){ 159 | my_err("lseek set", __LINE__); 160 | } 161 | if((length = lseek(fd , 0 , SEEK_CUR)) == -1 ){ 162 | my_err("length" , __LINE__); 163 | } 164 | if(lseek(fd , 0 ,SEEK_SET)== -1){ 165 | my_err("lseek end" , __LINE__); 166 | } 167 | return length; 168 | } 169 | 170 | 171 | void * file_read(int fd,int len,void* buf){ 172 | int re; 173 | memset(buf, 0, sizeof(buf)); 174 | if((re = read(fd , buf , len)) == -1){ 175 | my_err("c_read", __LINE__); 176 | } 177 | return buf; 178 | } 179 | 180 | //显示接收信息 181 | 182 | void show_recv(int conn_fd, char *recv_buf, int buflen ){ 183 | int i; 184 | int ret; 185 | 186 | ret = my_recv(conn_fd, recv_buf, buflen); 187 | if(ret < 0){ 188 | my_err("recv faild", __LINE__); 189 | } 190 | 191 | for(i = 0; i < ret; i++){ 192 | printf("%c", recv_buf[i]); 193 | } 194 | } 195 | 196 | //输入并传送 197 | 198 | void input_(int conn_fd, const char *string, int len){ 199 | char input_buf[len]; 200 | int flag; 201 | char recv_buf[BUFSIZE] = {'\0'}; 202 | do{ 203 | 204 | printf("%s:",string); 205 | 206 | if(strcmp(string, "password") == 0){ 207 | getpwd(32,input_buf); 208 | } 209 | else if( input_check(len, input_buf) == 0){ 210 | my_err("inpput check", __LINE__); 211 | } 212 | 213 | if(send(conn_fd, input_buf, strlen(input_buf), 0) < 0){ 214 | my_err("send", __LINE__); 215 | } 216 | 217 | if(my_recv(conn_fd, recv_buf,sizeof(recv_buf)) < 0){ 218 | my_err("data is too long", __LINE__); 219 | } 220 | 221 | if(recv_buf[0] == VALID_INPUT){ 222 | flag = VALID_INPUT; 223 | } 224 | else{ 225 | printf("%s error, input again,\n",string); 226 | flag = INVALID_INPUT; 227 | } 228 | }while(flag == INVALID_INPUT); 229 | } 230 | 231 | 232 | //传送 233 | /* 234 | void __input(int conn_fd, char *input_buf){ 235 | int flag; 236 | char recv_buf[BUFSIZE] = {'\0'}; 237 | do{ 238 | 239 | if(send(conn_fd, input_buf, strlen(input_buf), 0) < 0){ 240 | my_err("send", __LINE__); 241 | } 242 | 243 | if(my_recv(conn_fd, recv_buf,sizeof(recv_buf)) < 0){ 244 | my_err("data is too long", __LINE__); 245 | } 246 | 247 | if(recv_buf[0] == VALID_INPUT){ 248 | flag = VALID_INPUT; 249 | } 250 | else{ 251 | printf("error, input again,\n"); 252 | flag = INVALID_INPUT; 253 | input_check(32, input_buf); 254 | } 255 | }while(flag == INVALID_INPUT); 256 | } 257 | 258 | */ 259 | 260 | //获取系统当前日期 261 | user_date_t DateNow() { 262 | user_date_t curDate; 263 | time_t now; //实例化time_t结构 264 | struct tm *timeNow; //实例化tm结构指针 265 | time(&now); 266 | timeNow = localtime(&now); 267 | curDate.year=timeNow->tm_year+1900; 268 | curDate.month=timeNow->tm_mon+1; 269 | curDate.day=timeNow->tm_mday; 270 | 271 | return curDate; 272 | } 273 | 274 | //获取系统当前时间 275 | user_time_t TimeNow(){ 276 | user_time_t curTime; 277 | time_t now; //实例化time_t结构 278 | struct tm *timeNow; //实例化tm结构指针 279 | time(&now); 280 | timeNow = localtime(&now); 281 | curTime.hour=timeNow->tm_hour; 282 | curTime.minute=timeNow->tm_min; 283 | curTime.second=timeNow->tm_sec; 284 | 285 | return curTime; 286 | } 287 | 288 | int ser_atoi(char * buf){ //atoi 289 | 290 | int ret; 291 | ret = atoi(buf); 292 | 293 | return ret; 294 | } 295 | 296 | //发送数据 297 | int send_data(int conn_fd,send_t * psend){ 298 | if(send(conn_fd, psend, sizeof(send_t), 0) < 0){ 299 | my_err("send",__LINE__); 300 | } 301 | return 0; 302 | } 303 | 304 | 305 | 306 | //将字符串str就地转换为大写字符串,并返回字符串头指针 307 | char *Str2Upper(char *str) { 308 | if (NULL == str) 309 | return NULL; 310 | else { 311 | char *p = str; 312 | while ('\0' != *p) { 313 | if (*p >= 'a' && *p <= 'z') 314 | *p -= 32; 315 | p++; 316 | } 317 | return str; 318 | } 319 | } 320 | 321 | //显示消息 322 | void show_message(int conn_fd){ 323 | message_t leave; 324 | message_t * pmess = &leave; 325 | 326 | recv(conn_fd, pmess, sizeof(leave), 0); 327 | printf("%d-%d-%d %2d:%2d:%2d\n",pmess->date.year,pmess->date.month,pmess->date.day,pmess->time.hour,pmess->time.minute,pmess->time.second); 328 | printf("<%s>: ", pmess->send_name); 329 | puts(pmess->message_buf); 330 | puts(" "); 331 | } 332 | 333 | void recv_message(int conn_fd, message_t * data){ 334 | 335 | if(recv(conn_fd, data, sizeof(message_t), 0) < 0){ 336 | send_data(conn_fd,(char *)("n")); 337 | return; 338 | } 339 | else{ 340 | send_data(conn_fd, (char*)("y")); 341 | } 342 | 343 | } 344 | 345 | 346 | void send_message(int conn_fd, char * send_name){ 347 | 348 | static char send_buf[BUFSIZE-36]; 349 | static char recv_buf[BUFSIZE]; 350 | int flag = INVALID_INPUT; 351 | 352 | message_t info; 353 | message_t *data = &info; 354 | 355 | data->date = DateNow(); 356 | data->time = TimeNow(); 357 | strcpy(data->send_name, send_name); 358 | 359 | input_check(BUFSIZE-36, send_buf); 360 | strcpy(data->message_buf, send_buf); 361 | do{ 362 | 363 | if(send(conn_fd, data, sizeof(info), 0) < 0){ 364 | my_err("send", __LINE__); 365 | } 366 | 367 | if(my_recv(conn_fd, recv_buf,sizeof(recv_buf)) < 0){ 368 | my_err("data is too long", __LINE__); 369 | } 370 | 371 | if(recv_buf[0] == VALID_INPUT){ 372 | flag = VALID_INPUT; 373 | } 374 | else{ 375 | printf("error, input again,\n"); 376 | flag = INVALID_INPUT; 377 | input_check(BUFSIZE-36, send_buf); 378 | strcpy(data->message_buf, send_buf); 379 | } 380 | }while(flag == INVALID_INPUT); 381 | 382 | } 383 | 384 | -------------------------------------------------------------------------------- /backup/common.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-05 09:10 5 | * Filename : common.h 6 | * Description : 7 | * *****************************************************************************/ 8 | #ifndef COMMON_H_ 9 | #define COMMON_H_ 10 | 11 | #include"struct.h" 12 | #include 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 | 26 | int file_write(int fd, const void * data, int len); //写入文件 27 | 28 | void * file_read(int fd, int len, void * data); //读出文件 29 | 30 | int file_len(int fd);// 获得文件长度 31 | 32 | void my_err(const char * err_string, int line); 33 | 34 | int my_recv(int conn_fd, char *data_buf, int len); 35 | 36 | char l_getc(); //从键盘获取一个字符,避免回车 37 | 38 | void cl_stdin(); //清空输入缓冲 39 | 40 | void getpwd(int maxlen, char *pwd); 41 | 42 | void __input(int conn_fd, char * input_buf); //传送 43 | 44 | int input_check(int input_len, char *input_buf); //输入合法性检查 45 | 46 | void show_recv(int conn_fd, char * recv_buf, int buflen); //显示接收消息 47 | 48 | void input_(int conn_fd, const char *string, int len);// 输入并且传送 49 | 50 | user_date_t DateNow(); //获取当前日期 51 | 52 | user_time_t TimeNow(); //获取当前时间 53 | 54 | char* Str2Upper(char * entUpperName ); //字符串转化 55 | 56 | int ser_atoi(char *buf); //将字符转化为int 57 | 58 | int send_data(int conn_fd, send_t * psend); //发送数据 59 | 60 | void send_message(int conn_fd, char * send_name); //send message,need servive answer 61 | 62 | void show_message(int conn_fd); //recvie and show message 63 | 64 | void recv_message(int conn_fd, message_t * data); 65 | 66 | #endif 67 | 68 | -------------------------------------------------------------------------------- /backup/service.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-03 20:31 5 | * Filename : service.c 6 | * Description : 7 | * *****************************************************************************/ 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include"../common/common.h" 18 | #include"service.h" 19 | 20 | #define SERV_PORT 4507 21 | #define LISTENQ 12 22 | #define INVALID_USERINFO 'n' 23 | #define VALID_USERINFO 'y' 24 | #define USERNAME 0 25 | #define PASSWORD 1 26 | 27 | 28 | int main(int argc, char *argv[]){ 29 | int sock_fd, conn_fd; 30 | int optval; 31 | int flag_recv = USERNAME; 32 | int ret; 33 | pid_t pid; 34 | socklen_t cli_len; 35 | struct sockaddr_in cli_addr, serv_addr; 36 | char recv_buf[BUFSIZE]; 37 | 38 | //创建一个TCP套接字 39 | sock_fd = socket(AF_INET, SOCK_STREAM, 0); 40 | 41 | if(sock_fd < 0){ 42 | my_err("socket", __LINE__); 43 | } 44 | 45 | //设置该套接字使之可以重新绑定端口 46 | optval = 1; 47 | if(setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0){ 48 | my_err("setsokopt", __LINE__); 49 | } 50 | 51 | //初始化服务器端地址 52 | memset(&serv_addr, 0, sizeof(struct sockaddr_in)); 53 | serv_addr.sin_family = AF_INET; 54 | serv_addr.sin_port = htons(SERV_PORT); 55 | serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); 56 | 57 | //将套接字绑定到本地端口 58 | if(bind(sock_fd, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr_in)) < 0){ 59 | my_err("bind",__LINE__); 60 | } 61 | 62 | //将套接字转化为监听套接字 63 | if(listen(sock_fd, LISTENQ) < 0){ 64 | my_err("listen",__LINE__); 65 | } 66 | 67 | cli_len = sizeof(struct sockaddr_in); 68 | 69 | while(1){ 70 | 71 | //通过accept接受客户端的连接请求,并返回连接套接字用于手法数据 72 | conn_fd = accept(sock_fd, (struct sockaddr*)&cli_addr, &cli_len); 73 | if(conn_fd < 0){ 74 | my_err("accept", __LINE__); 75 | } 76 | 77 | printf("IP:%s is accepted.\n",inet_ntoa(cli_addr.sin_addr)); 78 | 79 | //创建一个子进程处理请求 80 | if((pid = fork()) == 0){ 81 | 82 | while(1){ 83 | my_recv(conn_fd, recv_buf, sizeof(recv_buf)); 84 | 85 | flag_recv = ser_atoi(recv_buf); 86 | switch(flag_recv){ 87 | case 11: 88 | serve_user_register(conn_fd); 89 | break; 90 | case 12: 91 | serve_user_update(conn_fd); 92 | break; 93 | case 13: 94 | serve_user_delet(conn_fd); 95 | break; 96 | case 14: 97 | serve_user_login(conn_fd); 98 | break; 99 | case 15: 100 | serve_user_logoff(conn_fd); 101 | break; 102 | case 21:; 103 | case 22:; 104 | case 23:; 105 | case 24:; 106 | case 25:; 107 | } 108 | }//while 109 | 110 | close(sock_fd); 111 | close(conn_fd); 112 | exit(0); //子进程 113 | 114 | } 115 | 116 | else{ 117 | close(conn_fd); 118 | } 119 | } 120 | return 0; 121 | } 122 | 123 | -------------------------------------------------------------------------------- /client/Client: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/client/Client -------------------------------------------------------------------------------- /client/Makefile: -------------------------------------------------------------------------------- 1 | #!Makefile 2 | 3 | PROGRAM = Client 4 | 5 | C_SOURCES = $(shell find . -name "*.c") 6 | C_OBJECTS = $(patsubst %.c, %.o, $(C_SOURCES)) 7 | 8 | CC = gcc 9 | LD = ld 10 | 11 | #C_FLAGS = -DDEBUG -c -Wall -Wextra -ggdb -I. 12 | C_FLAGS = -c. 13 | 14 | all: $(C_OBJECTS) 15 | @echo compile... 16 | $(CC) $(C_OBJECTS) -o $(PROGRAM) -lpthread 17 | 18 | .c.o: 19 | @echo link$< ... 20 | $(CC) $(C_FLAGS) $< -o $@ 21 | clean: 22 | $(RM) $(C_OBJECTS) 23 | -------------------------------------------------------------------------------- /client/account_per.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-05 09:46 5 | * Filename : account_per.c 6 | * Description : 7 | * *****************************************************************************/ 8 | #include "client.h" 9 | 10 | //检查用户文件是否存在 11 | int userinfo_p_checkfile(const char *username){ 12 | 13 | chdir("/home/wyf/"); 14 | 15 | if(access(username, 0) == 0){ 16 | return 1; 17 | } 18 | else{ 19 | return -1; 20 | } 21 | } 22 | 23 | int creat_file(const char * string){ 24 | int fd; 25 | 26 | if((fd = open(string, O_CREAT | O_RDWR | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP )) < 0){ 27 | my_err("file_creat", __LINE__); 28 | } 29 | 30 | return 0; 31 | } 32 | 33 | 34 | //创建用户文件 35 | int creat_userdir(const char *username){ 36 | int ret = 0; 37 | int fd; 38 | DIR * dir; 39 | char pathname[255]; 40 | 41 | strcpy(pathname, "/home/wyf/"); 42 | strcat(pathname, username); 43 | strcat(pathname, "/"); 44 | 45 | chdir("/home/wyf/"); 46 | 47 | if((fd = mkdir(username, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) < 0){ 48 | 49 | my_err("creat_info",__LINE__); 50 | } 51 | 52 | dir = opendir(username); 53 | if(dir == NULL){ 54 | my_err("dir", __LINE__); 55 | } 56 | 57 | chdir(pathname); 58 | 59 | if((fd = open(username, O_CREAT | O_RDWR | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP )) < 0){ 60 | my_err("creat_info",__LINE__); 61 | } 62 | close(fd); 63 | if((fd = open("message", O_CREAT | O_RDWR | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP )) < 0){ 64 | my_err("creat_message",__LINE__); 65 | } 66 | 67 | close(fd); 68 | closedir(dir); 69 | ret = 1; 70 | 71 | return ret; 72 | } 73 | 74 | //写入用户信息 75 | int userinfo_p_insert(const userinfo_t * data){ 76 | int ret = 0; 77 | int fd; 78 | int len; 79 | 80 | my_chdir(); 81 | fd = open(data->username, O_RDWR | O_TRUNC ); 82 | if(fd < 0){ 83 | my_err("open_p", __LINE__); 84 | } 85 | len = sizeof(userinfo_t); 86 | ret = file_write(fd, data, len); 87 | close(fd); 88 | 89 | return ret; 90 | } 91 | 92 | //根据用户id获取用户信息并通过data传出 93 | 94 | int userinfo_p_seletid(int userid, userinfo_t * data){ 95 | 96 | FILE *fp; 97 | int found = 0; 98 | userinfo_t buf; 99 | fp = fopen("USER","rb"); 100 | if(fp == NULL){ 101 | return 0; 102 | } 103 | while(!feof(fp)){ 104 | if(fread(&buf,sizeof(userinfo_t),1,fp)){ 105 | data = &buf; 106 | found = 1; 107 | break; 108 | } 109 | } 110 | fclose(fp); 111 | 112 | return found; 113 | } 114 | 115 | 116 | //根据用户名获取用户信息并通过data传出 117 | 118 | int userinfo_p_seletname(const char* username, userinfo_t * data){ 119 | 120 | FILE *fp; 121 | int found = 0; 122 | userinfo_t buf; 123 | fp = fopen("USER","rb"); 124 | if(fp == NULL){ 125 | return 0; 126 | } 127 | while(!feof(fp)){ 128 | if(fread(&buf,sizeof(userinfo_service_t),1,fp)){ 129 | if(strcmp(buf.username,username) == 0){ 130 | data = &buf; 131 | found = 1; 132 | break; 133 | } 134 | } 135 | } 136 | fclose(fp); 137 | return found; 138 | } 139 | 140 | 141 | 142 | int userinfo_p_delet(const char * username){ 143 | FILE * fp; 144 | int ret = 0; 145 | 146 | fp = fopen(username,"rb+"); 147 | if(fp == NULL){ 148 | my_err("delet_p", __LINE__); 149 | } 150 | fclose(fp); 151 | if(unlink(username) < 0){ 152 | my_err("unlink_p",__LINE__); 153 | } 154 | ret = 1; 155 | return ret; 156 | } 157 | 158 | 159 | -------------------------------------------------------------------------------- /client/account_per.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/client/account_per.o -------------------------------------------------------------------------------- /client/account_ser.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-05 13:52 5 | * Filename : account_ser.c 6 | * Description : 7 | * *****************************************************************************/ 8 | 9 | #include "client.h" 10 | 11 | int userinfo_s_add(userinfo_t *data){ 12 | int ret = 0; 13 | 14 | ret = userinfo_p_checkfile(data->username); 15 | if(ret < 0){ 16 | creat_userdir(data->username); 17 | } 18 | 19 | ret = userinfo_p_insert(data); 20 | 21 | return ret; 22 | } 23 | 24 | int userinfo_s_delet(const char *username) 25 | { 26 | int ret; 27 | 28 | ret = userinfo_p_delet(username); 29 | 30 | return ret; 31 | } 32 | 33 | int userinfo_s_updata(userinfo_t *data, const char* username) 34 | { 35 | int ret = 0; 36 | 37 | userinfo_p_delet(username); 38 | 39 | ret = userinfo_s_add(data); 40 | 41 | return ret; 42 | } 43 | 44 | int userinfo_s_select(const char *username, userinfo_t *data) 45 | { 46 | int ret = 0; 47 | 48 | ret = userinfo_p_seletname(username, data); 49 | 50 | return ret; 51 | } 52 | 53 | int userinfo_s_login(const char *username){ 54 | 55 | int ret = 0; 56 | 57 | chdir("/home/wyf/"); 58 | if(access(username, 0) < 0){ 59 | if(creat_userdir(username) < 0){ 60 | my_err("file make", __LINE__); 61 | } 62 | } 63 | 64 | ret = 1; 65 | 66 | return ret; 67 | } 68 | -------------------------------------------------------------------------------- /client/account_ser.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/client/account_ser.o -------------------------------------------------------------------------------- /client/account_u.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-08 09:09 5 | * Filename : account_u.c 6 | * Description : 7 | * *****************************************************************************/ 8 | 9 | #include"client.h" 10 | 11 | //system界面 12 | 13 | void account_ui_init(int conn_fd,char *username){ 14 | char choice; 15 | 16 | do{ 17 | puts(""); 18 | puts("\033[33mwhat do you want?\033[0m"); 19 | puts("Update"); 20 | puts("delet"); 21 | puts("return"); 22 | puts("exit"); 23 | 24 | puts("\033[33m.-.-.-.-.-.-.-.-.-.--.-.\033[0m"); 25 | choice = l_getc(); 26 | 27 | switch(choice){ 28 | case 'u': 29 | case 'U': 30 | account_ui_update(conn_fd,username); 31 | break; 32 | case 'd': 33 | case 'D': 34 | //call S prepare to delet user informatio 35 | account_ui_delete(conn_fd, username); 36 | break; 37 | case 'r': 38 | case 'R': 39 | return; 40 | break; 41 | } 42 | }while(choice != 'e' && choice != 'E'); 43 | account_ui_logoff(conn_fd,username); 44 | } 45 | 46 | 47 | //登录后界面 48 | void userinfo_ui_init(int conn_fd, char *username){ 49 | 50 | char choice; 51 | 52 | system("clear"); 53 | do{ 54 | print(); 55 | puts("\033[33mwhat do you want?\033[0m"); 56 | puts("Chat"); 57 | puts("FTP"); 58 | puts("Systerm"); 59 | puts("Exit"); 60 | 61 | puts("\033[33m.-.-.-.-.-.-.-.-.-.--.-.\033[0m"); 62 | choice = l_getc(); 63 | 64 | switch(choice){ 65 | case 'c': 66 | case 'C': 67 | chat_user_init(conn_fd, username); 68 | break; 69 | case 'f': 70 | case 'F': 71 | file_ui_init(conn_fd, username); 72 | break; 73 | case 's': 74 | case 'S': 75 | account_ui_init(conn_fd,username); 76 | break; 77 | } 78 | }while(choice != 'e' && choice != 'E'); 79 | 80 | account_ui_logoff(conn_fd, username); 81 | } 82 | 83 | 84 | void account_ui_update(int conn_fd, char *username){ 85 | 86 | userinfo_t info; 87 | userinfo_t *data = &info; 88 | send_t send_buf; 89 | send_t *psend = &send_buf; 90 | char choice; 91 | char input_buf[32], passwd_buf[32]; 92 | int ret = 0; 93 | 94 | 95 | userinfo_s_select(username, data); 96 | 97 | puts("\033[33myour information blew:\033[0m"); 98 | 99 | printf("%s","name:"); 100 | puts(data->username); 101 | printf("%s","sex:"); 102 | switch(data->sex){ 103 | case 1: 104 | puts("faleman"); 105 | break; 106 | case 2: 107 | puts("man"); 108 | break; 109 | } 110 | 111 | cl_stdin(); 112 | puts("\033[33m\nwhat do you want\033[0m]"); 113 | puts("updata sex"); 114 | puts("updata password"); 115 | puts("return"); 116 | 117 | choice = l_getc(); 118 | 119 | switch(choice){ 120 | case 'p': 121 | case 'P': 122 | printf("new password:"); 123 | getpwd(32, input_buf); 124 | printf("check if:"); 125 | getpwd(32, passwd_buf); 126 | if(strcmp(passwd_buf, input_buf) == 0){ 127 | strcpy(data->userpasswd, passwd_buf); 128 | } 129 | else{ 130 | puts("wrong, not the same"); 131 | return; 132 | } 133 | break; 134 | case 's': 135 | case 'S': 136 | puts("1.female"); 137 | puts("2.man"); 138 | scanf("%d",&ret); 139 | if(ret == 1){ 140 | data->sex = USER_FEMALE; 141 | } 142 | else if(ret == 2){ 143 | data->sex = USER_MAN; 144 | } 145 | break; 146 | } 147 | 148 | strcpy(psend->userinfo.userpasswd, data->userpasswd); 149 | psend->command_type = _USERCHA; 150 | strcpy(psend->userinfo.username, username); 151 | if(data->sex == USER_MAN){ 152 | strcpy(psend->userinfo.usersex, "man"); 153 | } 154 | else if(data->sex == USER_FEMALE){ 155 | strcpy(psend->userinfo.usersex, "female"); 156 | } 157 | 158 | send_data(conn_fd, psend); 159 | 160 | //userinfo_s_updata(data, username); 161 | 162 | //should be run to sign 163 | return; 164 | } 165 | 166 | 167 | void account_ui_logoff(int conn_fd,char *username){ 168 | send_t send_buf; 169 | send_t *psend = &send_buf; 170 | 171 | memset(psend, 0, sizeof(send_t)); 172 | strcpy(psend->userinfo.username, username); 173 | psend->conn_fd = conn_fd; 174 | // psend->userstatus = _OFFLINE; 175 | psend->command_type = _USEROFFL; 176 | send_data(conn_fd, psend); 177 | 178 | puts("\033[31mlog off ...\33[8m]"); 179 | 180 | sleep(2); 181 | 182 | shutdown(conn_fd, SHUT_RD); 183 | exit(0); 184 | } 185 | 186 | 187 | void account_ui_delete(int conn_fd,char *username){ 188 | 189 | send_t send_buf; 190 | send_t *psend = &send_buf; 191 | char choice; 192 | 193 | system("clear"); 194 | puts("\033[31m:ready for delection, can never return\033[1m"); 195 | puts("delet(y/Y)?"); 196 | 197 | choice = l_getc(); 198 | if(choice == 'y' || choice == 'Y'){ 199 | 200 | strcpy(psend->userinfo.username, username); 201 | psend->command_type = _USERDEL; 202 | send_data(conn_fd, psend); 203 | 204 | userinfo_s_delet(username); 205 | 206 | account_ui_logoff(conn_fd, username); 207 | } 208 | else if(choice == 'r' || choice == 'R'){ 209 | return ; 210 | } 211 | } 212 | -------------------------------------------------------------------------------- /client/account_u.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/client/account_u.o -------------------------------------------------------------------------------- /client/chat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/client/chat.o -------------------------------------------------------------------------------- /client/client.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : Catherine199787@outlook.com 4 | * Last modified : 2016-08-03 15:24 5 | * Filename : client.c 6 | * Description : client 7 | * *****************************************************************************/ 8 | #include"client.h" 9 | char h_username[32]; 10 | //sigin in 11 | 12 | int signin_ui(int conn_fd){ 13 | 14 | int ret = 0; 15 | send_t send_buf, recv_buf; 16 | send_t *psend = &send_buf; 17 | send_t *precv = &recv_buf; 18 | 19 | system("clear"); 20 | 21 | printf("\033[31m \033[0m:\033[33m welcome \033[0m,log in please\n"); 22 | printf("username:"); 23 | 24 | input_check(32, psend->userinfo.username); //send and keep username 25 | 26 | printf("password:"); 27 | getpwd(32, psend->userinfo.userpasswd); 28 | 29 | psend->command_type = _USERONL; 30 | psend->conn_fd = conn_fd; 31 | send_data(psend->conn_fd, psend); 32 | 33 | recv_data(conn_fd, precv); 34 | 35 | switch(precv->userstatus){ 36 | case INVALID_USERINFO: 37 | if(precv->input_check == _INPUTNAME){ 38 | puts("name not exist"); 39 | } 40 | else if(precv->input_check == _INPUTPASSWD){ 41 | puts("wrong password"); 42 | } 43 | else if(precv->input_check == _ONLINE){ 44 | puts("user already signed in"); 45 | } 46 | break; 47 | case VALID_USERINFO: 48 | strcpy(h_username, precv->userinfo.username); 49 | ret = 1; 50 | puts("Sign in..."); 51 | if(userinfo_s_login(precv->userinfo.username) < 0){ 52 | my_err("file wrong", __LINE__); 53 | } 54 | break; 55 | } 56 | sleep(1); 57 | return ret; 58 | } 59 | 60 | 61 | //register 62 | int register_ui(int conn_fd){ 63 | 64 | int ret = 0; 65 | char sex_buf[10]; 66 | char passwd_buf[32]; 67 | char input_buf[32]; 68 | 69 | send_t send_buf, recv_buf; 70 | send_t *psend = &send_buf; 71 | send_t *precv = &recv_buf; 72 | 73 | userinfo_t info; 74 | userinfo_t *data = &info; 75 | 76 | puts(" "); 77 | puts("\033[31m\033[0m:complete you information first"); 78 | 79 | //username 80 | printf("username:"); 81 | 82 | input_check(32, psend->userinfo.username); //send and keep username 83 | 84 | strcpy(info.username, psend->userinfo.username); 85 | 86 | //password 87 | while(1){ 88 | printf("password:"); 89 | 90 | getpwd(32,input_buf); //implicit input 91 | printf("check it:"); //input twice 92 | getpwd(32, passwd_buf); 93 | 94 | //when input same strings 95 | if(strcmp(input_buf, passwd_buf) == 0){ 96 | strcpy(psend->userinfo.userpasswd, passwd_buf); 97 | strcpy(info.userpasswd,input_buf); 98 | 99 | break; //while 100 | } 101 | else{ 102 | puts("wrong!input again,"); 103 | continue; 104 | } 105 | } 106 | 107 | //sex 108 | while(1){ 109 | printf("your sex[man/female]:"); 110 | input_check(10, sex_buf); 111 | 112 | //limit string 113 | if(strcmp(sex_buf, "man") == 0){ 114 | info.sex = USER_MAN; 115 | strcpy(psend->userinfo.usersex, sex_buf); 116 | 117 | break; 118 | } 119 | else if(strcmp(sex_buf, "female") == 0){ 120 | info.sex = USER_FEMALE; 121 | strcpy(psend->userinfo.usersex, sex_buf); 122 | 123 | break; 124 | } 125 | else{ 126 | puts("wrong input"); 127 | continue; 128 | } 129 | } 130 | 131 | 132 | psend->conn_fd = conn_fd; 133 | psend->command_type = _USERADD; 134 | 135 | send_data(psend->conn_fd,psend); 136 | 137 | recv_data(conn_fd, precv); 138 | 139 | switch(precv->userstatus){ 140 | case VALID_USERINFO: 141 | ret = 1; 142 | break; 143 | case INVALID_USERINFO: 144 | if(precv->input_check == _INPUTNAME){ 145 | puts("name exist"); 146 | } 147 | break; 148 | } 149 | 150 | if(ret == 1){ 151 | //store infomation 152 | userinfo_s_add(data); 153 | puts("register successe, sign in now!"); 154 | } 155 | sleep(1); 156 | return ret; 157 | } 158 | 159 | 160 | int main(){ 161 | 162 | int conn_fd; 163 | int serv_port; 164 | struct sockaddr_in serv_addr; 165 | send_t send_buf, recv_buf; 166 | send_t *psend = &send_buf; 167 | send_t *precv = &recv_buf; 168 | pthread_t pid; 169 | userinfo_t data; 170 | memset(&data, 0, sizeof(userinfo_t)); 171 | 172 | 173 | //初始化服务器地址结构 174 | memset(&serv_addr, 0, sizeof(struct sockaddr_in)); 175 | serv_addr.sin_family = AF_INET; 176 | 177 | //服务器的端口与地址 178 | serv_port = 4507; 179 | serv_addr.sin_port = htons(serv_port); 180 | if(inet_aton("192.168.30.141", &serv_addr.sin_addr) == 0){ 181 | my_err("invalid server ip address,please check the service IP\n",__LINE__); 182 | } 183 | 184 | //创建一个TCP套接字 185 | conn_fd = socket(AF_INET, SOCK_STREAM, 0); 186 | if(conn_fd < 0){ 187 | my_err("socket", __LINE__); 188 | } 189 | 190 | //向服务器发送连接请求 191 | if(connect(conn_fd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) < 0){ 192 | my_err("connect", __LINE__); 193 | exit(0); 194 | } 195 | 196 | 197 | user_init_(conn_fd); //return when sign in 198 | 199 | precv->conn_fd = conn_fd; 200 | 201 | pthread_create(&pid, NULL, (void*)thread_fun, (void *)conn_fd); 202 | 203 | userinfo_ui_init(conn_fd, h_username); //main meune 204 | 205 | shutdown(conn_fd, SHUT_RD); 206 | } 207 | 208 | int * thread_fun(int conn_fd){ 209 | 210 | int ret; 211 | pthread_t pid; 212 | send_t recv_buf; 213 | send_t *precv = &recv_buf; 214 | memset(precv, 0, sizeof(recv_buf)); 215 | void *flag; 216 | 217 | while(1){ 218 | 219 | if((ret = recv_data(conn_fd, precv)) <= 0){ 220 | print(); 221 | puts("\033[31m404 not FOUND...\033[0m"); 222 | sleep(1); 223 | account_ui_logoff(conn_fd, precv->userinfo.username); 224 | break; 225 | } 226 | //else if(ret == 0){ 227 | // break; 228 | //} 229 | else{ 230 | 231 | pthread_create(&pid, NULL, (void*)&client, precv) ; 232 | pthread_join(pid, &flag); 233 | 234 | } 235 | } 236 | pthread_exit(0); 237 | } 238 | 239 | 240 | int user_init_(int conn_fd){ 241 | 242 | char choice; 243 | do{ 244 | int ret= 0; 245 | //登陆界面(int conn_fd) 246 | system("clear"); 247 | puts("\033[33m welcome ,what do you want?\033[0m"); 248 | puts("Sign in"); 249 | puts("Register"); 250 | puts("Exit"); 251 | puts(""); 252 | choice = l_getc(); 253 | 254 | cl_stdin(); 255 | switch (choice) { 256 | case 'S': 257 | case 's': 258 | ret = signin_ui(conn_fd); 259 | break; 260 | 261 | case 'r': 262 | case 'R': 263 | register_ui(conn_fd); 264 | break; 265 | } 266 | puts("\033[33m.-.-.-.-.-.-.-.-.-.--.-.\033[0m"); 267 | if(ret == 1){ 268 | return 0; 269 | } 270 | 271 | }while(choice != 'e' && choice != 'E'); 272 | 273 | account_ui_logoff(conn_fd, (char *)("user")); 274 | } 275 | 276 | 277 | 278 | void client(send_t * precv){ 279 | 280 | if(precv->userinfo.leavemes_flag == _LEAVEMES){ 281 | //chat_leave_check(); 282 | } 283 | 284 | switch(precv->command_type){ 285 | 286 | case _FRIENDDEL: 287 | 288 | chat_friend_del(precv); 289 | 290 | break; 291 | case _FRIENDLIST: 292 | 293 | chat_friend_show(precv); 294 | break; 295 | 296 | case _GROUPLIST: 297 | 298 | chat_group_show(precv); 299 | break; 300 | 301 | case _FRIENDFIND: 302 | 303 | friend_find_add(precv); 304 | break; 305 | case _GROUPSEND: 306 | 307 | group_find_add(precv); 308 | 309 | break; 310 | case _USERSALL: 311 | 312 | chat_friend_add(precv); 313 | 314 | break; 315 | case _GROUPSALL: 316 | 317 | chat_group_add(precv); 318 | 319 | break; 320 | 321 | case _DOWNLOAD: 322 | download_file(precv); 323 | break; 324 | case _FRIENDASK: 325 | printf("\33[s"); 326 | friend_chat_ask(precv); 327 | printf("\33[u"); 328 | fflush(stdout); 329 | break; 330 | case _MESSTIP: 331 | printf("\033[s"); 332 | message_tips(precv); 333 | printf("\33[u"); 334 | fflush(stdout); 335 | break; 336 | 337 | case _CHATONE: 338 | printf("\033[s"); 339 | user_chat_one(precv); 340 | printf("\33[u"); 341 | fflush(stdout); 342 | break; 343 | } 344 | 345 | } 346 | 347 | 348 | int print(){ 349 | 350 | int i = 35; 351 | 352 | printf("\33[10;1H"); 353 | 354 | while(i--){ 355 | printf("\33[K"); 356 | printf("\33[1B"); 357 | } 358 | 359 | printf("\33[10;1H"); 360 | 361 | return 0; 362 | } 363 | 364 | 365 | void my_chdir(){ 366 | 367 | userinfo_t data; 368 | userinfo_p_seletname(h_username, &data); 369 | 370 | chdir(data.path); 371 | } 372 | -------------------------------------------------------------------------------- /client/client.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-05 15:25 5 | * Filename : client.h 6 | * Description : 7 | * *****************************************************************************/ 8 | #ifndef CLIENT_H 9 | #define CLIENT_H 10 | 11 | #include "../common/common.h" 12 | 13 | typedef enum{ 14 | USER_FEMALE = 1, 15 | USER_MAN = 2 16 | }userinfo_sex_t; 17 | 18 | typedef enum{ 19 | LEAVE_MESSAGE_IN = 1, 20 | LEAVE_MESSAGE_NO = 2 21 | }userinfo_leavemes_t; 22 | 23 | 24 | typedef struct{ 25 | char type; 26 | char username[32]; 27 | char userpasswd[32]; 28 | char path[32]; 29 | userinfo_sex_t sex; 30 | userinfo_leavemes_t flag_leave; 31 | }userinfo_t; 32 | 33 | void * client_t(void * fd); //thread function 34 | 35 | int user_init_(int conn_fd); //menu 36 | 37 | int Scanfkeyboard(); //直接读取键盘输入 38 | 39 | int userinfo_s_add(userinfo_t *data ); //注册用 40 | 41 | int userinfo_s_delet(const char *username); 42 | 43 | int userinfo_s_select(const char *usestring, userinfo_t *data); 44 | 45 | int userinfo_s_updata(userinfo_t *data, const char *username); 46 | 47 | int userinfo_s_login(const char *username); //登录文件的检查的创建 48 | 49 | int message_tips(send_t *precv); 50 | 51 | int *thread_fun(int conn_fd); 52 | 53 | int signin_ui(int conn_fd); 54 | 55 | int pthread_recv(int conn_fd); 56 | 57 | void input_userinfo(int conn_fd, const char *string); 58 | 59 | void userinfo_ui_init(int conn_fd, char *username); //登录后界面 60 | 61 | void account_ui_init(int conn_fd, char* username); 62 | 63 | void account_ui_update(int conn_fd,char *username); 64 | 65 | void account_ui_delete(int conn_fd,char *username); 66 | 67 | void account_ui_logoff(int conn_fd, char *username); 68 | 69 | int userinfo_p_checkfile(const char * username); //检查文件是否存在 70 | 71 | int userinfo_p_update(const userinfo_t *data); //修改用户信息 72 | 73 | int userinfo_p_insert(const userinfo_t *data); //写入用户信息 74 | 75 | int userinfo_p_delet(const char *username); //注销用户 76 | 77 | int creat_userdir(const char *username); //创建用户文件 78 | 79 | int userinfo_p_seletid(int userid, userinfo_t *data); //获取用户信息 80 | 81 | int userinfo_p_seletname(const char *username, userinfo_t *data); //用户名获取用户信息 82 | //int message_p_select(const char *mesfilename); 83 | int message_p_insert(message_t * mes); 84 | 85 | int message_p_update(message_t * mes); 86 | 87 | int message_p_all(message_t *box, int type); 88 | 89 | int message_p_selet(int type, message_t *mes); 90 | 91 | int print(); 92 | 93 | int friend_chat_one(int conn_fd, char * username); 94 | 95 | int user_chat_one(send_t *precv); 96 | 97 | int message_init(int conn_fd, char * username); 98 | 99 | int message_store(); 100 | 101 | int message_box(int conn_fd, char *username, int type); 102 | 103 | int chat_user_init(int conn_fd, char *username); 104 | 105 | int chat_user_one(int conn_fd, char *username, char *name); 106 | 107 | int chat_user_group(int conn_fd, char *username, char * name); 108 | 109 | int chat_friend_ask(int conn_fd, char *username); 110 | 111 | int friend_chat_ask(send_t * precv); 112 | 113 | int chat_friend_show(send_t *precv); 114 | 115 | int friend_chat_show(int conn_fd, char *username); 116 | 117 | int chat_friend_del(send_t *precv); 118 | 119 | int chat_group_show(send_t *precv); 120 | 121 | int group_chat_show(int conn_fd, char *username); 122 | 123 | int chat_stranger_show(int conn_fd, char *username); 124 | 125 | int chat_friend_add(send_t *precv); 126 | 127 | int chat_group_add(send_t *precv); 128 | 129 | int friend_chat_send(int conn_fd, char* username); 130 | 131 | int friend_chat_add(int conn_fd, char *username); 132 | 133 | int file_ui_init(int conn_fd, char * username); 134 | 135 | int download_file(send_t *precv); 136 | 137 | int upload_file(int conn_fd, char *name); 138 | 139 | int group_chat_add(int conn_fd, char *username); 140 | 141 | int group_find_add(send_t *precv); 142 | 143 | int friend_find_add(send_t *precv); 144 | 145 | void chat_leave_check(); 146 | 147 | void client(send_t *precv); 148 | 149 | void my_chdir(); 150 | 151 | 152 | #endif 153 | -------------------------------------------------------------------------------- /client/client.h.gch: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/client/client.h.gch -------------------------------------------------------------------------------- /client/client.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/client/client.o -------------------------------------------------------------------------------- /client/common/common.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-05 08:49 5 | * Filename : common.c 6 | * Description : 7 | * *****************************************************************************/ 8 | #include"common.h" 9 | 10 | 11 | char l_getc(){ 12 | char ch; 13 | scanf("%c",&ch); 14 | while(ch == '\n'){ 15 | scanf("%c",&ch); 16 | } 17 | return ch; 18 | } 19 | 20 | //Linux没有getch() 21 | 22 | int getch(void){ 23 | int c= 0; 24 | struct termios org_opts, new_opts; 25 | int res = 0; 26 | 27 | //将之前的设置备份 28 | res = tcgetattr(STDIN_FILENO, &org_opts); 29 | assert(res == 0); 30 | 31 | //设置新的设定 32 | memcpy(&new_opts, &org_opts, sizeof(new_opts)); 33 | new_opts.c_lflag &= ~(ICANON |ECHO | ECHOE | ECHOK |ECHONL | ECHOKE | ICRNL); 34 | tcsetattr(STDIN_FILENO, TCSANOW, &new_opts); 35 | c= getchar(); 36 | 37 | //恢复之前的设定 38 | res = tcsetattr(STDIN_FILENO, TCSANOW, &org_opts); 39 | assert(res == 0); 40 | return c; 41 | } 42 | 43 | //密码输入 44 | 45 | void getpwd(int maxlen, char *pwd){ 46 | int j= 0; 47 | char c; 48 | 49 | while((j < maxlen-2) && (c = getch()) != '\n'){ 50 | if(c != 127){ 51 | printf("*"); 52 | pwd[j++] = c; 53 | } 54 | else { 55 | if(j > 0){ 56 | j = j - 1; 57 | printf("\b \b"); 58 | } 59 | } 60 | } 61 | pwd[j++] = '\0'; 62 | pwd[j++] = '\n'; 63 | 64 | puts(" "); 65 | } 66 | 67 | void cl_stdin(){ 68 | char ch; 69 | while(( ch = getchar()) != '\n' && ch != EOF); 70 | } 71 | 72 | //合法输入 73 | int input_check(int input_len, char * input_buf){ 74 | char c; 75 | int i = 0; 76 | if(input_buf == NULL){ 77 | my_err("input check", __LINE__); 78 | } 79 | 80 | while((c = getchar()) != '\n' && (c != EOF) && (i < input_len-2)){ 81 | input_buf[i++] = c; 82 | } 83 | 84 | input_buf[i++] = '\0'; 85 | input_buf[i++] = '\n'; 86 | 87 | return i; 88 | } 89 | 90 | 91 | 92 | //自定义错误函数 93 | void my_err(const char* err_string, int line){ 94 | fprintf(stderr, "line:%d \n", line); 95 | perror(err_string); 96 | } 97 | 98 | //从套接字上读取一次数据 99 | 100 | int recv_data(int conn_fd, send_t *precv){ 101 | int len_remain = 0; 102 | 103 | len_remain = recv(conn_fd, precv, sizeof(send_t), 0); 104 | if(len_remain < 0){ 105 | my_err("recv", __LINE__); 106 | } 107 | 108 | printf("%d recv\n", precv->command_type); 109 | return len_remain; 110 | } 111 | 112 | 113 | //文件写入函数 114 | 115 | int file_write(int fd, const void * buf, int len){ 116 | int we; 117 | 118 | we = write(fd , buf, len); 119 | if(we < 0){ 120 | my_err("file_write", __LINE__); 121 | } 122 | 123 | return we; 124 | } 125 | 126 | //获取文件长度 127 | int file_len(int fd){ 128 | 129 | int length = 0; 130 | if(lseek(fd ,0 ,SEEK_END)== -1){ 131 | my_err("lseek set", __LINE__); 132 | } 133 | if((length = lseek(fd , 0 , SEEK_CUR)) == -1 ){ 134 | my_err("length" , __LINE__); 135 | } 136 | if(lseek(fd , 0 ,SEEK_SET)== -1){ 137 | my_err("lseek end" , __LINE__); 138 | } 139 | return length; 140 | } 141 | 142 | 143 | int file_read(int fd,int len,void* buf){ 144 | int re; 145 | if((re = read(fd , buf , len)) == -1){ 146 | my_err("c_read", __LINE__); 147 | } 148 | return re; 149 | } 150 | 151 | //显示接收信息 152 | /* 153 | void show_recv(int conn_fd, char *recv_buf, int buflen ){ 154 | int i; 155 | int ret; 156 | 157 | ret = my_recv(conn_fd, recv_buf, buflen); 158 | if(ret < 0){ 159 | my_err("recv faild", __LINE__); 160 | } 161 | 162 | for(i = 0; i < ret; i++){ 163 | printf("%c", recv_buf[i]); 164 | } 165 | } 166 | */ 167 | //获取系统当前日期 168 | user_date_t DateNow() { 169 | user_date_t curDate; 170 | time_t now; //实例化time_t结构 171 | struct tm *timeNow; //实例化tm结构指针 172 | time(&now); 173 | timeNow = localtime(&now); 174 | curDate.year=timeNow->tm_year+1900; 175 | curDate.month=timeNow->tm_mon+1; 176 | curDate.day=timeNow->tm_mday; 177 | 178 | return curDate; 179 | } 180 | 181 | //获取系统当前时间 182 | user_time_t TimeNow(){ 183 | user_time_t curTime; 184 | time_t now; //实例化time_t结构 185 | struct tm *timeNow; //实例化tm结构指针 186 | time(&now); 187 | timeNow = localtime(&now); 188 | curTime.hour=timeNow->tm_hour; 189 | curTime.minute=timeNow->tm_min; 190 | curTime.second=timeNow->tm_sec; 191 | 192 | return curTime; 193 | } 194 | 195 | int ser_atoi(char * buf){ //atoi 196 | 197 | int ret; 198 | ret = atoi(buf); 199 | 200 | return ret; 201 | } 202 | /* 203 | int recv_data(int conn_fd, send_t * psend){ 204 | int n = 0; 205 | send_t recv_buf; 206 | char *precv = (char*) &recv_buf; 207 | 208 | if(n == 0){ 209 | n = recv(conn_fd, &recv_buf, sizeof(recv_buf), 0); 210 | if(n <= 0){ 211 | my_err("recv_", __LINE__); 212 | } 213 | else if(n == 0){ 214 | return 0; 215 | } 216 | precv = (char*)&recv_buf; 217 | } 218 | 219 | memcpy(psend, precv, sizeof(send_t)); 220 | 221 | return sizeof(send_t); 222 | } 223 | */ 224 | 225 | //发送数据 226 | int send_data(int conn_fd, send_t * psend){ 227 | 228 | int ret; 229 | if((ret = send(conn_fd, psend, sizeof(send_t), 0)) < 0){ 230 | my_err("send",__LINE__); 231 | } 232 | 233 | 234 | printf("%d send\n", psend->command_type); 235 | memset(psend, 0, sizeof(send_t)); 236 | return ret; 237 | } 238 | 239 | 240 | 241 | //将字符串str就地转换为大写字符串,并返回字符串头指针 242 | char *Str2Upper(char *str) { 243 | if (NULL == str) 244 | return NULL; 245 | else { 246 | char *p = str; 247 | while ('\0' != *p) { 248 | if (*p >= 'a' && *p <= 'z') 249 | *p -= 32; 250 | p++; 251 | } 252 | return str; 253 | } 254 | } 255 | 256 | //显示消息 257 | /*void show_message(int conn_fd){ 258 | message_t leave; 259 | message_t * pmess = &leave; 260 | 261 | recv(conn_fd, pmess, sizeof(leave), 0); 262 | printf("%d-%d-%d %2d:%2d:%2d\n",pmess->date.year,pmess->date.month,pmess->date.day,pmess->time.hour,pmess->time.minute,pmess->time.second); 263 | printf("<%s>: ", pmess->send_name); 264 | puts(pmess->message_buf); 265 | puts(" "); 266 | }*/ 267 | 268 | /* 269 | void recv_message(int conn_fd, message_t * data){ 270 | 271 | if(recv(conn_fd, data, sizeof(message_t), 0) < 0){ 272 | send_data(conn_fd,(char *)("n")); 273 | return; 274 | } 275 | else{ 276 | send_data(conn_fd, (char*)("y")); 277 | } 278 | 279 | } 280 | 281 | 282 | void send_message(int conn_fd, char * send_name){ 283 | 284 | static char send_buf[BUFSIZE-36]; 285 | static char recv_buf[BUFSIZE]; 286 | int flag = INVALID_INPUT; 287 | 288 | message_t info; 289 | message_t *data = &info; 290 | 291 | data->date = DateNow(); 292 | data->time = TimeNow(); 293 | strcpy(data->send_name, send_name); 294 | 295 | input_check(BUFSIZE-36, send_buf); 296 | strcpy(data->message_buf, send_buf); 297 | do{ 298 | 299 | if(send(conn_fd, data, sizeof(info), 0) < 0){ 300 | my_err("send", __LINE__); 301 | } 302 | 303 | if(my_recv(conn_fd, recv_buf,sizeof(recv_buf)) < 0){ 304 | my_err("data is too long", __LINE__); 305 | } 306 | 307 | if(recv_buf[0] == VALID_INPUT){ 308 | flag = VALID_INPUT; 309 | } 310 | else{ 311 | printf("error, input again,\n"); 312 | flag = INVALID_INPUT; 313 | input_check(BUFSIZE-36, send_buf); 314 | strcpy(data->message_buf, send_buf); 315 | } 316 | }while(flag == INVALID_INPUT); 317 | 318 | } 319 | */ 320 | -------------------------------------------------------------------------------- /client/common/common.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-05 09:10 5 | * Filename : common.h 6 | * Description : 7 | * *****************************************************************************/ 8 | #ifndef COMMON_H_ 9 | #define COMMON_H_ 10 | 11 | #include 12 | #include 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 | #include"struct.h" 29 | 30 | int file_write(int fd, const void * data, int len); //写入文件 31 | 32 | int file_read(int fd, int len, void * data); //读出文件 33 | 34 | int file_len(int fd);// 获得文件长度 35 | 36 | void my_err(const char * err_string, int line); 37 | 38 | int recv_data(int conn_fd, send_t *precv); 39 | 40 | char l_getc(); //从键盘获取一个字符,避免回车 41 | 42 | void cl_stdin(); //清空输入缓冲 43 | 44 | void getpwd(int maxlen, char *pwd); 45 | 46 | int input_check(int input_len, char *input_buf); //输入合法性检查 47 | 48 | void show_recv(int conn_fd, char * recv_buf, int buflen); //显示接收消息 49 | 50 | user_date_t DateNow(); //获取当前日期 51 | 52 | user_time_t TimeNow(); //获取当前时间 53 | 54 | char* Str2Upper(char * entUpperName ); //字符串转化 55 | 56 | int ser_atoi(char *buf); //将字符转化为int 57 | 58 | int send_data(int conn_fd, send_t * psend); //发送数据 59 | 60 | void send_message(int conn_fd, char * send_name); //send message,need servive answer 61 | 62 | void show_message(int conn_fd); //recvie and show message 63 | 64 | void recv_message(int conn_fd, message_t * data); 65 | 66 | #endif 67 | 68 | -------------------------------------------------------------------------------- /client/common/common.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/client/common/common.o -------------------------------------------------------------------------------- /client/common/struct.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-13 21:12 5 | * Filename : struct.h 6 | * Description : 7 | * *****************************************************************************/ 8 | 9 | 10 | #define ACCOUNTSIZE 100 //最大连接数量 11 | #define FRIENDSIZE 50 //最大好友数 12 | #define BUFSIZE 1024 //最大缓冲区 13 | #define GROUPSIZE 50 //最大群规模 14 | #define GROUPCOUNT 20 //最大群数量 15 | 16 | #define INVALID_USERINFO 'n' //无效信息 17 | #define VALID_USERINFO 'y' // 有效信息 18 | 19 | 20 | //用户操作类型 21 | 22 | #define _USERADD 11 //用户注册 23 | #define _USERCHA 12 //更改用户基本信息 24 | #define _USERDEL 13 //删除用户 25 | #define _USERONL 14 //上线操作 26 | #define _USEROFFL 15 //下线操作 27 | 28 | #define _LEAVEMES 's' //有离线消息 29 | #define _NOMESSAGE 'e' //无离线消息 30 | #define _ONLINE 'o' //为上线状态 31 | #define _OFFLINE 'd' //为离线状态 32 | 33 | #define _FRIENDLIST 21 //用户好友列表 34 | #define _FRIENDFIND 22 //查找用户 35 | #define _FRIENDASK 23 //好友请求 36 | #define _FRIENDSEND 24 //add friend 37 | #define _FRIENDDEL 25 //delet friend 38 | #define _USERSALL 26 39 | 40 | #define _GROUPLIST 31 //用户群列表 41 | #define _GROUPFIND 32 //查找群 42 | #define _GROUPOWN 33 //creat a group 43 | #define _GROUPSEND 33 //add group 44 | #define _GROUPDEL 34 //group quit 45 | #define _GROUPDISMISS 35 //group dismiss 46 | #define _GROUPSALL 36 47 | 48 | #define _RECALL 99 //返回到主目录 49 | #define _SYSTEMTIP 91 //system tip 50 | 51 | #define _INPUTNAME 41 52 | #define _INPUTPASSWD 42 53 | #define _INPUTSEX 43 54 | #define _INPUTFRIEND 44 55 | #define _INPUTGROUP 45 56 | 57 | #define _CHATGRO 52 58 | #define _CHATONE 51 59 | #define _LEAVEMESS 53 //查看离线消息 60 | #define _MESSTIP 54 61 | 62 | #define LOADSTART 't' 63 | #define LOADOVER 'r' 64 | #define LOADWRONG 'g' 65 | 66 | #define _UPLOAD 61 67 | #define _DOWNLOAD 62 68 | #define _LOADERR 63 69 | 70 | #define LISTENQ 12 71 | #define SERV_PORT 4507 72 | 73 | typedef struct{ 74 | char username[32]; 75 | char usersex[10]; 76 | char status; 77 | }friend_info_t; 78 | 79 | typedef struct{ 80 | char groupname[32]; 81 | char owner[32]; 82 | int groupid; 83 | int size; 84 | friend_info_t member[GROUPSIZE]; 85 | }group_info_t; 86 | 87 | typedef struct { 88 | int year; 89 | int month; 90 | int day; 91 | }user_date_t; 92 | 93 | typedef struct{ 94 | int hour; 95 | int minute; 96 | int second; 97 | }user_time_t; 98 | 99 | typedef struct{ 100 | user_date_t date; 101 | user_time_t time; 102 | char send_name[32]; 103 | char from_name[32]; 104 | char message_buf[BUFSIZE-36]; 105 | 106 | }message_t; 107 | 108 | typedef struct{ 109 | char filename[32]; 110 | char filepath[BUFSIZE]; 111 | int count; 112 | int lenth; 113 | }fileinfo_message_t; 114 | 115 | typedef struct{ 116 | 117 | int conn_fd; 118 | int flag_recv; 119 | }thread_t; 120 | 121 | typedef struct { 122 | 123 | user_date_t date; 124 | user_time_t time; 125 | }user_logtime_t; 126 | 127 | 128 | typedef struct{ 129 | 130 | char markname[32]; 131 | int filelen; 132 | int sendlen; 133 | int givelen; 134 | int leftlen; 135 | 136 | }fileinfo_t; 137 | 138 | 139 | typedef struct{ 140 | char filepath[32]; 141 | char markname[32]; 142 | int filelen; 143 | int sendlen; 144 | int givelen; 145 | int sendtimes; 146 | int leftlen; 147 | char buf[BUFSIZE]; 148 | }file_t; 149 | 150 | 151 | 152 | typedef struct{ 153 | int userid; 154 | char username[32]; 155 | char userpasswd[32]; 156 | char usersex[10]; 157 | char leavemes_flag; 158 | char status; 159 | int friendslist[FRIENDSIZE]; 160 | int grouplist[GROUPCOUNT]; 161 | int conn_fd; 162 | user_logtime_t ontime; 163 | user_logtime_t offtime; 164 | }userinfo_service_t; 165 | 166 | 167 | typedef struct{ 168 | userinfo_service_t userinfo; 169 | friend_info_t friendinfo; 170 | group_info_t group_info; 171 | user_date_t data; 172 | user_time_t time; 173 | message_t message; 174 | fileinfo_message_t messageinfo; 175 | fileinfo_t file; 176 | int conn_fd; 177 | char check_answer; 178 | int input_check; 179 | char userstatus; 180 | int command_type; 181 | char buf[BUFSIZE]; 182 | }send_t; 183 | 184 | -------------------------------------------------------------------------------- /client/common/tags: -------------------------------------------------------------------------------- 1 | !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ 2 | !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ 3 | !_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ 4 | !_TAG_PROGRAM_NAME Exuberant Ctags // 5 | !_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ 6 | !_TAG_PROGRAM_VERSION 5.8 // 7 | ACCOUNTSIZE struct.h 10;" d 8 | BUFSIZE struct.h 12;" d 9 | COMMON_H_ common.h 9;" d 10 | DateNow common.c /^user_date_t DateNow() {$/;" f 11 | FRIENDSIZE struct.h 11;" d 12 | GROUPCOUNT struct.h 14;" d 13 | GROUPSIZE struct.h 13;" d 14 | INVALID_INPUT common.c 22;" d file: 15 | INVALID_USERINFO struct.h 16;" d 16 | LISTENQ struct.h 55;" d 17 | SERV_PORT struct.h 56;" d 18 | Str2Upper common.c /^char *Str2Upper(char *str) {$/;" f 19 | TimeNow common.c /^user_time_t TimeNow(){$/;" f 20 | VALID_INPUT common.c 23;" d file: 21 | VALID_USERINFO struct.h 17;" d 22 | _FRIENDASK struct.h 35;" d 23 | _FRIENDDEL struct.h 37;" d 24 | _FRIENDFIND struct.h 34;" d 25 | _FRIENDLIST struct.h 33;" d 26 | _FRIENDSEND struct.h 36;" d 27 | _GROUPDEL struct.h 43;" d 28 | _GROUPDISMISS struct.h 44;" d 29 | _GROUPFIND struct.h 40;" d 30 | _GROUPLIST struct.h 39;" d 31 | _GROUPOWN struct.h 41;" d 32 | _GROUPSEND struct.h 42;" d 33 | _INPUTFRIEND struct.h 52;" d 34 | _INPUTGROUP struct.h 53;" d 35 | _INPUTNAME struct.h 49;" d 36 | _INPUTPASSWD struct.h 50;" d 37 | _INPUTSEX struct.h 51;" d 38 | _LEAVEMES struct.h 28;" d 39 | _LEAVEMESS struct.h 45;" d 40 | _NOMESSAGE struct.h 29;" d 41 | _OFFLINE struct.h 31;" d 42 | _ONLINE struct.h 30;" d 43 | _RECALL struct.h 47;" d 44 | _USERADD struct.h 22;" d 45 | _USERCHA struct.h 23;" d 46 | _USERDEL struct.h 24;" d 47 | _USEROFFL struct.h 26;" d 48 | _USERONL struct.h 25;" d 49 | __input common.c /^void __input(int conn_fd, char *input_buf){$/;" f 50 | cl_stdin common.c /^void cl_stdin(){$/;" f 51 | command_type struct.h /^ int command_type;$/;" m union:__anon10 52 | conn_fd struct.h /^ int conn_fd;$/;" m struct:__anon7 53 | count struct.h /^ int count;$/;" m struct:__anon6 54 | data struct.h /^ user_date_t data;$/;" m union:__anon10 55 | date struct.h /^ user_date_t date;$/;" m struct:__anon5 56 | date struct.h /^ user_date_t date;$/;" m struct:__anon8 57 | day struct.h /^ int day;$/;" m struct:__anon3 58 | file_len common.c /^int file_len(int fd){$/;" f 59 | file_read common.c /^void * file_read(int fd,int len,void* buf){$/;" f 60 | file_write common.c /^int file_write(int fd, const void * buf, int len){$/;" f 61 | fileinfo_message_t struct.h /^}fileinfo_message_t;$/;" t typeref:struct:__anon6 62 | filename struct.h /^ char filename[32];$/;" m struct:__anon6 63 | filepath struct.h /^ char filepath[BUFSIZE];$/;" m struct:__anon6 64 | flag_recv struct.h /^ int flag_recv;$/;" m struct:__anon7 65 | friend_info_t struct.h /^}friend_info_t;$/;" t typeref:struct:__anon1 66 | friendinfo struct.h /^ friend_info_t friendinfo;$/;" m union:__anon10 67 | friendslist struct.h /^ int friendslist[FRIENDSIZE];$/;" m struct:__anon9 68 | getch common.c /^int getch(void){$/;" f 69 | getpwd common.c /^void getpwd(int maxlen, char *pwd){$/;" f 70 | group_info struct.h /^ group_info_t group_info;$/;" m union:__anon10 71 | group_info_t struct.h /^}group_info_t;$/;" t typeref:struct:__anon2 72 | groupid struct.h /^ int groupid;$/;" m struct:__anon2 73 | grouplist struct.h /^ int grouplist[GROUPCOUNT];$/;" m struct:__anon9 74 | groupname struct.h /^ char groupname[32];$/;" m struct:__anon2 75 | hour struct.h /^ int hour;$/;" m struct:__anon4 76 | input_ common.c /^void input_(int conn_fd, const char *string, int len){$/;" f 77 | input_check common.c /^int input_check(int input_len, char * input_buf){$/;" f 78 | input_check struct.h /^ char input_check;$/;" m union:__anon10 79 | l_getc common.c /^char l_getc(){$/;" f 80 | leavemes_flag struct.h /^ char leavemes_flag;$/;" m struct:__anon9 81 | lenth struct.h /^ int lenth;$/;" m struct:__anon6 82 | main shiyi.c /^int main()$/;" f 83 | member struct.h /^ friend_info_t member[GROUPSIZE];$/;" m struct:__anon2 84 | message struct.h /^ message_t message;$/;" m union:__anon10 85 | message_buf struct.h /^ char message_buf[BUFSIZE-36];$/;" m struct:__anon5 86 | message_t struct.h /^}message_t;$/;" t typeref:struct:__anon5 87 | messageinfo struct.h /^ fileinfo_message_t messageinfo;$/;" m union:__anon10 88 | minute struct.h /^ int minute;$/;" m struct:__anon4 89 | month struct.h /^ int month;$/;" m struct:__anon3 90 | my_err common.c /^void my_err(const char* err_string, int line){$/;" f 91 | my_recv common.c /^int my_recv(int conn_fd, char *data_buf, int len){$/;" f 92 | offtime struct.h /^ user_logtime_t offtime;$/;" m struct:__anon9 93 | ontime struct.h /^ user_logtime_t ontime;$/;" m struct:__anon9 94 | owner struct.h /^ char owner[32];$/;" m struct:__anon2 95 | pthread struct.h /^ thread_t pthread;$/;" m union:__anon10 96 | recv_message common.c /^void recv_message(int conn_fd, message_t * data){$/;" f 97 | second struct.h /^ int second;$/;" m struct:__anon4 98 | send_data common.c /^int send_data(int conn_fd,char *string){$/;" f 99 | send_message common.c /^void send_message(int conn_fd, char * send_name){$/;" f 100 | send_name struct.h /^ char send_name[32];$/;" m struct:__anon5 101 | send_t struct.h /^}send_t;$/;" t typeref:union:__anon10 102 | ser_atoi common.c /^int ser_atoi(char * buf){ \/\/atoi$/;" f 103 | show_message common.c /^void show_message(int conn_fd){$/;" f 104 | show_recv common.c /^void show_recv(int conn_fd, char *recv_buf, int buflen ){$/;" f 105 | size struct.h /^ int size;$/;" m struct:__anon2 106 | status struct.h /^ char status;$/;" m struct:__anon9 107 | status struct.h /^ char status[5];$/;" m struct:__anon1 108 | thread shiyi.c /^int* thread(char s[])$/;" f 109 | thread_t struct.h /^}thread_t;$/;" t typeref:struct:__anon7 110 | time struct.h /^ user_time_t time;$/;" m struct:__anon5 111 | time struct.h /^ user_time_t time;$/;" m struct:__anon8 112 | time struct.h /^ user_time_t time;$/;" m union:__anon10 113 | user_date_t struct.h /^}user_date_t;$/;" t typeref:struct:__anon3 114 | user_logtime_t struct.h /^}user_logtime_t;$/;" t typeref:struct:__anon8 115 | user_time_t struct.h /^}user_time_t;$/;" t typeref:struct:__anon4 116 | userid struct.h /^ int userid;$/;" m struct:__anon9 117 | userinfo struct.h /^ userinfo_service_t userinfo;$/;" m union:__anon10 118 | userinfo_service_t struct.h /^}userinfo_service_t;$/;" t typeref:struct:__anon9 119 | username struct.h /^ char username[32];$/;" m struct:__anon1 120 | username struct.h /^ char username[32];$/;" m struct:__anon9 121 | username struct.h /^ char username[32];$/;" m union:__anon10 122 | userpasswd struct.h /^ char userpasswd[32];$/;" m struct:__anon9 123 | usersex struct.h /^ char usersex[10];$/;" m struct:__anon1 124 | usersex struct.h /^ char usersex[10];$/;" m struct:__anon9 125 | userstatus struct.h /^ char userstatus;$/;" m union:__anon10 126 | year struct.h /^ int year;$/;" m struct:__anon3 127 | -------------------------------------------------------------------------------- /client/file.c: -------------------------------------------------------------------------------- 1 | #include"client.h" 2 | 3 | int file_ui_init(int conn_fd, char* username) 4 | { 5 | char choice; 6 | send_t send_buf; 7 | send_t *psend = &send_buf; 8 | 9 | do{ 10 | print(); 11 | puts(" "); 12 | puts("\033[33mwhat do you want?\033[0m"); 13 | puts("downloads"); 14 | puts("upload"); 15 | puts("return"); 16 | puts("\033[33m.-.-.-.-.-.-.-.-.-.--.-.\033[0m"); 17 | choice = l_getc(); 18 | 19 | switch(choice){ 20 | 21 | case 'u': 22 | case 'U': 23 | upload_file(conn_fd, username); 24 | break; 25 | case 'd': 26 | case 'D': 27 | cl_stdin(); 28 | puts("input the file markname"); 29 | printf("markname:"); 30 | input_check(32, psend->file.markname); 31 | puts(" "); 32 | 33 | psend->command_type = _DOWNLOAD; 34 | psend->conn_fd = conn_fd; 35 | strcpy(psend->userinfo.username, username); 36 | send_data(psend->conn_fd, psend); 37 | break; 38 | } 39 | }while(choice != 'r' && choice != 'R'); 40 | return 0; 41 | } 42 | 43 | int upload_file(int conn_fd, char* username){ 44 | 45 | int fd; 46 | char *temp_buf; 47 | file_t file_buf; 48 | file_t *pfile = &file_buf; 49 | send_t send_buf; 50 | send_t *psend = &send_buf; 51 | 52 | temp_buf = (char*) malloc (1024*1024); 53 | 54 | print(); 55 | puts("input file name with absolute path"); 56 | printf("filepath:"); 57 | cl_stdin(); 58 | input_check(32, pfile->filepath); 59 | 60 | fd = open(pfile->filepath, O_RDWR ); 61 | if(fd < 0){ 62 | puts("wrong path or file not exist"); 63 | return 0; 64 | } 65 | 66 | //get the file information , size or ... 67 | pfile->filelen = file_len(fd); 68 | pfile->leftlen = file_len(fd); 69 | pfile->givelen = 0; 70 | 71 | file_read(fd, pfile->filelen, temp_buf); 72 | 73 | puts(""); 74 | puts(":file information below"); 75 | printf("filepath:\033[33m%s\033[0m\n", pfile->filepath); 76 | printf("size:\033[33m%d\033[0mB\n\n", pfile->filelen); 77 | 78 | if(pfile->filelen < 0){ 79 | my_err("<\033[31msystem\033[0m>:wrong path or file not exist", __LINE__); 80 | } 81 | else{ 82 | puts("set your file a markname for confidential"); 83 | printf("markname:"); 84 | input_check(32, pfile->markname); 85 | //copy file into send_buf; 86 | 87 | psend->userstatus = LOADSTART; 88 | 89 | print(); 90 | while(1){ 91 | 92 | strcpy(psend->file.markname, pfile->markname); 93 | psend->file.filelen = pfile->filelen; 94 | psend->command_type = _UPLOAD; 95 | 96 | psend->conn_fd = conn_fd; 97 | strcpy(psend->userinfo.username, username); 98 | 99 | //start upload 100 | if(psend->userstatus == LOADSTART){ 101 | 102 | pfile->sendlen = send_data(conn_fd,psend); 103 | //change flag 104 | psend->userstatus = VALID_USERINFO; 105 | 106 | if(pfile->sendlen < 0){ 107 | my_err("upload faild", __LINE__); 108 | } 109 | sleep(1); 110 | continue; 111 | } 112 | 113 | //no means 114 | if((pfile->leftlen <= 0) || (pfile->givelen >= pfile->filelen)){ 115 | break; 116 | } 117 | 118 | //when not the last time 119 | if(pfile->leftlen >= BUFSIZE){ 120 | psend->file.givelen = pfile->givelen; 121 | memcpy(psend->buf, temp_buf + pfile->givelen, BUFSIZE); 122 | psend->file.sendlen = BUFSIZE; 123 | pfile->sendlen = send_data(conn_fd, psend); 124 | 125 | if(pfile->sendlen <= 0){ 126 | my_err("upload faild", __LINE__); 127 | //can make mark; 128 | } 129 | } 130 | else{ 131 | memcpy(psend->buf, temp_buf+pfile->givelen, pfile->leftlen); 132 | psend->userstatus = LOADOVER; 133 | psend->file.givelen = pfile->givelen + pfile->leftlen; 134 | psend->file.sendlen = pfile->leftlen; 135 | pfile->sendlen = send_data(conn_fd, psend); 136 | 137 | if(pfile->sendlen <= 0){ 138 | my_err("upload faild", __LINE__); 139 | } 140 | pfile->givelen = pfile->givelen + pfile->leftlen; 141 | 142 | break; 143 | } 144 | 145 | if(pfile->givelen < pfile->filelen ){ 146 | //设置光标位置 147 | //清除从光标到行尾的内容 148 | printf("sended \033[33m%d\033[0mB , filelen: \033[33m%d\033[0mB\n", pfile->givelen, pfile->filelen); 149 | printf("\033[1A"); 150 | printf("\033[K"); 151 | } 152 | 153 | usleep(100000); 154 | pfile->givelen += BUFSIZE; 155 | pfile->leftlen = pfile->filelen - pfile->givelen; 156 | 157 | }//while 158 | 159 | close(fd); 160 | free(temp_buf); 161 | if(pfile->givelen == pfile->filelen){ 162 | 163 | puts("100%"); 164 | puts("upload over"); 165 | return 0; 166 | } 167 | else{ 168 | my_err("upload faild", __LINE__); 169 | } 170 | }//else 171 | return 0; 172 | } 173 | 174 | int download_file(send_t *precv){ 175 | 176 | int ret; 177 | int fd; 178 | file_t file_buf; 179 | file_t *pfile = &file_buf; 180 | 181 | //maskname 判断 182 | if(precv->userstatus == _LOADERR){ 183 | puts("wrong markname"); 184 | return 0; 185 | } 186 | 187 | fd = open(precv->file.markname, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP); 188 | if(fd < 0){ 189 | my_err("file_open", __LINE__); 190 | } 191 | // 开始标志 192 | if(precv->userstatus == LOADSTART){ 193 | puts("the file information below:"); 194 | printf("name:\033[33m%s\033[0m\n", precv->file.markname); 195 | printf("size:\033[33m%dB\033[0m\n", precv->file.filelen); 196 | puts("start recving the file..."); 197 | puts(" "); 198 | }else{ 199 | 200 | ret = file_write(fd, precv->buf, precv->file.sendlen ); 201 | pfile->givelen = file_len(fd); 202 | printf("recvlen:\033[33m%d\033[0mB filelen:\033[33m%d\033[0m\n", pfile->givelen, precv->file.filelen); 203 | printf("\033[1A"); 204 | printf("\033[K"); 205 | 206 | if(precv->userstatus == LOADOVER){ 207 | 208 | // 209 | if(pfile->givelen == precv->file.filelen){ 210 | puts("download over"); 211 | } 212 | else{ 213 | my_err("download failed", __LINE__); 214 | } 215 | } 216 | } 217 | close(fd); 218 | return 0; 219 | } 220 | -------------------------------------------------------------------------------- /client/file.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/client/file.o -------------------------------------------------------------------------------- /client/tags: -------------------------------------------------------------------------------- 1 | !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ 2 | !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ 3 | !_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ 4 | !_TAG_PROGRAM_NAME Exuberant Ctags // 5 | !_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ 6 | !_TAG_PROGRAM_VERSION 5.8 // 7 | ACCOUNTSIZE common/struct.h 10;" d 8 | BUFSIZE common/struct.h 12;" d 9 | CC Makefile /^CC = gcc$/;" m 10 | CLIENT_H client.h 9;" d 11 | COMMON_H_ common/common.h 9;" d 12 | C_FLAGS Makefile /^C_FLAGS = -c.$/;" m 13 | C_OBJECTS Makefile /^C_OBJECTS = $(patsubst %.c, %.o, $(C_SOURCES))$/;" m 14 | C_SOURCES Makefile /^C_SOURCES = $(shell find . -name "*.c")$/;" m 15 | DateNow common/common.c /^user_date_t DateNow() {$/;" f 16 | FRIENDSIZE common/struct.h 11;" d 17 | GROUPCOUNT common/struct.h 14;" d 18 | GROUPSIZE common/struct.h 13;" d 19 | INVALID_USERINFO common/struct.h 16;" d 20 | LD Makefile /^LD = ld$/;" m 21 | LEAVE_MESSAGE_IN client.h /^ LEAVE_MESSAGE_IN = 1,$/;" e enum:__anon14 22 | LEAVE_MESSAGE_NO client.h /^ LEAVE_MESSAGE_NO = 2$/;" e enum:__anon14 23 | LISTENQ common/struct.h 70;" d 24 | LOADOVER common/struct.h 63;" d 25 | LOADSTART common/struct.h 62;" d 26 | LOADWRONG common/struct.h 64;" d 27 | PROGRAM Makefile /^PROGRAM = Client$/;" m 28 | SERV_PORT common/struct.h 71;" d 29 | Str2Upper common/common.c /^char *Str2Upper(char *str) {$/;" f 30 | TimeNow common/common.c /^user_time_t TimeNow(){$/;" f 31 | USER_FEMALE client.h /^ USER_FEMALE = 1,$/;" e enum:__anon13 32 | USER_FILE account_per.c 10;" d file: 33 | USER_MAN client.h /^ USER_MAN = 2$/;" e enum:__anon13 34 | VALID_USERINFO common/struct.h 17;" d 35 | _CHATGRO common/struct.h 57;" d 36 | _CHATONE common/struct.h 58;" d 37 | _DOWNLOAD common/struct.h 67;" d 38 | _FRIENDASK common/struct.h 35;" d 39 | _FRIENDDEL common/struct.h 37;" d 40 | _FRIENDFIND common/struct.h 34;" d 41 | _FRIENDLIST common/struct.h 33;" d 42 | _FRIENDSEND common/struct.h 36;" d 43 | _GROUPDEL common/struct.h 44;" d 44 | _GROUPDISMISS common/struct.h 45;" d 45 | _GROUPFIND common/struct.h 41;" d 46 | _GROUPLIST common/struct.h 40;" d 47 | _GROUPOWN common/struct.h 42;" d 48 | _GROUPSALL common/struct.h 46;" d 49 | _GROUPSEND common/struct.h 43;" d 50 | _INPUTFRIEND common/struct.h 54;" d 51 | _INPUTGROUP common/struct.h 55;" d 52 | _INPUTNAME common/struct.h 51;" d 53 | _INPUTPASSWD common/struct.h 52;" d 54 | _INPUTSEX common/struct.h 53;" d 55 | _LEAVEMES common/struct.h 28;" d 56 | _LEAVEMESS common/struct.h 59;" d 57 | _LOADERR common/struct.h 68;" d 58 | _MESSTIP common/struct.h 60;" d 59 | _NOMESSAGE common/struct.h 29;" d 60 | _OFFLINE common/struct.h 31;" d 61 | _ONLINE common/struct.h 30;" d 62 | _RECALL common/struct.h 48;" d 63 | _SYSTEMTIP common/struct.h 49;" d 64 | _UPLOAD common/struct.h 66;" d 65 | _USERADD common/struct.h 22;" d 66 | _USERCHA common/struct.h 23;" d 67 | _USERDEL common/struct.h 24;" d 68 | _USEROFFL common/struct.h 26;" d 69 | _USERONL common/struct.h 25;" d 70 | _USERSALL common/struct.h 38;" d 71 | account_ui_delete account_u.c /^void account_ui_delete(int conn_fd,char *username){$/;" f 72 | account_ui_init account_u.c /^void account_ui_init(int conn_fd,char *username){$/;" f 73 | account_ui_logoff account_u.c /^void account_ui_logoff(int conn_fd,char *username){$/;" f 74 | account_ui_update account_u.c /^void account_ui_update(int conn_fd, char *username){$/;" f 75 | buf common/struct.h /^ char buf[BUFSIZE];$/;" m struct:__anon10 76 | buf common/struct.h /^ char buf[BUFSIZE];$/;" m struct:__anon12 77 | chat_friend_add chat.c /^int chat_friend_add(send_t *precv){$/;" f 78 | chat_friend_del chat.c /^int chat_friend_del(send_t *precv){$/;" f 79 | chat_friend_show chat.c /^int chat_friend_show(send_t * precv){$/;" f 80 | chat_group_add chat.c /^int chat_group_add(send_t *precv){$/;" f 81 | chat_group_show chat.c /^int chat_group_show(send_t *precv){$/;" f 82 | chat_user_init chat.c /^int chat_user_init(int conn_fd, char*username)$/;" f 83 | check_answer common/struct.h /^ char check_answer;$/;" m struct:__anon12 84 | cl_stdin common/common.c /^void cl_stdin(){$/;" f 85 | client client.c /^void client(send_t * precv){$/;" f 86 | command_type common/struct.h /^ int command_type;$/;" m struct:__anon12 87 | conn_fd common/struct.h /^ int conn_fd;$/;" m struct:__anon11 88 | conn_fd common/struct.h /^ int conn_fd;$/;" m struct:__anon12 89 | conn_fd common/struct.h /^ int conn_fd;$/;" m struct:__anon7 90 | count common/struct.h /^ int count;$/;" m struct:__anon6 91 | creat_userdir account_per.c /^int creat_userdir(const char *username){$/;" f 92 | data common/struct.h /^ user_date_t data;$/;" m struct:__anon12 93 | date common/struct.h /^ user_date_t date;$/;" m struct:__anon5 94 | date common/struct.h /^ user_date_t date;$/;" m struct:__anon8 95 | day common/struct.h /^ int day;$/;" m struct:__anon3 96 | download_file file.c /^int download_file(send_t *precv){$/;" f 97 | file common/struct.h /^ fileinfo_t file;$/;" m struct:__anon12 98 | file_len common/common.c /^int file_len(int fd){$/;" f 99 | file_read common/common.c /^int file_read(int fd,int len,void* buf){$/;" f 100 | file_t common/struct.h /^}file_t;$/;" t typeref:struct:__anon10 101 | file_ui_init file.c /^int file_ui_init(int conn_fd, char* username)$/;" f 102 | file_write common/common.c /^int file_write(int fd, const void * buf, int len){$/;" f 103 | fileinfo_message_t common/struct.h /^}fileinfo_message_t;$/;" t typeref:struct:__anon6 104 | fileinfo_t common/struct.h /^}fileinfo_t;$/;" t typeref:struct:__anon9 105 | filelen common/struct.h /^ int filelen;$/;" m struct:__anon9 106 | filelen common/struct.h /^ int filelen;$/;" m struct:__anon10 107 | filename common/struct.h /^ char filename[32];$/;" m struct:__anon6 108 | filepath common/struct.h /^ char filepath[32];$/;" m struct:__anon10 109 | filepath common/struct.h /^ char filepath[BUFSIZE];$/;" m struct:__anon6 110 | flag_leave client.h /^ userinfo_leavemes_t flag_leave;$/;" m struct:__anon15 111 | flag_recv common/struct.h /^ int flag_recv;$/;" m struct:__anon7 112 | friend_chat_add chat.c /^int friend_chat_add(int conn_fd, char *username){$/;" f 113 | friend_chat_ask chat.c /^int friend_chat_ask(send_t* precv){$/;" f 114 | friend_chat_one chat.c /^int friend_chat_one(int conn_fd, char *username){$/;" f 115 | friend_chat_send chat.c /^int friend_chat_send(int conn_fd, char *username){$/;" f 116 | friend_chat_show chat.c /^int friend_chat_show(int conn_fd, char* username){$/;" f 117 | friend_find_add chat.c /^int friend_find_add(send_t *precv){$/;" f 118 | friend_info_t common/struct.h /^}friend_info_t;$/;" t typeref:struct:__anon1 119 | friendinfo common/struct.h /^ friend_info_t friendinfo;$/;" m struct:__anon12 120 | friendslist common/struct.h /^ int friendslist[FRIENDSIZE];$/;" m struct:__anon11 121 | from_name common/struct.h /^ char from_name[32];$/;" m struct:__anon5 122 | getch common/common.c /^int getch(void){$/;" f 123 | getpwd common/common.c /^void getpwd(int maxlen, char *pwd){$/;" f 124 | givelen common/struct.h /^ int givelen;$/;" m struct:__anon9 125 | givelen common/struct.h /^ int givelen;$/;" m struct:__anon10 126 | group_chat_add chat.c /^int group_chat_add(int conn_fd, char *username){$/;" f 127 | group_chat_show chat.c /^int group_chat_show(int conn_fd, char* username){$/;" f 128 | group_find_add chat.c /^int group_find_add(send_t *precv){$/;" f 129 | group_info common/struct.h /^ group_info_t group_info;$/;" m struct:__anon12 130 | group_info_t common/struct.h /^}group_info_t;$/;" t typeref:struct:__anon2 131 | groupid common/struct.h /^ int groupid;$/;" m struct:__anon2 132 | grouplist common/struct.h /^ int grouplist[GROUPCOUNT];$/;" m struct:__anon11 133 | groupname common/struct.h /^ char groupname[32];$/;" m struct:__anon2 134 | h_username client.c /^char h_username[32];$/;" v 135 | hour common/struct.h /^ int hour;$/;" m struct:__anon4 136 | id client.h /^ char id[5];$/;" m struct:__anon15 137 | input_check common/common.c /^int input_check(int input_len, char * input_buf){$/;" f 138 | input_check common/struct.h /^ int input_check;$/;" m struct:__anon12 139 | l_getc common/common.c /^char l_getc(){$/;" f 140 | leavemes_flag common/struct.h /^ char leavemes_flag;$/;" m struct:__anon11 141 | leftlen common/struct.h /^ int leftlen;$/;" m struct:__anon9 142 | leftlen common/struct.h /^ int leftlen;$/;" m struct:__anon10 143 | lenth common/struct.h /^ int lenth;$/;" m struct:__anon6 144 | main client.c /^int main(){$/;" f 145 | markname common/struct.h /^ char markname[32];$/;" m struct:__anon9 146 | markname common/struct.h /^ char markname[32];$/;" m struct:__anon10 147 | member common/struct.h /^ friend_info_t member[GROUPSIZE];$/;" m struct:__anon2 148 | message common/struct.h /^ message_t message;$/;" m struct:__anon12 149 | message_box chat.c /^int message_box(int conn_fd, char* username, int type){$/;" f 150 | message_buf common/struct.h /^ char message_buf[BUFSIZE-36];$/;" m struct:__anon5 151 | message_init chat.c /^int message_init(int conn_fd, char *username){$/;" f 152 | message_p_all chat.c /^int message_p_all(message_t *box, int type){$/;" f 153 | message_p_insert chat.c /^int message_p_insert(message_t * mes){$/;" f 154 | message_p_selet chat.c /^int message_p_selet(int type, message_t * mes){$/;" f 155 | message_t common/struct.h /^}message_t;$/;" t typeref:struct:__anon5 156 | message_tips chat.c /^int message_tips(send_t *precv){$/;" f 157 | messageinfo common/struct.h /^ fileinfo_message_t messageinfo;$/;" m struct:__anon12 158 | minute common/struct.h /^ int minute;$/;" m struct:__anon4 159 | month common/struct.h /^ int month;$/;" m struct:__anon3 160 | my_err common/common.c /^void my_err(const char* err_string, int line){$/;" f 161 | offtime common/struct.h /^ user_logtime_t offtime;$/;" m struct:__anon11 162 | ontime common/struct.h /^ user_logtime_t ontime;$/;" m struct:__anon11 163 | owner common/struct.h /^ char owner[32];$/;" m struct:__anon2 164 | print client.c /^int print(){$/;" f 165 | recv_data common/common.c /^int recv_data(int conn_fd, send_t *precv){$/;" f 166 | register_ui client.c /^int register_ui(int conn_fd){$/;" f 167 | second common/struct.h /^ int second;$/;" m struct:__anon4 168 | send_data common/common.c /^int send_data(int conn_fd, send_t * psend){$/;" f 169 | send_name common/struct.h /^ char send_name[32];$/;" m struct:__anon5 170 | send_t common/struct.h /^}send_t;$/;" t typeref:struct:__anon12 171 | sendlen common/struct.h /^ int sendlen;$/;" m struct:__anon9 172 | sendlen common/struct.h /^ int sendlen;$/;" m struct:__anon10 173 | sendtimes common/struct.h /^ int sendtimes;$/;" m struct:__anon10 174 | ser_atoi common/common.c /^int ser_atoi(char * buf){ \/\/atoi$/;" f 175 | sex client.h /^ userinfo_sex_t sex;$/;" m struct:__anon15 176 | signin_ui client.c /^int signin_ui(int conn_fd){$/;" f 177 | size common/struct.h /^ int size;$/;" m struct:__anon2 178 | status common/struct.h /^ char status;$/;" m struct:__anon1 179 | status common/struct.h /^ char status;$/;" m struct:__anon11 180 | thread_fun client.c /^int * thread_fun(int conn_fd){$/;" f 181 | thread_t common/struct.h /^}thread_t;$/;" t typeref:struct:__anon7 182 | time common/struct.h /^ user_time_t time;$/;" m struct:__anon12 183 | time common/struct.h /^ user_time_t time;$/;" m struct:__anon5 184 | time common/struct.h /^ user_time_t time;$/;" m struct:__anon8 185 | upload_file file.c /^int upload_file(int conn_fd, char* username){$/;" f 186 | user_chat_one chat.c /^int user_chat_one(send_t *precv){$/;" f 187 | user_date_t common/struct.h /^}user_date_t;$/;" t typeref:struct:__anon3 188 | user_init_ client.c /^int user_init_(int conn_fd){$/;" f 189 | user_logtime_t common/struct.h /^}user_logtime_t;$/;" t typeref:struct:__anon8 190 | user_time_t common/struct.h /^}user_time_t;$/;" t typeref:struct:__anon4 191 | userid common/struct.h /^ int userid;$/;" m struct:__anon11 192 | userinfo common/struct.h /^ userinfo_service_t userinfo;$/;" m struct:__anon12 193 | userinfo_leavemes_t client.h /^}userinfo_leavemes_t;$/;" t typeref:enum:__anon14 194 | userinfo_p_checkfile account_per.c /^int userinfo_p_checkfile(const char *username){$/;" f 195 | userinfo_p_delet account_per.c /^int userinfo_p_delet(const char * username){$/;" f 196 | userinfo_p_insert account_per.c /^int userinfo_p_insert(const userinfo_t * data){$/;" f 197 | userinfo_p_seletid account_per.c /^int userinfo_p_seletid(int userid, userinfo_t * data){$/;" f 198 | userinfo_p_seletname account_per.c /^int userinfo_p_seletname(const char* username, userinfo_t * data){$/;" f 199 | userinfo_s_add account_ser.c /^int userinfo_s_add(userinfo_t *data){$/;" f 200 | userinfo_s_delet account_ser.c /^int userinfo_s_delet(const char *username)$/;" f 201 | userinfo_s_login account_ser.c /^int userinfo_s_login(const char *username){$/;" f 202 | userinfo_s_select account_ser.c /^int userinfo_s_select(const char *username, userinfo_t *data)$/;" f 203 | userinfo_s_updata account_ser.c /^int userinfo_s_updata(userinfo_t *data, const char* username)$/;" f 204 | userinfo_service_t common/struct.h /^}userinfo_service_t;$/;" t typeref:struct:__anon11 205 | userinfo_sex_t client.h /^}userinfo_sex_t;$/;" t typeref:enum:__anon13 206 | userinfo_t client.h /^}userinfo_t;$/;" t typeref:struct:__anon15 207 | userinfo_ui_init account_u.c /^void userinfo_ui_init(int conn_fd, char *username){$/;" f 208 | username client.h /^ char username[32];$/;" m struct:__anon15 209 | username common/struct.h /^ char username[32];$/;" m struct:__anon1 210 | username common/struct.h /^ char username[32];$/;" m struct:__anon11 211 | userpasswd client.h /^ char userpasswd[32];$/;" m struct:__anon15 212 | userpasswd common/struct.h /^ char userpasswd[32];$/;" m struct:__anon11 213 | usersex common/struct.h /^ char usersex[10];$/;" m struct:__anon1 214 | usersex common/struct.h /^ char usersex[10];$/;" m struct:__anon11 215 | userstatus common/struct.h /^ char userstatus;$/;" m struct:__anon12 216 | year common/struct.h /^ int year;$/;" m struct:__anon3 217 | -------------------------------------------------------------------------------- /client/temp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/client/temp -------------------------------------------------------------------------------- /client/temp1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/client/temp1 -------------------------------------------------------------------------------- /client/temp1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | //#include "/usr/include/c++/4.8.2/iostream" 11 | using namespace std; 12 | /* 13 | typedef union epoll_data 14 | { 15 | void *ptr; 16 | int fd; 17 | __uint32_t u32; 18 | __uint64_t u64; 19 | } epoll_data_t; 20 | 21 | struct epoll_event 22 | { 23 | __uint32_t events; // Epoll events / 24 | epoll_data_t data; // User data variable / 25 | }; 26 | */ 27 | int num = 0; 28 | #define MAX_EVENTS 10 29 | struct epoll_event ev, clientevents[MAX_EVENTS],events[MAX_EVENTS+1]; 30 | int listenfd, connfd, nfds, epollfd; 31 | void setnonblocking(int sock) 32 | { 33 | int opts; 34 | opts=fcntl(sock,F_GETFL); 35 | if(opts<0) 36 | { 37 | perror("fcntl(sock,GETFL)"); 38 | //exit(0); 39 | } 40 | opts = opts|O_NONBLOCK; 41 | if(fcntl(sock,F_SETFL,opts)<0) 42 | { 43 | perror("fcntl(sock,SETFL,opts)"); 44 | // exit(1); 45 | } 46 | } 47 | //初始化event,data.u32为0表示未使用 48 | void InitEvents() 49 | { 50 | int i; 51 | for(i=0;i0) 117 | printf("nfds:%d\n",nfds); 118 | 119 | for(i=0;i 10 | #include 11 | #include 12 | 13 | // ---- 设置窗口大小 14 | void CMyConsole::SetSize(short len, short wid) // 设置同样的缓冲区和窗口大小,不会出现滚动条 15 | { 16 | COORD pos = {len, wid}; 17 | SMALL_RECT rc = {0, 0, len-1, wid-1}; // 坐标从0开始 18 | 19 | // -- 设置窗口信息 20 | // @param HANDLE [in] 窗口句柄 21 | // @param bool [in] 意思不明,但在true时才起作用 22 | // @param RECT * [in] 分别指定窗口左上角坐标和右下角坐标 23 | // #return bool 成功返回非0值 24 | SetConsoleWindowInfo(hOut, true, &rc); 25 | 26 | // -- 设置缓冲区大小 27 | // -- 长和宽不得小于控制台大小;不得小于系统最小限制。否则设置无效 28 | // @param HANDLE [in] 窗口句柄 29 | // @param COORD [in] 坐标结构,包含长和宽 30 | // #return bool 成功返回非0值 31 | SetConsoleScreenBufferSize(hOut, pos); 32 | } 33 | 34 | // ---- 设置光标位置 35 | void CMyConsole::Goto(short x, short y) 36 | { 37 | COORD pos = {x, y}; 38 | 39 | SetConsoleCursorPosition(hOut, pos); 40 | } 41 | 42 | int main() 43 | { 44 | CMyConsole myConsole; 45 | 46 | myConsole.SetSize(60, 30); 47 | myConsole.Goto(18, 14); 48 | printf("设置完毕!尺寸大小:60*30!\n"); 49 | myConsole.Goto(0, 0); 50 | system("pause"); 51 | 52 | return 0; 53 | } 54 | 55 | -------------------------------------------------------------------------------- /common/common.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-05 08:49 5 | * Filename : common.c 6 | * Description : 7 | * *****************************************************************************/ 8 | #include"common.h" 9 | 10 | 11 | char l_getc(){ 12 | char ch; 13 | scanf("%c",&ch); 14 | while(ch == '\n'){ 15 | scanf("%c",&ch); 16 | } 17 | return ch; 18 | } 19 | 20 | //Linux没有getch() 21 | 22 | int getch(void){ 23 | int c= 0; 24 | struct termios org_opts, new_opts; 25 | int res = 0; 26 | 27 | //将之前的设置备份 28 | res = tcgetattr(STDIN_FILENO, &org_opts); 29 | assert(res == 0); 30 | 31 | //设置新的设定 32 | memcpy(&new_opts, &org_opts, sizeof(new_opts)); 33 | new_opts.c_lflag &= ~(ICANON |ECHO | ECHOE | ECHOK |ECHONL | ECHOKE | ICRNL); 34 | tcsetattr(STDIN_FILENO, TCSANOW, &new_opts); 35 | c= getchar(); 36 | 37 | //恢复之前的设定 38 | res = tcsetattr(STDIN_FILENO, TCSANOW, &org_opts); 39 | assert(res == 0); 40 | return c; 41 | } 42 | 43 | //密码输入 44 | 45 | void getpwd(int maxlen, char *pwd){ 46 | int j= 0; 47 | char c; 48 | 49 | while((j < maxlen-2) && (c = getch()) != '\n'){ 50 | if(c != 127){ 51 | printf("*"); 52 | pwd[j++] = c; 53 | } 54 | else { 55 | if(j > 0){ 56 | j = j - 1; 57 | printf("\b \b"); 58 | } 59 | } 60 | } 61 | pwd[j++] = '\0'; 62 | pwd[j++] = '\n'; 63 | 64 | puts(" "); 65 | } 66 | 67 | void cl_stdin(){ 68 | char ch; 69 | while(( ch = getchar()) != '\n' && ch != EOF); 70 | } 71 | 72 | //合法输入 73 | int input_check(int input_len, char * input_buf){ 74 | char c; 75 | int i = 0; 76 | if(input_buf == NULL){ 77 | my_err("input check", __LINE__); 78 | } 79 | 80 | while((c = getchar()) != '\n' && (c != EOF) && (i < input_len-2)){ 81 | input_buf[i++] = c; 82 | } 83 | 84 | input_buf[i++] = '\0'; 85 | input_buf[i++] = '\n'; 86 | 87 | return i; 88 | } 89 | 90 | 91 | 92 | //自定义错误函数 93 | void my_err(const char* err_string, int line){ 94 | fprintf(stderr, "line:%d \n", line); 95 | perror(err_string); 96 | } 97 | 98 | //从套接字上读取一次数据 99 | 100 | int recv_data(int conn_fd, send_t *precv){ 101 | int len_remain = 0; 102 | 103 | len_remain = recv(conn_fd, precv, sizeof(send_t), 0); 104 | if(len_remain < 0){ 105 | my_err("recv", __LINE__); 106 | } 107 | 108 | printf("%d recv\n", precv->command_type); 109 | return len_remain; 110 | } 111 | 112 | 113 | //文件写入函数 114 | 115 | int file_write(int fd, const void * buf, int len){ 116 | int we; 117 | 118 | we = write(fd , buf, len); 119 | if(we < 0){ 120 | my_err("file_write", __LINE__); 121 | } 122 | 123 | return we; 124 | } 125 | 126 | //获取文件长度 127 | int file_len(int fd){ 128 | 129 | int length = 0; 130 | if(lseek(fd ,0 ,SEEK_END)== -1){ 131 | my_err("lseek set", __LINE__); 132 | } 133 | if((length = lseek(fd , 0 , SEEK_CUR)) == -1 ){ 134 | my_err("length" , __LINE__); 135 | } 136 | if(lseek(fd , 0 ,SEEK_SET)== -1){ 137 | my_err("lseek end" , __LINE__); 138 | } 139 | return length; 140 | } 141 | 142 | 143 | int file_read(int fd,int len,void* buf){ 144 | int re; 145 | if((re = read(fd , buf , len)) == -1){ 146 | my_err("c_read", __LINE__); 147 | } 148 | return re; 149 | } 150 | 151 | //显示接收信息 152 | /* 153 | void show_recv(int conn_fd, char *recv_buf, int buflen ){ 154 | int i; 155 | int ret; 156 | 157 | ret = my_recv(conn_fd, recv_buf, buflen); 158 | if(ret < 0){ 159 | my_err("recv faild", __LINE__); 160 | } 161 | 162 | for(i = 0; i < ret; i++){ 163 | printf("%c", recv_buf[i]); 164 | } 165 | } 166 | */ 167 | //获取系统当前日期 168 | user_date_t DateNow() { 169 | user_date_t curDate; 170 | time_t now; //实例化time_t结构 171 | struct tm *timeNow; //实例化tm结构指针 172 | time(&now); 173 | timeNow = localtime(&now); 174 | curDate.year=timeNow->tm_year+1900; 175 | curDate.month=timeNow->tm_mon+1; 176 | curDate.day=timeNow->tm_mday; 177 | 178 | return curDate; 179 | } 180 | 181 | //获取系统当前时间 182 | user_time_t TimeNow(){ 183 | user_time_t curTime; 184 | time_t now; //实例化time_t结构 185 | struct tm *timeNow; //实例化tm结构指针 186 | time(&now); 187 | timeNow = localtime(&now); 188 | curTime.hour=timeNow->tm_hour; 189 | curTime.minute=timeNow->tm_min; 190 | curTime.second=timeNow->tm_sec; 191 | 192 | return curTime; 193 | } 194 | 195 | int ser_atoi(char * buf){ //atoi 196 | 197 | int ret; 198 | ret = atoi(buf); 199 | 200 | return ret; 201 | } 202 | /* 203 | int recv_data(int conn_fd, send_t * psend){ 204 | int n = 0; 205 | send_t recv_buf; 206 | char *precv = (char*) &recv_buf; 207 | 208 | if(n == 0){ 209 | n = recv(conn_fd, &recv_buf, sizeof(recv_buf), 0); 210 | if(n <= 0){ 211 | my_err("recv_", __LINE__); 212 | } 213 | else if(n == 0){ 214 | return 0; 215 | } 216 | precv = (char*)&recv_buf; 217 | } 218 | 219 | memcpy(psend, precv, sizeof(send_t)); 220 | 221 | return sizeof(send_t); 222 | } 223 | */ 224 | 225 | //发送数据 226 | int send_data(int conn_fd, send_t * psend){ 227 | 228 | int ret; 229 | if((ret = send(conn_fd, psend, sizeof(send_t), 0)) < 0){ 230 | my_err("send",__LINE__); 231 | } 232 | 233 | 234 | printf("%u send\n", psend->command_type); 235 | memset(psend, 0, sizeof(send_t)); 236 | return ret; 237 | } 238 | 239 | 240 | 241 | //将字符串str就地转换为大写字符串,并返回字符串头指针 242 | char *Str2Upper(char *str) { 243 | if (NULL == str) 244 | return NULL; 245 | else { 246 | char *p = str; 247 | while ('\0' != *p) { 248 | if (*p >= 'a' && *p <= 'z') 249 | *p -= 32; 250 | p++; 251 | } 252 | return str; 253 | } 254 | } 255 | 256 | //显示消息 257 | /*void show_message(int conn_fd){ 258 | message_t leave; 259 | message_t * pmess = &leave; 260 | 261 | recv(conn_fd, pmess, sizeof(leave), 0); 262 | printf("%d-%d-%d %2d:%2d:%2d\n",pmess->date.year,pmess->date.month,pmess->date.day,pmess->time.hour,pmess->time.minute,pmess->time.second); 263 | printf("<%s>: ", pmess->send_name); 264 | puts(pmess->message_buf); 265 | puts(" "); 266 | }*/ 267 | 268 | /* 269 | void recv_message(int conn_fd, message_t * data){ 270 | 271 | if(recv(conn_fd, data, sizeof(message_t), 0) < 0){ 272 | send_data(conn_fd,(char *)("n")); 273 | return; 274 | } 275 | else{ 276 | send_data(conn_fd, (char*)("y")); 277 | } 278 | 279 | } 280 | 281 | 282 | void send_message(int conn_fd, char * send_name){ 283 | 284 | static char send_buf[BUFSIZE-36]; 285 | static char recv_buf[BUFSIZE]; 286 | int flag = INVALID_INPUT; 287 | 288 | message_t info; 289 | message_t *data = &info; 290 | 291 | data->date = DateNow(); 292 | data->time = TimeNow(); 293 | strcpy(data->send_name, send_name); 294 | 295 | input_check(BUFSIZE-36, send_buf); 296 | strcpy(data->message_buf, send_buf); 297 | do{ 298 | 299 | if(send(conn_fd, data, sizeof(info), 0) < 0){ 300 | my_err("send", __LINE__); 301 | } 302 | 303 | if(my_recv(conn_fd, recv_buf,sizeof(recv_buf)) < 0){ 304 | my_err("data is too long", __LINE__); 305 | } 306 | 307 | if(recv_buf[0] == VALID_INPUT){ 308 | flag = VALID_INPUT; 309 | } 310 | else{ 311 | printf("error, input again,\n"); 312 | flag = INVALID_INPUT; 313 | input_check(BUFSIZE-36, send_buf); 314 | strcpy(data->message_buf, send_buf); 315 | } 316 | }while(flag == INVALID_INPUT); 317 | 318 | } 319 | */ 320 | -------------------------------------------------------------------------------- /common/common.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-05 09:10 5 | * Filename : common.h 6 | * Description : 7 | * *****************************************************************************/ 8 | #ifndef COMMON_H_ 9 | #define COMMON_H_ 10 | 11 | #include 12 | #include 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 | #include 29 | #include 30 | #include"struct.h" 31 | 32 | int file_write(int fd, const void * data, int len); //写入文件 33 | 34 | int file_read(int fd, int len, void * data); //读出文件 35 | 36 | int file_len(int fd);// 获得文件长度 37 | 38 | void my_err(const char * err_string, int line); 39 | 40 | int recv_data(int conn_fd, send_t *precv); 41 | 42 | char l_getc(); //从键盘获取一个字符,避免回车 43 | 44 | void cl_stdin(); //清空输入缓冲 45 | 46 | void getpwd(int maxlen, char *pwd); 47 | 48 | int input_check(int input_len, char *input_buf); //输入合法性检查 49 | 50 | void show_recv(int conn_fd, char * recv_buf, int buflen); //显示接收消息 51 | 52 | user_date_t DateNow(); //获取当前日期 53 | 54 | user_time_t TimeNow(); //获取当前时间 55 | 56 | char* Str2Upper(char * entUpperName ); //字符串转化 57 | 58 | int ser_atoi(char *buf); //将字符转化为int 59 | 60 | int send_data(int conn_fd, send_t * psend); //发送数据 61 | 62 | void send_message(int conn_fd, char * send_name); //send message,need servive answer 63 | 64 | void show_message(int conn_fd); //recvie and show message 65 | 66 | void recv_message(int conn_fd, message_t * data); 67 | 68 | #endif 69 | 70 | -------------------------------------------------------------------------------- /common/shan.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-17 15:53 5 | * Filename : shan.c 6 | * Description : 7 | * *****************************************************************************/ 8 | 9 | #define ACCOUNTSIZE 100 //最大连接数量 10 | #define FRIENDSIZE 50 //最大好友数 11 | #define BUFSIZE 225 //最大缓冲区 12 | #define GROUPSIZE 50 //最大群规模 13 | #define GROUPCOUNT 20 //最大群数量 14 | 15 | 16 | typedef struct{ 17 | char username[32]; 18 | char usersex[10]; 19 | char status; 20 | }friend_info_t; 21 | 22 | typedef struct{ 23 | char groupname[32]; 24 | char owner[32]; 25 | int groupid; 26 | int size; 27 | friend_info_t member[GROUPSIZE]; 28 | }group_info_t; 29 | 30 | typedef struct { 31 | int year; 32 | int month; 33 | int day; 34 | }user_date_t; 35 | 36 | typedef struct{ 37 | int hour; 38 | int minute; 39 | int second; 40 | }user_time_t; 41 | 42 | typedef struct{ 43 | user_date_t date; 44 | user_time_t time; 45 | char send_name[32]; 46 | char from_name[32]; 47 | char message_buf[BUFSIZE-36]; 48 | 49 | }message_t; 50 | 51 | typedef struct{ 52 | char filename[32]; 53 | char filepath[BUFSIZE]; 54 | int count; 55 | int lenth; 56 | }fileinfo_message_t; 57 | 58 | typedef struct{ 59 | 60 | int conn_fd; 61 | int flag_recv; 62 | }thread_t; 63 | 64 | typedef struct { 65 | 66 | user_date_t date; 67 | user_time_t time; 68 | }user_logtime_t; 69 | 70 | 71 | typedef struct{ 72 | 73 | char markname[32]; 74 | int filelen; 75 | int sendlen; 76 | int givelen; 77 | int leftlen; 78 | 79 | }fileinfo_t; 80 | 81 | 82 | typedef struct{ 83 | char filepath[32]; 84 | char markname[32]; 85 | int filelen; 86 | int sendlen; 87 | int givelen; 88 | int sendtimes; 89 | int leftlen; 90 | char buf[BUFSIZE]; 91 | }file_t; 92 | 93 | 94 | 95 | typedef struct{ 96 | int userid; 97 | char username[32]; 98 | char userpasswd[32]; 99 | char usersex[10]; 100 | char leavemes_flag; 101 | char status; 102 | int friendslist[FRIENDSIZE]; 103 | int grouplist[GROUPCOUNT]; 104 | int conn_fd; 105 | user_logtime_t ontime; 106 | user_logtime_t offtime; 107 | }userinfo_service_t; 108 | 109 | 110 | typedef struct{ 111 | userinfo_service_t userinfo; 112 | friend_info_t friendinfo; 113 | group_info_t group_info; 114 | user_date_t data; 115 | user_time_t time; 116 | message_t message; 117 | fileinfo_message_t messageinfo; 118 | fileinfo_t file; 119 | int conn_fd; 120 | char check_answer; 121 | int input_check; 122 | char userstatus; 123 | int command_type; 124 | char buf[BUFSIZE]; 125 | }send_t; 126 | 127 | 128 | 129 | #include 130 | #include 131 | #include 132 | 133 | int main(int argc, char *argv[]) 134 | { 135 | send_t hh; 136 | 137 | int i; 138 | i = sizeof(hh); 139 | printf("%d f\n", i); 140 | 141 | return 0; 142 | } 143 | -------------------------------------------------------------------------------- /common/shiyi.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int* thread(char s[]) 9 | { 10 | int pos = 0; 11 | 12 | while(1) 13 | { 14 | printf("\033[s"); 15 | //设置光标位置 16 | printf("\033[%d;%dH", 5, 5); 17 | //清除从光标到行尾的内容 18 | 19 | printf("\033[K"); 20 | printf("%s", s[pos]); 21 | pos = (pos+1)%3; 22 | printf("\033[u"); 23 | 24 | fflush(stdout); 25 | sleep(1); 26 | } 27 | 28 | return NULL; 29 | } 30 | 31 | int main() 32 | { 33 | pthread_t thid1, thid2; 34 | int i; 35 | char s[100]; 36 | 37 | printf("\033[%d;%dH", 30, 5); 38 | 39 | //创建线程 40 | if( pthread_create(&thid,NULL,(void*)thread,(void*)&i) 41 | != 0 ) 42 | { 43 | printf("Error!\n"); 44 | exit(1); 45 | } 46 | if( pthread_create(&thid2,NULL,(void*)thread,(void*)&i) 47 | != 0 ) 48 | { 49 | printf("Error!\n"); 50 | exit(1); 51 | } 52 | 53 | 54 | while(1) 55 | { 56 | printf("\033[%d;%dH", 30, 5); 57 | scanf("%s", s); 58 | printf("\033[%d;%dH", 30, 5); 59 | printf("\033[K"); 60 | printf("\033[%d;%dH%s", 20, 5, s); 61 | 62 | } 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /common/struct.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-13 21:12 5 | * Filename : struct.h 6 | * Description : 7 | * *****************************************************************************/ 8 | 9 | 10 | #define ACCOUNTSIZE 1000 //最大连接数量 11 | #define FRIENDSIZE 50 //最大好友数 12 | #define BUFSIZE 1024 //最大缓冲区 13 | #define GROUPSIZE 50 //最大群规模 14 | #define GROUPCOUNT 20 //最大群数量 15 | 16 | #define INVALID_USERINFO 'n' //无效信息 17 | #define VALID_USERINFO 'y' // 有效信息 18 | 19 | 20 | //用户操作类型 21 | 22 | #define _USERADD 11 //用户注册 23 | #define _USERCHA 12 //更改用户基本信息 24 | #define _USERDEL 13 //删除用户 25 | #define _USERONL 14 //上线操作 26 | #define _USEROFFL 15 //下线操作 27 | 28 | #define _LEAVEMES 's' //有离线消息 29 | #define _NOMESSAGE 'e' //无离线消息 30 | #define _ONLINE 'o' //为上线状态 31 | #define _OFFLINE 'd' //为离线状态 32 | 33 | #define _FRIENDLIST 21 //用户好友列表 34 | #define _FRIENDFIND 22 //查找用户 35 | #define _FRIENDASK 23 //好友请求 36 | #define _FRIENDSEND 24 //add friend 37 | #define _FRIENDDEL 25 //delet friend 38 | #define _USERSALL 26 39 | 40 | #define _GROUPLIST 31 //用户群列表 41 | #define _GROUPFIND 32 //查找群 42 | #define _GROUPOWN 33 //creat a group 43 | #define _GROUPSEND 33 //add group 44 | #define _GROUPDEL 34 //group quit 45 | #define _GROUPDISMISS 35 //group dismiss 46 | #define _GROUPSALL 36 47 | 48 | #define _RECALL 99 //返回到主目录 49 | #define _SYSTEMTIP 91 //system tip 50 | #define _DENY 92 51 | 52 | #define _INPUTNAME 41 53 | #define _INPUTPASSWD 42 54 | #define _INPUTSEX 43 55 | #define _INPUTFRIEND 44 56 | #define _INPUTGROUP 45 57 | 58 | #define _CHATGRO 52 59 | #define _CHATONE 51 60 | #define _LEAVEMESS 53 //查看离线消息 61 | #define _MESSTIP 54 62 | 63 | #define LOADSTART 't' 64 | #define LOADOVER 'r' 65 | #define LOADWRONG 'g' 66 | 67 | #define _UPLOAD 61 68 | #define _DOWNLOAD 62 69 | #define _LOADERR 63 70 | 71 | #define LISTENQ 1024 72 | #define SERV_PORT 4507 73 | 74 | typedef struct{ 75 | char username[32]; 76 | char usersex[10]; 77 | char status; 78 | }friend_info_t; 79 | 80 | typedef struct{ 81 | char groupname[32]; 82 | char owner[32]; 83 | int groupid; 84 | int size; 85 | friend_info_t member[GROUPSIZE]; 86 | }group_info_t; 87 | 88 | typedef struct { 89 | int year; 90 | int month; 91 | int day; 92 | }user_date_t; 93 | 94 | typedef struct{ 95 | int hour; 96 | int minute; 97 | int second; 98 | }user_time_t; 99 | 100 | typedef struct{ 101 | int type; 102 | user_date_t date; 103 | user_time_t time; 104 | char send_name[32]; 105 | char from_name[32]; 106 | char message_buf[BUFSIZE-36]; 107 | 108 | }message_t; 109 | 110 | typedef struct{ 111 | char filename[32]; 112 | char filepath[BUFSIZE]; 113 | int count; 114 | int lenth; 115 | }fileinfo_message_t; 116 | 117 | typedef struct{ 118 | 119 | int conn_fd; 120 | int flag_recv; 121 | }thread_t; 122 | 123 | typedef struct { 124 | 125 | user_date_t date; 126 | user_time_t time; 127 | }user_logtime_t; 128 | 129 | 130 | typedef struct{ 131 | 132 | char markname[32]; 133 | int filelen; 134 | int sendlen; 135 | int givelen; 136 | int leftlen; 137 | 138 | }fileinfo_t; 139 | 140 | 141 | typedef struct{ 142 | char filepath[32]; 143 | char markname[32]; 144 | int filelen; 145 | int sendlen; 146 | int givelen; 147 | int sendtimes; 148 | int leftlen; 149 | char buf[BUFSIZE]; 150 | }file_t; 151 | 152 | 153 | 154 | typedef struct{ 155 | int userid; 156 | char username[32]; 157 | char userpasswd[32]; 158 | char usersex[10]; 159 | char leavemes_flag; 160 | char status; 161 | int friendslist[FRIENDSIZE]; 162 | int grouplist[GROUPCOUNT]; 163 | int conn_fd; 164 | user_logtime_t ontime; 165 | user_logtime_t offtime; 166 | }userinfo_service_t; 167 | 168 | 169 | typedef struct{ 170 | userinfo_service_t userinfo; 171 | friend_info_t friendinfo; 172 | group_info_t group_info; 173 | message_t message; 174 | fileinfo_message_t messageinfo; 175 | fileinfo_t file; 176 | int conn_fd; 177 | char check_answer; 178 | int input_check; 179 | char userstatus; 180 | int command_type; 181 | char buf[BUFSIZE]; 182 | }send_t; 183 | 184 | -------------------------------------------------------------------------------- /common/tags: -------------------------------------------------------------------------------- 1 | !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ 2 | !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ 3 | !_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ 4 | !_TAG_PROGRAM_NAME Exuberant Ctags // 5 | !_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ 6 | !_TAG_PROGRAM_VERSION 5.8 // 7 | ACCOUNTSIZE struct.h 10;" d 8 | BUFSIZE struct.h 12;" d 9 | COMMON_H_ common.h 9;" d 10 | DateNow common.c /^user_date_t DateNow() {$/;" f 11 | FRIENDSIZE struct.h 11;" d 12 | GROUPCOUNT struct.h 14;" d 13 | GROUPSIZE struct.h 13;" d 14 | INVALID_INPUT common.c 22;" d file: 15 | INVALID_USERINFO struct.h 16;" d 16 | LISTENQ struct.h 55;" d 17 | SERV_PORT struct.h 56;" d 18 | Str2Upper common.c /^char *Str2Upper(char *str) {$/;" f 19 | TimeNow common.c /^user_time_t TimeNow(){$/;" f 20 | VALID_INPUT common.c 23;" d file: 21 | VALID_USERINFO struct.h 17;" d 22 | _FRIENDASK struct.h 35;" d 23 | _FRIENDDEL struct.h 37;" d 24 | _FRIENDFIND struct.h 34;" d 25 | _FRIENDLIST struct.h 33;" d 26 | _FRIENDSEND struct.h 36;" d 27 | _GROUPDEL struct.h 43;" d 28 | _GROUPDISMISS struct.h 44;" d 29 | _GROUPFIND struct.h 40;" d 30 | _GROUPLIST struct.h 39;" d 31 | _GROUPOWN struct.h 41;" d 32 | _GROUPSEND struct.h 42;" d 33 | _INPUTFRIEND struct.h 52;" d 34 | _INPUTGROUP struct.h 53;" d 35 | _INPUTNAME struct.h 49;" d 36 | _INPUTPASSWD struct.h 50;" d 37 | _INPUTSEX struct.h 51;" d 38 | _LEAVEMES struct.h 28;" d 39 | _LEAVEMESS struct.h 45;" d 40 | _NOMESSAGE struct.h 29;" d 41 | _OFFLINE struct.h 31;" d 42 | _ONLINE struct.h 30;" d 43 | _RECALL struct.h 47;" d 44 | _USERADD struct.h 22;" d 45 | _USERCHA struct.h 23;" d 46 | _USERDEL struct.h 24;" d 47 | _USEROFFL struct.h 26;" d 48 | _USERONL struct.h 25;" d 49 | __input common.c /^void __input(int conn_fd, char *input_buf){$/;" f 50 | cl_stdin common.c /^void cl_stdin(){$/;" f 51 | command_type struct.h /^ int command_type;$/;" m union:__anon10 52 | conn_fd struct.h /^ int conn_fd;$/;" m struct:__anon7 53 | count struct.h /^ int count;$/;" m struct:__anon6 54 | data struct.h /^ user_date_t data;$/;" m union:__anon10 55 | date struct.h /^ user_date_t date;$/;" m struct:__anon5 56 | date struct.h /^ user_date_t date;$/;" m struct:__anon8 57 | day struct.h /^ int day;$/;" m struct:__anon3 58 | file_len common.c /^int file_len(int fd){$/;" f 59 | file_read common.c /^void * file_read(int fd,int len,void* buf){$/;" f 60 | file_write common.c /^int file_write(int fd, const void * buf, int len){$/;" f 61 | fileinfo_message_t struct.h /^}fileinfo_message_t;$/;" t typeref:struct:__anon6 62 | filename struct.h /^ char filename[32];$/;" m struct:__anon6 63 | filepath struct.h /^ char filepath[BUFSIZE];$/;" m struct:__anon6 64 | flag_recv struct.h /^ int flag_recv;$/;" m struct:__anon7 65 | friend_info_t struct.h /^}friend_info_t;$/;" t typeref:struct:__anon1 66 | friendinfo struct.h /^ friend_info_t friendinfo;$/;" m union:__anon10 67 | friendslist struct.h /^ int friendslist[FRIENDSIZE];$/;" m struct:__anon9 68 | getch common.c /^int getch(void){$/;" f 69 | getpwd common.c /^void getpwd(int maxlen, char *pwd){$/;" f 70 | group_info struct.h /^ group_info_t group_info;$/;" m union:__anon10 71 | group_info_t struct.h /^}group_info_t;$/;" t typeref:struct:__anon2 72 | groupid struct.h /^ int groupid;$/;" m struct:__anon2 73 | grouplist struct.h /^ int grouplist[GROUPCOUNT];$/;" m struct:__anon9 74 | groupname struct.h /^ char groupname[32];$/;" m struct:__anon2 75 | hour struct.h /^ int hour;$/;" m struct:__anon4 76 | input_ common.c /^void input_(int conn_fd, const char *string, int len){$/;" f 77 | input_check common.c /^int input_check(int input_len, char * input_buf){$/;" f 78 | input_check struct.h /^ char input_check;$/;" m union:__anon10 79 | l_getc common.c /^char l_getc(){$/;" f 80 | leavemes_flag struct.h /^ char leavemes_flag;$/;" m struct:__anon9 81 | lenth struct.h /^ int lenth;$/;" m struct:__anon6 82 | main shiyi.c /^int main()$/;" f 83 | member struct.h /^ friend_info_t member[GROUPSIZE];$/;" m struct:__anon2 84 | message struct.h /^ message_t message;$/;" m union:__anon10 85 | message_buf struct.h /^ char message_buf[BUFSIZE-36];$/;" m struct:__anon5 86 | message_t struct.h /^}message_t;$/;" t typeref:struct:__anon5 87 | messageinfo struct.h /^ fileinfo_message_t messageinfo;$/;" m union:__anon10 88 | minute struct.h /^ int minute;$/;" m struct:__anon4 89 | month struct.h /^ int month;$/;" m struct:__anon3 90 | my_err common.c /^void my_err(const char* err_string, int line){$/;" f 91 | my_recv common.c /^int my_recv(int conn_fd, char *data_buf, int len){$/;" f 92 | offtime struct.h /^ user_logtime_t offtime;$/;" m struct:__anon9 93 | ontime struct.h /^ user_logtime_t ontime;$/;" m struct:__anon9 94 | owner struct.h /^ char owner[32];$/;" m struct:__anon2 95 | pthread struct.h /^ thread_t pthread;$/;" m union:__anon10 96 | recv_message common.c /^void recv_message(int conn_fd, message_t * data){$/;" f 97 | second struct.h /^ int second;$/;" m struct:__anon4 98 | send_data common.c /^int send_data(int conn_fd,char *string){$/;" f 99 | send_message common.c /^void send_message(int conn_fd, char * send_name){$/;" f 100 | send_name struct.h /^ char send_name[32];$/;" m struct:__anon5 101 | send_t struct.h /^}send_t;$/;" t typeref:union:__anon10 102 | ser_atoi common.c /^int ser_atoi(char * buf){ \/\/atoi$/;" f 103 | show_message common.c /^void show_message(int conn_fd){$/;" f 104 | show_recv common.c /^void show_recv(int conn_fd, char *recv_buf, int buflen ){$/;" f 105 | size struct.h /^ int size;$/;" m struct:__anon2 106 | status struct.h /^ char status;$/;" m struct:__anon9 107 | status struct.h /^ char status[5];$/;" m struct:__anon1 108 | thread shiyi.c /^int* thread(char s[])$/;" f 109 | thread_t struct.h /^}thread_t;$/;" t typeref:struct:__anon7 110 | time struct.h /^ user_time_t time;$/;" m struct:__anon5 111 | time struct.h /^ user_time_t time;$/;" m struct:__anon8 112 | time struct.h /^ user_time_t time;$/;" m union:__anon10 113 | user_date_t struct.h /^}user_date_t;$/;" t typeref:struct:__anon3 114 | user_logtime_t struct.h /^}user_logtime_t;$/;" t typeref:struct:__anon8 115 | user_time_t struct.h /^}user_time_t;$/;" t typeref:struct:__anon4 116 | userid struct.h /^ int userid;$/;" m struct:__anon9 117 | userinfo struct.h /^ userinfo_service_t userinfo;$/;" m union:__anon10 118 | userinfo_service_t struct.h /^}userinfo_service_t;$/;" t typeref:struct:__anon9 119 | username struct.h /^ char username[32];$/;" m struct:__anon1 120 | username struct.h /^ char username[32];$/;" m struct:__anon9 121 | username struct.h /^ char username[32];$/;" m union:__anon10 122 | userpasswd struct.h /^ char userpasswd[32];$/;" m struct:__anon9 123 | usersex struct.h /^ char usersex[10];$/;" m struct:__anon1 124 | usersex struct.h /^ char usersex[10];$/;" m struct:__anon9 125 | userstatus struct.h /^ char userstatus;$/;" m union:__anon10 126 | year struct.h /^ int year;$/;" m struct:__anon3 127 | -------------------------------------------------------------------------------- /gtk/Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | program = study 3 | #PATH += /usr/include/gtk-2.0 4 | LDLIBS= `pkg-config --libs gtk+-2.0` 5 | CFLAGS = -Wall -g `pkg-config --cflags gtk+-2.0` 6 | 7 | $(program):$(program).o 8 | $(CC) $(LDLIBS) $(program).o -o $(program) 9 | 10 | $(program).o:$(program).c 11 | $(CC) $(CFLAGS) -c $(program).c 12 | 13 | clean: 14 | 15 | -rm -f $(program) 16 | -rm -f *.o 17 | 18 | -------------------------------------------------------------------------------- /gtk/study: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/gtk/study -------------------------------------------------------------------------------- /gtk/study.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-04 16:20 5 | * Filename : study.c 6 | * Description : 7 | * *****************************************************************************/ 8 | 9 | #include 10 | 11 | void hello(GtkWidget *widget, gpointer *data){ 12 | g_print("button clicked and data = %s \n",(char*)data); 13 | } 14 | 15 | void destroy(GtkWidget *widget, gpointer * data){ 16 | gtk_main_quit(); 17 | } 18 | 19 | int main(int argc, char *argv[]){ 20 | GtkWidget *window; 21 | GtkWidget *button; 22 | GtkWidget *box; 23 | GtkWidget *group; 24 | GtkWidget *check ,*radio; 25 | 26 | gtk_init(&argc, &argv); 27 | 28 | window = gtk_window_new(GTK_WINDOW_TOPLEVEL); //创建顶级窗口 29 | 30 | //关闭窗口时,执行回调函数 31 | g_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(destroy), NULL); 32 | 33 | gtk_container_border_width(GTK_CONTAINER(window),100); //边框宽度 34 | 35 | //生成垂直容器 36 | box = gtk_vbox(FALSE, 0); 37 | gtk_container_add(GTK_CONTATINER(window), box); 38 | 39 | //生成两个垂直按钮 40 | check = gtk_check_button_new_with_label("silly A"); //创建带文本的标签 41 | 42 | //单击按钮时,执行回调函数 43 | g_signal_connect (GTK_OBJECT(check), "clicked", GTK_SIGNAL_FUNC(clicked_button), "check button1"); 44 | 45 | //加入box中 46 | gtk_box_pack_start(GTK_BOX(box), check, TRUE, TRUE, 0); 47 | //gtk_container_add(GTK_CONTAINER(window), button); //将按钮添加到窗口中 48 | 49 | gtk_widget_show(check); //显示控件 50 | 51 | check = gtk_check_button_new_label("silly B"); 52 | g_signal_connect(GTK_OBJECT(check), "clicked", GTK_SINGAL_FUNC(click_button), "check button2"); 53 | gtk_box_pack_start(GTK_BOX(box), check, TRUE, TRUEM 0); 54 | gtk_widget_show(check); 55 | 56 | 57 | //三个radio 58 | radio = gtk_radio_button_new_label(NULL, "1"); 59 | g_signal_connect(GTK_OBJECT(radio), "clicked", GTK_SIGNAL_FUNC(click_button), "1"); 60 | 61 | gtk_box_pack_start(GTK_BOX(box), radio, TRUN, TRUN, 0); 62 | gtk_widget_show(radio); 63 | 64 | //生成第一个radio按钮时group参数指向NULL, 此后都要重新获取group 65 | group = gtk_radio_button_group(GTK_RADIO_BUTTON(radio)); 66 | radio = gtk_radio_button_new_label(group, "2"); 67 | g_signal_connect(GTK_OBJET(box),"clicked", GTK_SIGNAL_FUNC(click_button), "2") 68 | gtk_box_pack_start(GTK_BOX(box), TRUN, TRUN, 0); 69 | gtk_widget_show(radio); 70 | 71 | group = gtk_radio_buttun_group(GTK_RADIO_BUTTON(radio)); 72 | radio = gtk_radio_button_new_label(group, "3"); 73 | g_signal_connect(GTK_OBJECT(radio), "clicked", GTK_SIGNAL_FUNC(click_button), "3"); 74 | 75 | //将第三个按钮设置为选中状态 76 | gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio), TRUN); 77 | gtk_box_pack_start(GET_) 78 | gtk_widget_show(window); 79 | 80 | gtk_main(); //使GTK进入消息处理循环 81 | 82 | return 0; 83 | } 84 | 85 | -------------------------------------------------------------------------------- /gtk/study.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/gtk/study.o -------------------------------------------------------------------------------- /service/EntityKey.c: -------------------------------------------------------------------------------- 1 | 2 | #include"service.h" 3 | 4 | //定义存储所有实体主键的文件名为EntityKey.dat 5 | static const char ENTITY_KEY_FILE[] = "./EntityKey.dat"; 6 | 7 | 8 | 9 | /*根据传入的实体名entName,为新实体分配一个唯一的主键。函数返回值为取新实体的主键值*/ 10 | inline long EntKey_Srv_CompNewKey(char entName[]) { 11 | return EntKey_Srv_CompNewKeys(entName, 1); 12 | } 13 | 14 | /*根据传入的实体名entName及实体个数count,为这个count个新实体分配一个长度为count的主键值区间, 15 | * 使得每个新实体在该区间内都可以分配到 唯一的 主键。返回值为该主键区间的最小值*/ 16 | inline long EntKey_Srv_CompNewKeys(char entName[], int count) { 17 | char entUpperName[32]; 18 | 19 | int nameLen = strlen(entName); 20 | 21 | memcpy(entUpperName, entName, nameLen); 22 | entUpperName[nameLen] = '\0'; 23 | 24 | /*转换为大写*/ 25 | Str2Upper(entUpperName); 26 | 27 | return EntKey_Perst_GetNewKeys(entUpperName, count); 28 | } 29 | 30 | 31 | //将主键key保存到主键链表keyList中 32 | inline void EntKey_Srv_Add2List(entkey_list_t keyList, long key) { 33 | assert(NULL != keyList); 34 | if (!EntKey_Srv_CheckExist(keyList, key)) { 35 | entkey_node_t *newNode = (entkey_node_t *) malloc( 36 | sizeof(entkey_node_t)); 37 | if (NULL == newNode) { 38 | printf("Memory overflow!\n"); 39 | return; 40 | } else { 41 | 42 | newNode->data.key = key; 43 | List_AddTail(keyList, newNode); 44 | } 45 | } 46 | } 47 | 48 | //在主键链表keyList中检查key是否存在,返回1存在,否则0 49 | inline int EntKey_Srv_CheckExist(entkey_list_t keyList, long key){ 50 | assert(NULL!=keyList); 51 | entkey_node_t *p; 52 | List_ForEach(keyList, p){ 53 | if(p->data.key==key) 54 | return 1; 55 | } 56 | return 0; 57 | } 58 | 59 | 60 | 61 | 62 | 63 | 64 | /*根据传入的实体名entName及实体个数count,为这个count个新实体分配一个长度为count的主键值区间, 65 | * 使得每个新实体在该区间内都可以分配到 唯一的 主键。返回值为该主键区间的最小值*/ 66 | long EntKey_Perst_GetNewKeys(char entName[], int count) { 67 | entity_key_t ent; 68 | FILE *fp; 69 | int found = 0; 70 | long newEntKey = 1; 71 | 72 | if (count < 1) { 73 | printf("Entity count must be bigger than 0!\n"); 74 | return 0; 75 | } 76 | 77 | //判断文件是否存在 78 | if (access(ENTITY_KEY_FILE, 0)) { 79 | //新建文件 80 | fp = fopen(ENTITY_KEY_FILE, "wb+"); 81 | if (NULL == fp) { 82 | return 0; 83 | } 84 | } else { 85 | //以更新模式打开 86 | fp = fopen(ENTITY_KEY_FILE, "rb+"); 87 | if (NULL == fp) { 88 | return 0; 89 | } 90 | } 91 | 92 | while (!feof(fp)) { 93 | if (fread(&ent, sizeof(entity_key_t), 1, fp)) { 94 | if (0 == strcmp(ent.entyName, entName)) { //找到主键记录 95 | fseek(fp, -sizeof(entity_key_t), SEEK_CUR); 96 | newEntKey = ent.key + 1; 97 | ent.key += count; 98 | fwrite(&ent, sizeof(entity_key_t), 1, fp); 99 | found = 1; 100 | break; 101 | } 102 | } 103 | } 104 | 105 | //未找到实体的主键记录,新加主键记录到文件末尾, 106 | if (!found) { 107 | strcpy(ent.entyName, entName); 108 | newEntKey = 1; 109 | ent.key = count; 110 | fwrite(&ent, sizeof(entity_key_t), 1, fp); 111 | } 112 | 113 | fclose(fp); 114 | 115 | return newEntKey; 116 | } 117 | 118 | -------------------------------------------------------------------------------- /service/EntityKey.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/service/EntityKey.o -------------------------------------------------------------------------------- /service/Makefile: -------------------------------------------------------------------------------- 1 | #!Makefile 2 | 3 | PROGRAM = Service 4 | 5 | C_SOURCES = $(shell find . -name "*.c") 6 | C_OBJECTS = $(patsubst %.c, %.o, $(C_SOURCES)) 7 | 8 | CC = gcc 9 | 10 | #C_FLAGS = -DDEBUG -c -Wall -Wextra -ggdb -I. 11 | C_FLAGS = -c. 12 | 13 | all: $(C_OBJECTS) 14 | @echo compile... 15 | $(CC) $(C_OBJECTS) -o $(PROGRAM) -lpthread 16 | 17 | .c.o: 18 | @echo link$< ... 19 | $(CC) $(C_FLAGS) $< -o $@ 20 | 21 | .PHONY:clean 22 | clean: 23 | $(RM) $(C_OBJECTS) Service 24 | -------------------------------------------------------------------------------- /service/Service: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/service/Service -------------------------------------------------------------------------------- /service/account.c: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-09 15:10 5 | * Filename : account.c 6 | * Description : 7 | * *****************************************************************************/ 8 | 9 | #include"service.h" 10 | 11 | #define USER_FILE "/home/" 12 | 13 | //在账号文件中删除与参数id匹配的账号,删除成功 return 1;否则 return 0; 14 | int userinfo_p_deletid(int id) { 15 | chdir(USER_FILE); 16 | if(rename("USER", "USER_TEMP")<0){ 17 | printf("Cannot open file %s!\n", "USER"); 18 | return 0; 19 | } 20 | 21 | FILE *fp1; 22 | fp1= fopen("USER_TEMP", "rb"); 23 | if (fp1 == NULL){ 24 | printf("Cannot open file %s!\n", "USER"); 25 | return 0; 26 | } 27 | FILE *fp2; 28 | fp2 = fopen("USER", "wb"); 29 | if (fp2 == NULL) { 30 | printf("Cannot open file %s!\n", "USER_TEMP"); 31 | return 0; 32 | } 33 | userinfo_service_t buf; 34 | int found = 0; 35 | while (!feof(fp1)) { 36 | if (fread(&buf, sizeof(userinfo_service_t), 1, fp1)) { 37 | if (id == buf.userid) { 38 | found = 1; 39 | continue; 40 | } 41 | fwrite(&buf, sizeof(userinfo_service_t), 1, fp2); 42 | } 43 | } 44 | fclose(fp1); 45 | fclose(fp2); 46 | remove("USER_TEMP"); //删除临时文件 47 | return found; 48 | 49 | } 50 | 51 | int message_p_update(message_t * data) { 52 | 53 | chdir(USER_FILE); 54 | FILE *fp = fopen(data->send_name,"ab+"); 55 | if(fp == NULL){ 56 | my_err("open_USER", __LINE__); 57 | } 58 | 59 | message_t buf; //结构体 60 | int found = 0; 61 | while(!feof(fp)){ 62 | if(fread(&buf,sizeof(message_t),1,fp)){ 63 | fwrite(data, sizeof(message_t), 1, fp); 64 | found = 1; 65 | break; 66 | } 67 | } 68 | fclose(fp); 69 | return found; 70 | } 71 | 72 | 73 | 74 | //在账号文件中查找与参数账号匹配的账号,找到 return 1;否则 return 0;并进行覆盖重写 75 | int userinfo_p_update(userinfo_service_t * data) { 76 | chdir(USER_FILE); 77 | FILE *fp = fopen("USER","rb+"); 78 | if(fp == NULL){ 79 | my_err("open_USER", __LINE__); 80 | } 81 | userinfo_service_t buf; //结构体 82 | int found = 0; 83 | while(!feof(fp)){ 84 | if(fread(&buf,sizeof(userinfo_service_t),1,fp)){ 85 | if (buf.userid == data->userid) { 86 | fseek(fp, -sizeof(userinfo_service_t), SEEK_CUR); 87 | fwrite(data, sizeof(userinfo_service_t), 1, fp); 88 | found = 1; 89 | break; 90 | } 91 | } 92 | } 93 | fclose(fp); 94 | return found; 95 | } 96 | 97 | 98 | //检查用户文件是否存在 99 | int userinfo_p_checkfile(const char *string){ 100 | 101 | chdir(USER_FILE); 102 | 103 | if(access(string, 0) == 0){ 104 | return 1; 105 | } 106 | else{ 107 | return -1; 108 | } 109 | } 110 | 111 | //创建用户文件 112 | int creat_userdir(const char *string){ 113 | int ret = 0; 114 | int fd; 115 | 116 | chdir(USER_FILE); 117 | if((fd = open(string, O_CREAT | O_RDWR | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP )) < 0){ 118 | my_err("creat_info",__LINE__); 119 | } 120 | close(fd); 121 | ret = 1; 122 | return ret; 123 | } 124 | 125 | //写入用户信息 126 | int userinfo_p_insert(const userinfo_service_t * data){ 127 | 128 | chdir(USER_FILE); 129 | 130 | int ret = 0; 131 | FILE *fp; 132 | fp = fopen("USER","ab"); 133 | if(fp == NULL){ 134 | return 0; 135 | } 136 | ret = fwrite(data,sizeof(userinfo_service_t),1,fp); 137 | 138 | fclose(fp); 139 | 140 | return ret; 141 | } 142 | 143 | //根据用户id获取用户信息并通过data传出 144 | 145 | int userinfo_p_selet(int id, userinfo_service_t * data){ 146 | chdir(USER_FILE); 147 | 148 | FILE *fp; 149 | int found = 0; 150 | userinfo_service_t buf; 151 | fp = fopen("USER","rb"); 152 | if(fp == NULL){ 153 | return 0; 154 | } 155 | while(!feof(fp)){ 156 | if(fread(&buf,sizeof(userinfo_service_t),1,fp)){ 157 | if(buf.userid == id){ 158 | *data = buf; 159 | found = 1; 160 | break; 161 | } 162 | else if(buf.conn_fd == id){ 163 | *data = buf; 164 | found = -1; 165 | break; 166 | } 167 | } 168 | } 169 | fclose(fp); 170 | return found; 171 | 172 | } 173 | 174 | //根据用户名获取用户信息并通过data传出 175 | 176 | int userinfo_p_seletname(const char* username, userinfo_service_t * data){ 177 | 178 | chdir(USER_FILE); 179 | FILE *fp; 180 | int found = 0; 181 | userinfo_service_t buf; 182 | fp = fopen("USER","rb"); 183 | if(fp == NULL){ 184 | return 0; 185 | } 186 | while(!feof(fp)){ 187 | if(fread(&buf,sizeof(userinfo_service_t),1,fp)){ 188 | if(strcmp(buf.username,username) == 0){ 189 | *data = buf; 190 | found = 1; 191 | break; 192 | } 193 | } 194 | } 195 | fclose(fp); 196 | return found; 197 | } 198 | 199 | 200 | 201 | int userinfo_p_delet(int id){ 202 | int ret = id; 203 | return ret; 204 | } 205 | 206 | 207 | //注册函数 208 | 209 | int serve_user_register(send_t *precv){ 210 | 211 | int i; 212 | int ret = 0; //check flag 213 | send_t send_buf; 214 | send_t *psend = &send_buf; 215 | memset(psend, 0, sizeof(send_buf)); 216 | userinfo_service_t data; 217 | 218 | //check USER file 219 | ret = userinfo_p_checkfile((char*)("USER")); 220 | 221 | if(ret < 0){ 222 | creat_userdir("USER"); 223 | } 224 | 225 | psend->conn_fd = precv->conn_fd; 226 | 227 | if(server_name_check(precv->userinfo.username) > 0){ 228 | 229 | psend->userstatus = INVALID_USERINFO; 230 | psend->input_check = _INPUTNAME; 231 | 232 | send_data(psend->conn_fd, psend); 233 | 234 | return 0; 235 | } 236 | 237 | strcpy(data.username, precv->userinfo.username); 238 | 239 | //基本信息录入 240 | strcpy(data.userpasswd, precv->userinfo.userpasswd); 241 | 242 | strcpy(data.usersex, precv->userinfo.usersex); 243 | 244 | //id分配 245 | data.userid = EntKey_Srv_CompNewKey((char*)("USER")); 246 | 247 | //initialize no leave_message 248 | data.leavemes_flag = _NOMESSAGE; 249 | 250 | data.status = _OFFLINE; 251 | 252 | for(i = 0; i < FRIENDSIZE; i++){ 253 | data.friendslist[i] = 0; 254 | } 255 | 256 | for(i = 0; i < GROUPCOUNT; i++){ 257 | data.grouplist[i] = 0; 258 | } 259 | 260 | 261 | 262 | //log time initialize 263 | data.ontime.date.year = 0; 264 | data.ontime.date.month = 0; 265 | data.ontime.date.day = 0; 266 | data.offtime.date.year = 0; 267 | data.offtime.date.month = 0; 268 | data.offtime.date.day = 0; 269 | data.ontime.time.hour = 0; 270 | data.ontime.time.second = 0; 271 | data.ontime.time.minute = 0; 272 | data.offtime.time.hour = 0; 273 | data.offtime.time.second = 0; 274 | data.offtime.time.minute = 0; 275 | 276 | 277 | //用户信息录入 278 | ret = userinfo_p_insert(&data); 279 | 280 | if(ret > 0){ 281 | printf("\nNO. %d user registed, informatiom below:\n", data.userid); 282 | printf("name: %s\n", data.username); 283 | printf("sex: %s\n\n", data.usersex); 284 | 285 | psend->userstatus = VALID_USERINFO; 286 | 287 | 288 | send_data(precv->conn_fd, psend); 289 | } 290 | // return informatiom lenth 291 | return ret; 292 | } 293 | 294 | 295 | 296 | //登录函数 297 | 298 | int serve_user_login(send_t *precv){ 299 | 300 | 301 | send_t send_buf; 302 | send_t *psend = &send_buf; 303 | userinfo_service_t info; 304 | userinfo_service_t *data = &info; 305 | message_t mes; 306 | 307 | memset(&mes, 0, sizeof(message_t)); 308 | memset(psend, 0, sizeof(send_buf)); 309 | 310 | psend->conn_fd = precv->conn_fd; 311 | 312 | if(server_name_check(precv->userinfo.username) < 0){ 313 | 314 | psend->userstatus = INVALID_USERINFO; 315 | psend->input_check = _INPUTNAME; 316 | send_data(psend->conn_fd, psend); 317 | 318 | return 0; 319 | } 320 | else{ 321 | strcpy(psend->userinfo.username, precv->userinfo.username); 322 | } 323 | //用户名密码校验 324 | 325 | userinfo_p_seletname(psend->userinfo.username, data); 326 | 327 | if(data->status == _ONLINE){ 328 | printf(" name [%s] sign conflict, system refused sign in\n", precv->userinfo.username); 329 | psend->userstatus = INVALID_USERINFO; 330 | psend->input_check = _ONLINE; 331 | send_data(psend->conn_fd, psend); 332 | 333 | return 0; 334 | } 335 | //密码校验 336 | if((strcmp(data->userpasswd, precv->userinfo.userpasswd)) == 0){ 337 | 338 | //system tips 339 | printf("\nNO.%d log in, the informatiom below\n", data->userid); //service tips 340 | printf("name: %s\n", data->username); 341 | printf("sex: %s\n", data->usersex); 342 | printf("last log in time: %d-%d-%d %d:%d:%d\n\n",data->ontime.date.year,data->ontime.date.month,data->ontime.date.day,data->ontime.time.hour,data->ontime.time.minute,data->ontime.time.second); 343 | 344 | psend->userstatus = VALID_USERINFO; 345 | 346 | } 347 | else{ 348 | psend->userstatus = INVALID_USERINFO; 349 | psend->input_check = _INPUTPASSWD; 350 | send_data(psend->conn_fd, psend); 351 | 352 | return 0; 353 | } 354 | 355 | data->conn_fd = precv->conn_fd; 356 | //更改系统信息 357 | data->status = _ONLINE; 358 | data->ontime.date = DateNow(); 359 | data->ontime.time = TimeNow(); 360 | 361 | data->conn_fd = psend->conn_fd; 362 | //保存 363 | userinfo_p_update(data); 364 | 365 | send_data(psend->conn_fd, psend); 366 | 367 | strcpy(mes.from_name, precv->userinfo.username); 368 | mes.date = data->ontime.date; 369 | mes.time = data->ontime.time; 370 | mes.type = _ONLINE; 371 | 372 | message_tip(&mes); 373 | 374 | return 0; 375 | } 376 | 377 | /* 378 | int users_tips(char *username){ 379 | 380 | int i; 381 | send_t send_buf; 382 | send_t *psend = &send_buf; 383 | userinfo_service_t data; 384 | userinfo_service_t buf; 385 | 386 | userinfo_p_seletname(username, &data); 387 | 388 | for(i = 0; data.friendslist[i] != 0; i++){ 389 | 390 | memset(&buf, 0, sizeof(userinfo_service_t)); 391 | userinfo_p_selet(data.friendslist[i], &buf); 392 | 393 | if(buf.status == _ONLINE){ 394 | psend->command_type = _SYSTEMTIP; 395 | strcpy(psend->message.from_name, "system"); 396 | strcpy(psend->message.message_buf, "your friend: "); 397 | strcat(psend->message.message_buf, username); 398 | strcat(psend->message.message_buf, "sign in ."); 399 | 400 | send_data(buf.conn_fd, psend); 401 | 402 | } 403 | } 404 | return 0; 405 | } 406 | */ 407 | 408 | //下线函数 409 | 410 | int serve_user_logoff(send_t *precv){ 411 | 412 | message_t mes; 413 | userinfo_service_t info; 414 | userinfo_service_t *data = &info; 415 | int ret; 416 | 417 | memset(&mes, 0, sizeof(message_t)); 418 | 419 | ret = userinfo_p_selet(precv->conn_fd, data); 420 | if(!ret){ 421 | return 0; 422 | } 423 | 424 | if(data->status == _ONLINE){ 425 | data ->status = _OFFLINE; 426 | } 427 | else{ 428 | return 0; 429 | } 430 | 431 | //用户状态改为下线d 432 | 433 | //下线时间赋值 434 | data->offtime.date = DateNow(); 435 | data->offtime.time = TimeNow(); 436 | 437 | 438 | //system 439 | printf("\nNO.%d log off, the informatiom below\n", data->userid); 440 | printf("name: %s\n", data->username); 441 | printf("sex: %s\n", data->usersex); 442 | printf("log in time: %d-%d-%d %d:%d:%d\n\n",data->ontime.date.year,data->ontime.date.month,data->ontime.date.day,data->ontime.time.hour,data->ontime.time.minute,data->ontime.time.second); 443 | 444 | //系统信息进行保存 445 | userinfo_p_update(data); 446 | 447 | strcpy(mes.from_name, precv->userinfo.username); 448 | mes.date = data->offtime.date; 449 | mes.time = data->offtime.time; 450 | mes.type = _OFFLINE; 451 | 452 | // 453 | message_tip(&mes); 454 | return 0; 455 | } 456 | 457 | int serve_user_update(send_t *precv){ 458 | 459 | char name[32]; 460 | int check = 0; 461 | userinfo_service_t info; 462 | userinfo_service_t *data = &info; 463 | 464 | //获取用户名 465 | strcpy(name, precv->userinfo.username); 466 | 467 | check = userinfo_p_seletname(name, data); 468 | 469 | //基本信息录入 470 | strcpy(data->userpasswd, precv->userinfo.userpasswd); 471 | 472 | strcpy(data->usersex, precv->userinfo.usersex); 473 | 474 | 475 | //保存 476 | userinfo_p_update(data); 477 | return 0; 478 | 479 | } 480 | 481 | //not ok not ok 482 | 483 | int serve_user_delet(send_t * precv){ 484 | 485 | userinfo_service_t info; 486 | userinfo_service_t *data = &info; 487 | 488 | 489 | userinfo_p_seletname(precv->userinfo.username, data); 490 | 491 | printf("\nlose user: NO.%d\n, the informatiom below\n", data->userid); 492 | printf("name: %s\n", data->username); 493 | printf("sex: %s\n", data->usersex); 494 | 495 | userinfo_p_deletid(data->userid); 496 | 497 | return 0; 498 | } 499 | 500 | 501 | 502 | int userinfo_p_selectall(userinfo_service_t * users){ 503 | 504 | chdir(USER_FILE); 505 | FILE *fp; 506 | int count = 0; 507 | userinfo_service_t buf; 508 | fp = fopen("USER","rb"); 509 | if(fp == NULL){ 510 | return 0; 511 | } 512 | while(!feof(fp)){ 513 | if(fread(&buf,sizeof(userinfo_service_t),1,fp)){ 514 | if(buf.status == _OFFLINE) 515 | { 516 | continue; 517 | } 518 | else{ 519 | users[count++] = buf; 520 | } 521 | } 522 | } 523 | fclose(fp); 524 | return count; 525 | } 526 | 527 | int server_name_check(char *username){ //重名检测 528 | 529 | chdir(USER_FILE); 530 | int check = 0; 531 | userinfo_service_t data; 532 | userinfo_service_t *info = &data; 533 | int ret; 534 | 535 | //check USER file 536 | ret = userinfo_p_checkfile((char*)("USER")); 537 | if(ret < 0){ 538 | creat_userdir("USER"); 539 | } 540 | 541 | 542 | //userfile check 543 | check = userinfo_p_seletname(username, info); 544 | 545 | if(check){ //name exist 546 | 547 | //send from command_type 548 | return 1; 549 | } 550 | else{ 551 | return -1; 552 | } 553 | 554 | } 555 | 556 | 557 | 558 | -------------------------------------------------------------------------------- /service/account.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/service/account.o -------------------------------------------------------------------------------- /service/chat.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-13 09:26 5 | * Filename : chat.c 6 | * Description : 7 | * *****************************************************************************/ 8 | 9 | #include "service.h" 10 | /* 11 | int server_message_leave(int conn_fd){ 12 | 13 | char recv_buf[BUFSIZE]; 14 | char username[32] = {"\0"}; 15 | userinfo_service_t info; 16 | userinfo_service_t *data = &info; 17 | fileinfo_message_t finfo; 18 | fileinfo_message_t *fdata = &finfo; 19 | message_t minfo; 20 | message_t *mdata = &minfo; 21 | 22 | 23 | strcpy(username, recv_buf); 24 | 25 | userinfo_p_seletname(recv_buf, data); 26 | 27 | if(data->leavemes_flag == _LEAVEMES){ 28 | 29 | //user decide check leave_message or not 30 | if(strcmp(recv_buf, "y")){ 31 | 32 | server_file_select(username, fdata); //search leave_message file by username 33 | 34 | send(conn_fd, fdata, sizeof(finfo),0); 35 | 36 | while(fdata->count--){ 37 | 38 | server_message_selet((char*)("leave_message"), mdata); //get message 39 | 40 | send(conn_fd, mdata, sizeof(message_t), 0); //send 41 | } 42 | 43 | } 44 | else{ 45 | return 0; 46 | } 47 | } 48 | 49 | return 0; 50 | } 51 | 52 | 53 | int server_message_temp(int conn_fd){ 54 | 55 | char username[32] = {"\0"}; 56 | fileinfo_message_t finfo; 57 | fileinfo_message_t *fdata = &finfo; 58 | message_t minfo; 59 | message_t *mdata = &minfo; 60 | 61 | my_recv(conn_fd, username, sizeof(username)); 62 | 63 | server_file_select(username, fdata); //search temp file information by username 64 | 65 | send(conn_fd, fdata, sizeof(finfo),0); 66 | 67 | //no temp message, return 68 | if(fdata->count == 0){ 69 | return 0; 70 | } 71 | 72 | while(fdata->count--){ 73 | 74 | server_message_selet((char*)("temp_message"), mdata); //get message 75 | 76 | send(conn_fd, mdata, sizeof(message_t), 0); //send 77 | } 78 | 79 | return 0; 80 | } 81 | */ 82 | int message_tip(message_t *mes){ 83 | 84 | send_t send_buf; 85 | send_t *psend = &send_buf; 86 | userinfo_service_t data; 87 | userinfo_service_t temp_data; 88 | int sendlist[FRIENDSIZE]; 89 | char namelist[FRIENDSIZE][32]; 90 | int i, n = 0; 91 | 92 | memset(&data, 0, sizeof(data)); 93 | 94 | userinfo_p_seletname(mes->from_name, &data); 95 | for(i = 0; data.friendslist[i] != 0; i++){ 96 | 97 | memset(&temp_data, 0, sizeof(temp_data)); 98 | userinfo_p_selet(data.friendslist[i], &temp_data); 99 | if(temp_data.status == _OFFLINE){ 100 | continue; 101 | } 102 | else{ 103 | sendlist[n] = temp_data.conn_fd; 104 | strcpy(namelist[n], temp_data.username); 105 | n++; 106 | } 107 | } 108 | 109 | i = 0; 110 | switch(mes->type){ 111 | case _ONLINE: 112 | case _OFFLINE: 113 | 114 | while(n > i){ 115 | psend->command_type = _MESSTIP; 116 | strcpy(psend->message.from_name, mes->from_name); 117 | strcpy(psend->message.send_name, namelist[i]); 118 | strcpy(psend->message.message_buf, mes->message_buf); 119 | psend->message.date = mes->date; 120 | psend->message.time = mes->time; 121 | psend->message.type = mes->type; 122 | 123 | send_data(sendlist[i], psend); 124 | i++; 125 | } 126 | break; 127 | } 128 | return 0; 129 | } 130 | 131 | 132 | int message_p_insert(message_t *mes){ 133 | 134 | chdir("/home/"); 135 | int ret = 0; 136 | FILE *fp; 137 | fp = fopen(mes->send_name,"ab"); 138 | if(fp == NULL){ 139 | return 0; 140 | } 141 | ret = fwrite(mes ,sizeof(message_t),1,fp); 142 | 143 | fclose(fp); 144 | 145 | return ret; 146 | } 147 | 148 | int leave_p_insert(send_t *precv){ 149 | 150 | chdir("/home/"); 151 | int ret = 0; 152 | FILE *fp; 153 | fp = fopen("LEAVE","ab"); 154 | if(fp == NULL){ 155 | return 0; 156 | } 157 | ret = fwrite(precv ,sizeof(send_t),1,fp); 158 | 159 | fclose(fp); 160 | 161 | return ret; 162 | } 163 | 164 | int server_friend_delet(send_t * precv){ 165 | send_t send_buf; 166 | send_t *psend = &send_buf; 167 | userinfo_service_t data; 168 | int id = 0; 169 | int i; 170 | memset(psend, 0, sizeof(send_t)); 171 | 172 | psend->conn_fd = precv->conn_fd; 173 | psend->command_type = _FRIENDDEL; 174 | strcpy(psend->userinfo.username, precv->userinfo.username); 175 | 176 | if(server_name_check(precv->friendinfo.username) < 0){ 177 | psend->userstatus = INVALID_USERINFO; 178 | 179 | send_data(psend->conn_fd, psend); 180 | } 181 | 182 | userinfo_p_seletname(precv->friendinfo.username, &data); 183 | 184 | id = data.userid; 185 | memset(&data, 0, sizeof(userinfo_service_t)); 186 | 187 | userinfo_p_seletname(psend->userinfo.username, &data); 188 | 189 | for(i = 0; data.friendslist[i] != 0; i++){ 190 | 191 | if(data.friendslist[i] == id){ 192 | data.friendslist[i] = data.friendslist[i+1]; 193 | } 194 | } 195 | 196 | psend->userstatus = VALID_USERINFO; 197 | 198 | userinfo_p_update(&data); 199 | 200 | send_data(psend->conn_fd, psend); 201 | return 0; 202 | } 203 | 204 | 205 | int server_friend_show(send_t *precv){ 206 | 207 | int i = 0; 208 | 209 | userinfo_service_t info[FRIENDSIZE]; 210 | userinfo_service_t data; 211 | 212 | int list[FRIENDSIZE]; 213 | 214 | send_t send_buf; 215 | send_t *psend = &send_buf; 216 | memset(psend, 0, sizeof(send_t)); 217 | memset(&data, 0, sizeof(userinfo_service_t)); 218 | memset(info, 0, sizeof(info)); 219 | memset(list, 0, sizeof(list)); 220 | 221 | userinfo_p_seletname(precv->userinfo.username, &data); //get user information 222 | 223 | 224 | int n = 0, flag = 0; 225 | for(i = 0; iconn_fd = precv->conn_fd; 240 | psend->userstatus = INVALID_USERINFO; 241 | psend->command_type = _FRIENDLIST; 242 | send_data(psend->conn_fd, psend); 243 | return 0; 244 | } 245 | else{ 246 | while(i < n){ 247 | //get friend all information 248 | userinfo_p_selet(list[i], &info[i]); 249 | psend->conn_fd = precv->conn_fd; 250 | 251 | psend->userstatus = VALID_USERINFO; 252 | //get friend_info_t information 253 | strcpy(psend->friendinfo.username, info[i].username); 254 | strcpy(psend->friendinfo.usersex, info[i].usersex); 255 | psend->friendinfo.status = info[i].status; 256 | psend->command_type = _FRIENDLIST; 257 | 258 | send_data(psend->conn_fd, psend); 259 | i++; 260 | } 261 | } 262 | 263 | return 0; 264 | } 265 | 266 | /* 267 | 268 | int server_group_show(int conn_fd){ 269 | 270 | char username[32] = {"\0"}; 271 | int group_buf[FRIENDSIZE]; 272 | int i = 0; 273 | 274 | group_info_t ginfo; 275 | group_info_t *gdata = &ginfo; 276 | 277 | userinfo_service_t info; 278 | userinfo_service_t *data = &info; 279 | 280 | // 281 | 282 | userinfo_p_seletname(username, data); //get user information 283 | 284 | send(conn_fd, data->friendslist, sizeof(data->friendslist),0); 285 | 286 | for(i = 0; data->friendslist[i] != 0; i++){ 287 | //copy grouplist, reblind pointer 288 | group_buf[i] = data->grouplist[i]; 289 | } 290 | 291 | //no group, return 292 | if(i == 0){ 293 | return 0; 294 | } 295 | 296 | while(i--){ 297 | 298 | //get group_info_t information 299 | server_group_selet(group_buf[i], gdata); 300 | 301 | send(conn_fd, gdata, sizeof(group_info_t) ,0); //send friend information 302 | 303 | } 304 | 305 | return 0; 306 | } 307 | */ 308 | 309 | int server_friend_find(send_t *precv){ 310 | userinfo_service_t data, temp_data; 311 | 312 | int ret = 1; 313 | int i; 314 | send_t send_buf; 315 | send_t *psend = &send_buf; 316 | 317 | strcpy(psend->friendinfo.username, precv->friendinfo.username); 318 | psend->command_type = _FRIENDFIND; 319 | psend->conn_fd = precv->conn_fd; 320 | strcpy(psend->userinfo.username, precv->userinfo.username); 321 | 322 | //name not exist 323 | ret = userinfo_p_seletname(precv->friendinfo.username, &data); 324 | 325 | userinfo_p_selet(data.userid, &temp_data); 326 | for(i = 0; temp_data.friendslist[i] != 0; i++){ 327 | if(data.userid == temp_data.friendslist[i]){ 328 | 329 | psend->userstatus = INVALID_USERINFO; 330 | send_data(precv->conn_fd, psend); 331 | return 0; 332 | } 333 | } 334 | 335 | if(!ret){ 336 | psend->userstatus = INVALID_USERINFO; 337 | } 338 | else{ 339 | psend->userstatus = VALID_USERINFO; 340 | psend->friendinfo.status = data.status; 341 | strcpy(psend->friendinfo.usersex, data.usersex); 342 | } 343 | 344 | send_data(precv->conn_fd, psend); 345 | return 0; 346 | } 347 | 348 | int server_chat_one(send_t *precv, int type){ 349 | 350 | int ret; 351 | message_t mes; 352 | send_t send_buf; 353 | send_t *psend = &send_buf; 354 | userinfo_service_t data; 355 | 356 | memset(&data, 0, sizeof(userinfo_service_t)); 357 | memset(&mes, 0, sizeof(message_t)); 358 | 359 | 360 | ret = userinfo_p_seletname(precv->message.send_name, &data); 361 | if(!ret){ 362 | psend->userstatus = INVALID_USERINFO; 363 | strcpy(psend->message.send_name, precv->message.send_name); 364 | psend->conn_fd = precv->conn_fd; 365 | psend->message.type = INVALID_USERINFO; 366 | psend->command_type = _CHATONE; 367 | return 0; 368 | } 369 | 370 | if(data.status == _OFFLINE){ 371 | data.leavemes_flag = _LEAVEMES; 372 | userinfo_p_update(&data); 373 | 374 | ret = userinfo_p_checkfile(precv->message.send_name); 375 | if(ret < 0){ 376 | creat_userdir(precv->message.send_name); 377 | } 378 | leave_p_insert(precv); 379 | } 380 | else{ 381 | strcpy(psend->message.from_name, precv->message.from_name); 382 | strcpy(psend->message.send_name, precv->message.send_name); 383 | strcpy(psend->message.message_buf, precv->message.message_buf); 384 | psend->message.date = precv->message.date; 385 | psend->userstatus = VALID_USERINFO; 386 | psend->message.time = precv->message.time; 387 | psend->message.type = _CHATONE; 388 | psend->command_type = type; 389 | psend->conn_fd = data.conn_fd; 390 | 391 | send_data(psend->conn_fd, psend); 392 | } 393 | 394 | return 0; 395 | } 396 | 397 | int server_friend_send(send_t *precv){ 398 | int ret = 0; 399 | userinfo_service_t data; 400 | message_t mes; 401 | 402 | 403 | send_t send_buf; 404 | send_t *psend = &send_buf; 405 | 406 | memset(&mes, 0, sizeof(data)); 407 | memset(&data, 0, sizeof(data)); 408 | 409 | strcpy(mes.from_name, precv->message.from_name); 410 | strcpy(mes.send_name, precv->message.send_name); 411 | strcpy(mes.message_buf, precv->message.message_buf); 412 | mes.type = _FRIENDASK; 413 | mes.date = precv->message.date; 414 | mes.time = precv->message.time; 415 | 416 | userinfo_p_seletname(precv->message.send_name, &data); 417 | psend->conn_fd = data.conn_fd; 418 | 419 | if(data.status == _OFFLINE){ 420 | data.leavemes_flag = _LEAVEMES; 421 | userinfo_p_update(&data); 422 | 423 | 424 | ret = userinfo_p_checkfile("LEAVE"); 425 | if(ret < 0){ 426 | creat_userdir("LEAVE"); 427 | } 428 | 429 | leave_p_insert(precv); 430 | 431 | } 432 | else{ 433 | strcpy(psend->message.from_name, precv->message.from_name); 434 | strcpy(psend->message.send_name, precv->message.send_name); 435 | strcpy(psend->message.message_buf, precv->message.message_buf); 436 | psend->message.date = mes.date; 437 | psend->message.time = mes.time; 438 | psend->message.type = _FRIENDASK; 439 | psend->command_type = _MESSTIP; 440 | 441 | send_data(psend->conn_fd, psend); 442 | } 443 | 444 | 445 | return 0; 446 | } 447 | 448 | 449 | int server_friend_add(send_t *precv){ 450 | 451 | userinfo_service_t *users; 452 | int ret = -1; 453 | send_t send_buf; 454 | send_t *psend = &send_buf; 455 | 456 | memset(psend, 0, sizeof(send_t)); 457 | users = (userinfo_service_t *)malloc(sizeof(userinfo_service_t)*ACCOUNTSIZE); 458 | 459 | strcpy(psend->userinfo.username, precv->userinfo.username); 460 | psend->command_type = _USERSALL; 461 | psend->conn_fd = precv->conn_fd; 462 | 463 | //get friend all information 464 | ret = userinfo_p_selectall(users); 465 | int i = 0; 466 | 467 | if(ret == 0){ 468 | //no friend, return 469 | psend->userstatus = INVALID_USERINFO; 470 | send_data(psend->conn_fd, psend); 471 | } 472 | else{ 473 | 474 | do{ 475 | psend->userstatus = VALID_USERINFO; 476 | strcpy(psend->userinfo.username, precv->userinfo.username); 477 | psend->command_type = _USERSALL; 478 | psend->conn_fd = precv->conn_fd; 479 | 480 | 481 | //get friend_info_t information 482 | strcpy(psend->friendinfo.username, users[i].username); 483 | strcpy(psend->friendinfo.usersex, users[i].usersex); 484 | psend->friendinfo.status = users[i].status; 485 | send_data(psend->conn_fd, psend); 486 | 487 | i++; 488 | }while(--ret); 489 | } 490 | 491 | free(users); 492 | return 0; 493 | } 494 | 495 | 496 | int server_friend_combain(send_t *precv){ 497 | 498 | int i, j; 499 | int flag = 0; 500 | int ret, ret1; 501 | int user1, user2; 502 | userinfo_service_t data, data1; 503 | 504 | memset(&data, 0, sizeof(data)); 505 | memset(&data1, 0, sizeof(data1)); 506 | 507 | if(precv->userstatus == VALID_USERINFO){ 508 | ret = userinfo_p_seletname(precv->message.from_name, &data); 509 | if(ret < 0){ 510 | my_err("name error", __LINE__); 511 | } 512 | 513 | ret1 = userinfo_p_seletname(precv->message.send_name, &data1); 514 | if(ret1 < 0){ 515 | my_err("name error", __LINE__); 516 | } 517 | 518 | user2 = data1.userid; 519 | user1 = data.userid; 520 | for(i = 0; data.friendslist[i] != 0; i++){ 521 | if(data.friendslist[i] == user2){ 522 | flag = 1; 523 | } 524 | } 525 | data.friendslist[i] = user2; 526 | 527 | for(j = 0; data1.friendslist[j] != 0; j++){ 528 | if(data.friendslist[j] == user1){ 529 | flag = 1; 530 | } 531 | } 532 | data1.friendslist[j] = user1; 533 | 534 | userinfo_p_update(&data); 535 | userinfo_p_update(&data1); 536 | 537 | 538 | } 539 | // 540 | server_chat_one(precv, _FRIENDASK); 541 | return 0; 542 | } 543 | -------------------------------------------------------------------------------- /service/chat.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/service/chat.o -------------------------------------------------------------------------------- /service/common/common.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-05 08:49 5 | * Filename : common.c 6 | * Description : 7 | * *****************************************************************************/ 8 | #include"common.h" 9 | 10 | 11 | char l_getc(){ 12 | char ch; 13 | scanf("%c",&ch); 14 | while(ch == '\n'){ 15 | scanf("%c",&ch); 16 | } 17 | return ch; 18 | } 19 | 20 | //Linux没有getch() 21 | 22 | int getch(void){ 23 | int c= 0; 24 | struct termios org_opts, new_opts; 25 | int res = 0; 26 | 27 | //将之前的设置备份 28 | res = tcgetattr(STDIN_FILENO, &org_opts); 29 | assert(res == 0); 30 | 31 | //设置新的设定 32 | memcpy(&new_opts, &org_opts, sizeof(new_opts)); 33 | new_opts.c_lflag &= ~(ICANON |ECHO | ECHOE | ECHOK |ECHONL | ECHOKE | ICRNL); 34 | tcsetattr(STDIN_FILENO, TCSANOW, &new_opts); 35 | c= getchar(); 36 | 37 | //恢复之前的设定 38 | res = tcsetattr(STDIN_FILENO, TCSANOW, &org_opts); 39 | assert(res == 0); 40 | return c; 41 | } 42 | 43 | //密码输入 44 | 45 | void getpwd(int maxlen, char *pwd){ 46 | int j= 0; 47 | char c; 48 | 49 | while((j < maxlen-2) && (c = getch()) != '\n'){ 50 | if(c != 127){ 51 | printf("*"); 52 | pwd[j++] = c; 53 | } 54 | else { 55 | if(j > 0){ 56 | j = j - 1; 57 | printf("\b \b"); 58 | } 59 | } 60 | } 61 | pwd[j++] = '\0'; 62 | pwd[j++] = '\n'; 63 | 64 | puts(" "); 65 | } 66 | 67 | void cl_stdin(){ 68 | char ch; 69 | while(( ch = getchar()) != '\n' && ch != EOF); 70 | } 71 | 72 | //合法输入 73 | int input_check(int input_len, char * input_buf){ 74 | char c; 75 | int i = 0; 76 | if(input_buf == NULL){ 77 | my_err("input check", __LINE__); 78 | } 79 | 80 | while((c = getchar()) != '\n' && (c != EOF) && (i < input_len-2)){ 81 | input_buf[i++] = c; 82 | } 83 | 84 | input_buf[i++] = '\0'; 85 | input_buf[i++] = '\n'; 86 | 87 | return i; 88 | } 89 | 90 | 91 | 92 | //自定义错误函数 93 | void my_err(const char* err_string, int line){ 94 | fprintf(stderr, "line:%d \n", line); 95 | perror(err_string); 96 | } 97 | 98 | //从套接字上读取一次数据 99 | 100 | int recv_data(int conn_fd, send_t *precv){ 101 | int len_remain = 0; 102 | 103 | len_remain = recv(conn_fd, precv, sizeof(send_t), 0); 104 | if(len_remain < 0){ 105 | my_err("recv", __LINE__); 106 | } 107 | 108 | printf("%d recv\n", precv->command_type); 109 | return len_remain; 110 | } 111 | 112 | 113 | //文件写入函数 114 | 115 | int file_write(int fd, const void * buf, int len){ 116 | int we; 117 | 118 | we = write(fd , buf, len); 119 | if(we < 0){ 120 | my_err("file_write", __LINE__); 121 | } 122 | 123 | return we; 124 | } 125 | 126 | //获取文件长度 127 | int file_len(int fd){ 128 | 129 | int length = 0; 130 | if(lseek(fd ,0 ,SEEK_END)== -1){ 131 | my_err("lseek set", __LINE__); 132 | } 133 | if((length = lseek(fd , 0 , SEEK_CUR)) == -1 ){ 134 | my_err("length" , __LINE__); 135 | } 136 | if(lseek(fd , 0 ,SEEK_SET)== -1){ 137 | my_err("lseek end" , __LINE__); 138 | } 139 | return length; 140 | } 141 | 142 | 143 | int file_read(int fd,int len,void* buf){ 144 | int re; 145 | if((re = read(fd , buf , len)) == -1){ 146 | my_err("c_read", __LINE__); 147 | } 148 | return re; 149 | } 150 | 151 | //显示接收信息 152 | /* 153 | void show_recv(int conn_fd, char *recv_buf, int buflen ){ 154 | int i; 155 | int ret; 156 | 157 | ret = my_recv(conn_fd, recv_buf, buflen); 158 | if(ret < 0){ 159 | my_err("recv faild", __LINE__); 160 | } 161 | 162 | for(i = 0; i < ret; i++){ 163 | printf("%c", recv_buf[i]); 164 | } 165 | } 166 | */ 167 | //获取系统当前日期 168 | user_date_t DateNow() { 169 | user_date_t curDate; 170 | time_t now; //实例化time_t结构 171 | struct tm *timeNow; //实例化tm结构指针 172 | time(&now); 173 | timeNow = localtime(&now); 174 | curDate.year=timeNow->tm_year+1900; 175 | curDate.month=timeNow->tm_mon+1; 176 | curDate.day=timeNow->tm_mday; 177 | 178 | return curDate; 179 | } 180 | 181 | //获取系统当前时间 182 | user_time_t TimeNow(){ 183 | user_time_t curTime; 184 | time_t now; //实例化time_t结构 185 | struct tm *timeNow; //实例化tm结构指针 186 | time(&now); 187 | timeNow = localtime(&now); 188 | curTime.hour=timeNow->tm_hour; 189 | curTime.minute=timeNow->tm_min; 190 | curTime.second=timeNow->tm_sec; 191 | 192 | return curTime; 193 | } 194 | 195 | int ser_atoi(char * buf){ //atoi 196 | 197 | int ret; 198 | ret = atoi(buf); 199 | 200 | return ret; 201 | } 202 | /* 203 | int recv_data(int conn_fd, send_t * psend){ 204 | int n = 0; 205 | send_t recv_buf; 206 | char *precv = (char*) &recv_buf; 207 | 208 | if(n == 0){ 209 | n = recv(conn_fd, &recv_buf, sizeof(recv_buf), 0); 210 | if(n <= 0){ 211 | my_err("recv_", __LINE__); 212 | } 213 | else if(n == 0){ 214 | return 0; 215 | } 216 | precv = (char*)&recv_buf; 217 | } 218 | 219 | memcpy(psend, precv, sizeof(send_t)); 220 | 221 | return sizeof(send_t); 222 | } 223 | */ 224 | 225 | //发送数据 226 | int send_data(int conn_fd, send_t * psend){ 227 | 228 | int ret; 229 | if((ret = send(conn_fd, psend, sizeof(send_t), 0)) < 0){ 230 | my_err("send",__LINE__); 231 | } 232 | 233 | 234 | printf("%d send\n", psend->command_type); 235 | memset(psend, 0, sizeof(send_t)); 236 | return ret; 237 | } 238 | 239 | 240 | 241 | //将字符串str就地转换为大写字符串,并返回字符串头指针 242 | char *Str2Upper(char *str) { 243 | if (NULL == str) 244 | return NULL; 245 | else { 246 | char *p = str; 247 | while ('\0' != *p) { 248 | if (*p >= 'a' && *p <= 'z') 249 | *p -= 32; 250 | p++; 251 | } 252 | return str; 253 | } 254 | } 255 | 256 | //显示消息 257 | /*void show_message(int conn_fd){ 258 | message_t leave; 259 | message_t * pmess = &leave; 260 | 261 | recv(conn_fd, pmess, sizeof(leave), 0); 262 | printf("%d-%d-%d %2d:%2d:%2d\n",pmess->date.year,pmess->date.month,pmess->date.day,pmess->time.hour,pmess->time.minute,pmess->time.second); 263 | printf("<%s>: ", pmess->send_name); 264 | puts(pmess->message_buf); 265 | puts(" "); 266 | }*/ 267 | 268 | /* 269 | void recv_message(int conn_fd, message_t * data){ 270 | 271 | if(recv(conn_fd, data, sizeof(message_t), 0) < 0){ 272 | send_data(conn_fd,(char *)("n")); 273 | return; 274 | } 275 | else{ 276 | send_data(conn_fd, (char*)("y")); 277 | } 278 | 279 | } 280 | 281 | 282 | void send_message(int conn_fd, char * send_name){ 283 | 284 | static char send_buf[BUFSIZE-36]; 285 | static char recv_buf[BUFSIZE]; 286 | int flag = INVALID_INPUT; 287 | 288 | message_t info; 289 | message_t *data = &info; 290 | 291 | data->date = DateNow(); 292 | data->time = TimeNow(); 293 | strcpy(data->send_name, send_name); 294 | 295 | input_check(BUFSIZE-36, send_buf); 296 | strcpy(data->message_buf, send_buf); 297 | do{ 298 | 299 | if(send(conn_fd, data, sizeof(info), 0) < 0){ 300 | my_err("send", __LINE__); 301 | } 302 | 303 | if(my_recv(conn_fd, recv_buf,sizeof(recv_buf)) < 0){ 304 | my_err("data is too long", __LINE__); 305 | } 306 | 307 | if(recv_buf[0] == VALID_INPUT){ 308 | flag = VALID_INPUT; 309 | } 310 | else{ 311 | printf("error, input again,\n"); 312 | flag = INVALID_INPUT; 313 | input_check(BUFSIZE-36, send_buf); 314 | strcpy(data->message_buf, send_buf); 315 | } 316 | }while(flag == INVALID_INPUT); 317 | 318 | } 319 | */ 320 | -------------------------------------------------------------------------------- /service/common/common.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-05 09:10 5 | * Filename : common.h 6 | * Description : 7 | * *****************************************************************************/ 8 | #ifndef COMMON_H_ 9 | #define COMMON_H_ 10 | 11 | #include 12 | #include 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 | #include"struct.h" 29 | 30 | int file_write(int fd, const void * data, int len); //写入文件 31 | 32 | int file_read(int fd, int len, void * data); //读出文件 33 | 34 | int file_len(int fd);// 获得文件长度 35 | 36 | void my_err(const char * err_string, int line); 37 | 38 | int recv_data(int conn_fd, send_t *precv); 39 | 40 | char l_getc(); //从键盘获取一个字符,避免回车 41 | 42 | void cl_stdin(); //清空输入缓冲 43 | 44 | void getpwd(int maxlen, char *pwd); 45 | 46 | int input_check(int input_len, char *input_buf); //输入合法性检查 47 | 48 | void show_recv(int conn_fd, char * recv_buf, int buflen); //显示接收消息 49 | 50 | user_date_t DateNow(); //获取当前日期 51 | 52 | user_time_t TimeNow(); //获取当前时间 53 | 54 | char* Str2Upper(char * entUpperName ); //字符串转化 55 | 56 | int ser_atoi(char *buf); //将字符转化为int 57 | 58 | int send_data(int conn_fd, send_t * psend); //发送数据 59 | 60 | void send_message(int conn_fd, char * send_name); //send message,need servive answer 61 | 62 | void show_message(int conn_fd); //recvie and show message 63 | 64 | void recv_message(int conn_fd, message_t * data); 65 | 66 | #endif 67 | 68 | -------------------------------------------------------------------------------- /service/common/common.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/service/common/common.o -------------------------------------------------------------------------------- /service/common/struct.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-13 21:12 5 | * Filename : struct.h 6 | * Description : 7 | * *****************************************************************************/ 8 | 9 | 10 | #define ACCOUNTSIZE 100 //最大连接数量 11 | #define FRIENDSIZE 50 //最大好友数 12 | #define BUFSIZE 1024 //最大缓冲区 13 | #define GROUPSIZE 50 //最大群规模 14 | #define GROUPCOUNT 20 //最大群数量 15 | 16 | #define INVALID_USERINFO 'n' //无效信息 17 | #define VALID_USERINFO 'y' // 有效信息 18 | 19 | 20 | //用户操作类型 21 | 22 | #define _USERADD 11 //用户注册 23 | #define _USERCHA 12 //更改用户基本信息 24 | #define _USERDEL 13 //删除用户 25 | #define _USERONL 14 //上线操作 26 | #define _USEROFFL 15 //下线操作 27 | 28 | #define _LEAVEMES 's' //有离线消息 29 | #define _NOMESSAGE 'e' //无离线消息 30 | #define _ONLINE 'o' //为上线状态 31 | #define _OFFLINE 'd' //为离线状态 32 | 33 | #define _FRIENDLIST 21 //用户好友列表 34 | #define _FRIENDFIND 22 //查找用户 35 | #define _FRIENDASK 23 //好友请求 36 | #define _FRIENDSEND 24 //add friend 37 | #define _FRIENDDEL 25 //delet friend 38 | #define _USERSALL 26 39 | 40 | #define _GROUPLIST 31 //用户群列表 41 | #define _GROUPFIND 32 //查找群 42 | #define _GROUPOWN 33 //creat a group 43 | #define _GROUPSEND 33 //add group 44 | #define _GROUPDEL 34 //group quit 45 | #define _GROUPDISMISS 35 //group dismiss 46 | #define _GROUPSALL 36 47 | 48 | #define _RECALL 99 //返回到主目录 49 | #define _SYSTEMTIP 91 //system tip 50 | 51 | #define _INPUTNAME 41 52 | #define _INPUTPASSWD 42 53 | #define _INPUTSEX 43 54 | #define _INPUTFRIEND 44 55 | #define _INPUTGROUP 45 56 | 57 | #define _CHATGRO 52 58 | #define _CHATONE 51 59 | #define _LEAVEMESS 53 //查看离线消息 60 | #define _MESSTIP 54 61 | 62 | #define LOADSTART 't' 63 | #define LOADOVER 'r' 64 | #define LOADWRONG 'g' 65 | 66 | #define _UPLOAD 61 67 | #define _DOWNLOAD 62 68 | #define _LOADERR 63 69 | 70 | #define LISTENQ 12 71 | #define SERV_PORT 4507 72 | 73 | typedef struct{ 74 | char username[32]; 75 | char usersex[10]; 76 | char status; 77 | }friend_info_t; 78 | 79 | typedef struct{ 80 | char groupname[32]; 81 | char owner[32]; 82 | int groupid; 83 | int size; 84 | friend_info_t member[GROUPSIZE]; 85 | }group_info_t; 86 | 87 | typedef struct { 88 | int year; 89 | int month; 90 | int day; 91 | }user_date_t; 92 | 93 | typedef struct{ 94 | int hour; 95 | int minute; 96 | int second; 97 | }user_time_t; 98 | 99 | typedef struct{ 100 | user_date_t date; 101 | user_time_t time; 102 | char send_name[32]; 103 | char from_name[32]; 104 | char message_buf[BUFSIZE-36]; 105 | 106 | }message_t; 107 | 108 | typedef struct{ 109 | char filename[32]; 110 | char filepath[BUFSIZE]; 111 | int count; 112 | int lenth; 113 | }fileinfo_message_t; 114 | 115 | typedef struct{ 116 | 117 | int conn_fd; 118 | int flag_recv; 119 | }thread_t; 120 | 121 | typedef struct { 122 | 123 | user_date_t date; 124 | user_time_t time; 125 | }user_logtime_t; 126 | 127 | 128 | typedef struct{ 129 | 130 | char markname[32]; 131 | int filelen; 132 | int sendlen; 133 | int givelen; 134 | int leftlen; 135 | 136 | }fileinfo_t; 137 | 138 | 139 | typedef struct{ 140 | char filepath[32]; 141 | char markname[32]; 142 | int filelen; 143 | int sendlen; 144 | int givelen; 145 | int sendtimes; 146 | int leftlen; 147 | char buf[BUFSIZE]; 148 | }file_t; 149 | 150 | 151 | 152 | typedef struct{ 153 | int userid; 154 | char username[32]; 155 | char userpasswd[32]; 156 | char usersex[10]; 157 | char leavemes_flag; 158 | char status; 159 | int friendslist[FRIENDSIZE]; 160 | int grouplist[GROUPCOUNT]; 161 | int conn_fd; 162 | user_logtime_t ontime; 163 | user_logtime_t offtime; 164 | }userinfo_service_t; 165 | 166 | 167 | typedef struct{ 168 | userinfo_service_t userinfo; 169 | friend_info_t friendinfo; 170 | group_info_t group_info; 171 | user_date_t data; 172 | user_time_t time; 173 | message_t message; 174 | fileinfo_message_t messageinfo; 175 | fileinfo_t file; 176 | int conn_fd; 177 | char check_answer; 178 | int input_check; 179 | char userstatus; 180 | int command_type; 181 | char buf[BUFSIZE]; 182 | }send_t; 183 | 184 | -------------------------------------------------------------------------------- /service/list.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/service/list.h -------------------------------------------------------------------------------- /service/service.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-03 20:31 5 | * Filename : service.c 6 | * Description : 7 | * *****************************************************************************/ 8 | #include "service.h" 9 | #define EPOLL_MAX 1024 10 | 11 | int *service(send_t *precv){ 12 | 13 | switch(precv->command_type){ 14 | 15 | case _USERADD: 16 | 17 | serve_user_register(precv); 18 | 19 | break; 20 | case _USERCHA: 21 | 22 | serve_user_update(precv); 23 | break; 24 | case _USERDEL: 25 | 26 | serve_user_delet(precv); 27 | break; 28 | case _USERONL: 29 | 30 | serve_user_login(precv); 31 | 32 | break; 33 | case _USEROFFL: 34 | 35 | serve_user_logoff(precv); 36 | break; 37 | case _FRIENDLIST: 38 | 39 | server_friend_show(precv); 40 | 41 | break; 42 | case _FRIENDDEL: 43 | 44 | server_friend_delet(precv); 45 | 46 | break; 47 | case _USERSALL: 48 | server_friend_add(precv); 49 | 50 | break; 51 | case _UPLOAD: 52 | 53 | file_upload(precv); 54 | 55 | break; 56 | case _DOWNLOAD: 57 | 58 | file_download(precv); 59 | 60 | break; 61 | case _FRIENDSEND: 62 | server_friend_send(precv); 63 | break; 64 | 65 | case _FRIENDFIND: 66 | 67 | server_friend_find(precv); 68 | break; 69 | case _FRIENDASK: 70 | 71 | server_friend_combain(precv); 72 | break; 73 | 74 | case _CHATONE: 75 | server_chat_one(precv, _CHATONE); 76 | break; 77 | /* case _GROUPLIST: 78 | server_group_show(conn_fd); 79 | break; 80 | case _GROUPSEND: 81 | server_group_add(conn_fd); 82 | break; 83 | case _CHATONE: 84 | server_chat_one(conn_fd); // 85 | break; 86 | case _CHAT: 87 | server_message_leave(conn_fd); 88 | break; 89 | case 27: 90 | server_message_temp(conn_fd); 91 | break; 92 | case 28: 93 | server_chat_group(conn_fd); // 94 | break;*/ 95 | default: 96 | break; 97 | } 98 | pthread_exit(0); 99 | return NULL; 100 | } 101 | 102 | int main(){ 103 | 104 | int num = 0; 105 | struct sockaddr_in cli_addr, serv_addr; 106 | struct epoll_event ev, events[EPOLL_MAX]; 107 | char flag_recv = _ONLINE; 108 | int sock_fd, conn_fd; 109 | int epfd, nfds; 110 | int i; 111 | int optval; 112 | 113 | send_t recv_b; 114 | send_t *precv = &recv_b; 115 | pthread_t id; 116 | 117 | socklen_t cli_len; 118 | 119 | 120 | epfd = epoll_create(EPOLL_MAX); 121 | 122 | //创建一个TCP套接字 123 | sock_fd = socket(AF_INET, SOCK_STREAM, 0); 124 | 125 | if(sock_fd < 0){ 126 | my_err("socket", __LINE__); 127 | } 128 | 129 | //设置该套接字使之可以重新绑定端口 130 | optval = 1; 131 | if(setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0){ 132 | my_err("setsokopt", __LINE__); 133 | } 134 | 135 | 136 | //套接字加入epoll 137 | ev.data.fd = sock_fd; 138 | ev.events = EPOLLIN; 139 | epoll_ctl(epfd, EPOLL_CTL_ADD, sock_fd, &ev); 140 | 141 | 142 | //初始化服务器端地址 143 | memset(&serv_addr, 0, sizeof(struct sockaddr_in)); 144 | serv_addr.sin_family = AF_INET; 145 | serv_addr.sin_port = htons(SERV_PORT); 146 | serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); 147 | 148 | //将套接字绑定到本地端口 149 | if(bind(sock_fd, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr_in)) < 0){ 150 | my_err("bind",__LINE__); 151 | } 152 | 153 | //将套接字转化为监听套接字 154 | if(listen(sock_fd, LISTENQ) < 0){ 155 | my_err("listen",__LINE__); 156 | } 157 | 158 | cli_len = sizeof(struct sockaddr_in); 159 | 160 | system("clear"); 161 | puts("service is working..."); 162 | 163 | while(1){ 164 | 165 | //设置永久阻塞 166 | nfds = (epoll_wait(epfd, events, 1, -1)); 167 | 168 | if(nfds < 0){ 169 | break; 170 | } 171 | 172 | for(i = 0; i < nfds; i++){ 173 | //套接字描述符改变_连接请求 174 | if(events[i].data.fd == sock_fd){ 175 | 176 | //通过accept接受客户端的连接请求,并返回连接套接字用于手法数据 177 | if((conn_fd = accept(sock_fd, (struct sockaddr*)&cli_addr, &cli_len)) == -1){ 178 | exit(0); 179 | //my_err("accept", __LINE__); 180 | } 181 | 182 | printf("client from [IP:%s] is working.%d\n\n",inet_ntoa(cli_addr.sin_addr),num); 183 | num++; 184 | 185 | //初始化客户端fd 186 | ev.data.fd = conn_fd; 187 | ev.events = EPOLLIN; 188 | epoll_ctl(epfd, EPOLL_CTL_ADD, conn_fd, &ev); 189 | 190 | } 191 | 192 | //有读事件发生 193 | else if(events[i].events & EPOLLIN){ 194 | int fd; 195 | if(recv_data(events[i].data.fd, precv) <= 0 || precv->command_type == _USEROFFL){ 196 | 197 | printf("\n\nclient: %d disconnect\n", events[i].data.fd); 198 | //strcpy(precv->friendinfo.username, username); 199 | precv->conn_fd = events[i].data.fd; 200 | fd = events[i].data.fd; 201 | serve_user_logoff(precv); 202 | 203 | epoll_ctl(epfd, EPOLL_CTL_DEL, fd, &ev); 204 | close(fd); 205 | 206 | continue; 207 | } 208 | 209 | precv->conn_fd = events[i].data.fd; 210 | fd = events[i].data.fd; 211 | //线程参数结构体赋值 212 | 213 | //下线则解除 214 | void *flag ; 215 | pthread_create(&id, NULL,(void*)service,precv);//新开线程去处理事件*/ 216 | pthread_join(id,&flag); 217 | } 218 | } 219 | } 220 | 221 | close (epfd); 222 | shutdown(sock_fd, SHUT_RD); 223 | return 0; 224 | } 225 | 226 | int file_upload(send_t * precv){ 227 | 228 | int ret; 229 | int fd; 230 | file_t file_buf; 231 | file_t *pfile = &file_buf; 232 | 233 | fd = open(precv->file.markname, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP); 234 | if(fd < 0){ 235 | my_err("file_open", __LINE__); 236 | } // 开始标志 237 | 238 | if(precv->userstatus == LOADSTART){ 239 | printf("user start recving file :%s\n", precv->file.markname); 240 | printf("size of the file: %dB\n", precv->file.filelen); 241 | }else{ 242 | 243 | ret = file_write(fd, precv->buf, precv->file.sendlen); 244 | pfile->givelen = file_len(fd); 245 | 246 | // printf("recvlen : %dB\n\n", precv->file.givelen); 247 | // 248 | if(precv->userstatus == LOADOVER){ 249 | if(pfile->givelen != precv->file.givelen){ 250 | my_err("recv_len",__LINE__); 251 | } 252 | 253 | 254 | if(pfile->givelen == precv->file.filelen){ 255 | puts("download over"); 256 | } 257 | else{ 258 | my_err("download failed", __LINE__); 259 | } 260 | } 261 | } 262 | close(fd); 263 | return 0; 264 | } 265 | 266 | int file_download(send_t* precv){ 267 | 268 | int fd; 269 | file_t file_buf; 270 | file_t *pfile = &file_buf; 271 | send_t send_buf; 272 | send_t *psend = &send_buf; 273 | char *temp_buf; 274 | chdir("/home/"); 275 | 276 | temp_buf = (char*)malloc(1024*1024); 277 | char path[32] = {"/home/"}; 278 | 279 | strcat(path, precv->file.markname); 280 | psend->command_type = _DOWNLOAD; 281 | psend->conn_fd = precv->conn_fd; 282 | 283 | fd = open(path, O_RDWR ); 284 | if(fd < 0){ 285 | psend->userstatus = LOADWRONG; 286 | send_data(psend->conn_fd, psend); 287 | my_err("open_wrong", __LINE__); 288 | } 289 | 290 | //get the file information , size or ... 291 | //copy file into send_buf; 292 | pfile->filelen = file_len(fd); 293 | pfile->leftlen = pfile->filelen; 294 | pfile->givelen = 0; 295 | 296 | file_read(fd, pfile->filelen, temp_buf); 297 | puts(""); 298 | puts(":file send to user"); 299 | printf("markname:%s\n", precv->file.markname); 300 | printf("size:%dB\n", pfile->filelen); 301 | puts(""); 302 | psend->userstatus = LOADSTART; 303 | 304 | while(1){ 305 | chdir("/home/"); 306 | 307 | strcpy(psend->file.markname, precv->file.markname); 308 | psend->file.filelen = pfile->filelen; 309 | psend->command_type = _DOWNLOAD; 310 | psend->conn_fd = precv->conn_fd; 311 | 312 | if(psend->userstatus == LOADSTART){ 313 | pfile->sendlen = send_data(psend->conn_fd,psend); 314 | 315 | if(pfile->sendlen < 0){ 316 | my_err("download_file", __LINE__); 317 | } 318 | sleep(1); 319 | continue; 320 | } 321 | 322 | if((pfile->leftlen <= 0) || (pfile->givelen >= pfile->filelen)){ 323 | break; 324 | } 325 | 326 | if(pfile->leftlen >= BUFSIZE){ 327 | psend->file.givelen = pfile->givelen; 328 | memcpy(psend->buf, temp_buf+pfile->givelen, BUFSIZE); 329 | psend->file.sendlen = BUFSIZE; 330 | pfile->sendlen = send_data(precv->conn_fd, psend); 331 | 332 | if(pfile->sendlen <= 0){ 333 | my_err("download_file", __LINE__); 334 | //can make mark; 335 | } 336 | } 337 | else{ 338 | memcpy(psend->buf, temp_buf+pfile->givelen, pfile->leftlen); 339 | psend->userstatus = LOADOVER; 340 | psend->file.givelen = pfile->givelen + pfile->leftlen; 341 | psend->file.sendlen = pfile->leftlen; 342 | pfile->sendlen = send_data(precv->conn_fd, psend); 343 | 344 | if(pfile->sendlen <= 0){ 345 | my_err("download_failed", __LINE__); 346 | } 347 | 348 | pfile->givelen = pfile->givelen + pfile->leftlen; 349 | 350 | break; 351 | } 352 | 353 | usleep(100000); 354 | 355 | pfile->givelen += BUFSIZE; 356 | pfile->leftlen = pfile->filelen - pfile->givelen; 357 | }//while 358 | 359 | close(fd); 360 | free(temp_buf); 361 | 362 | if(pfile->givelen == pfile->filelen){ 363 | printf("user recved %dB, file len %dB\n", pfile->filelen, pfile->filelen); 364 | puts("user download over"); 365 | return 0; 366 | } 367 | else{ 368 | my_err("upload failed", __LINE__); 369 | } 370 | return 0; 371 | } 372 | -------------------------------------------------------------------------------- /service/service.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Author : Sequin_YF 3 | * Email : catherine199787@outlook.com 4 | * Last modified : 2016-08-09 10:52 5 | * Filename : service.h 6 | * Description : 7 | * *****************************************************************************/ 8 | #ifndef SERVICE_H 9 | #define SERVICE_H 10 | 11 | 12 | #include"../common/common.h" 13 | #include "list.h" 14 | 15 | 16 | typedef struct { 17 | char entyName[32]; 18 | long key; 19 | } entity_key_t; 20 | 21 | typedef struct entity_key_node{ 22 | entity_key_t data; 23 | struct entity_key_node *prev; 24 | struct entity_key_node *next; 25 | }entkey_node_t, *entkey_list_t; 26 | 27 | 28 | int *servive(thread_t *data); 29 | 30 | int serve_user_login(send_t *precv); //登录函数 31 | 32 | int serve_user_logoff(send_t *precv); //下线函数 33 | 34 | int serve_user_register(send_t *precv); //注册函数 35 | 36 | int serve_user_update(send_t *precv); //基本信息更改函数 37 | 38 | int serve_user_delet(send_t *precv); //清楚用户 39 | 40 | int server_message_leave(int conn_df); //用户查看离线消息 41 | 42 | int server_friend_delet(send_t * precv); 43 | 44 | int server_friend_show(send_t * precv); //用户查看好友列表 45 | 46 | int server_friend_combain(send_t *precv); 47 | 48 | int server_friend_add(send_t *precv); //用户添加好友请求 49 | 50 | int server_group_show(int conn_fd); //show group list 51 | 52 | int server_friend_find(send_t * precv); 53 | 54 | int server_friend_send(send_t *precv); 55 | 56 | int server_group_add(int conn_fd); //add group &search group 57 | 58 | int server_message_temp(int conn_fd); //show uncheck message since user sign in 59 | 60 | int server_chat_one(send_t *recv, int type); //chat one to one 61 | 62 | int server_chat_group(int conn_fd); //chat to group 63 | 64 | int server_group_owner(int conn_fd); //build a group 65 | 66 | int server_file_check(char * filename); //check file exit, or creat a new file;:w 67 | 68 | int server_file_select(char *filename, fileinfo_message_t * fdata); //init pointer fdata by filename 69 | 70 | int server_message_selet(char * filename, message_t *data); //read message by filename 71 | 72 | int server_group_selet(int groupid, group_info_t *data); 73 | 74 | int userinfo_p_checkfile(const char * username); //检查文件是否存在 75 | 76 | int message_p_update(message_t * data); 77 | 78 | int userinfo_p_update(userinfo_service_t *data); //修改用户信息 79 | 80 | int message_tip(message_t *mes); 81 | 82 | int message_p_insert(message_t *data); 83 | 84 | int userinfo_p_insert(const userinfo_service_t *data); //写入用户信息 85 | 86 | int userinfo_p_delet(int id); //注销用户 87 | 88 | int userinfo_p_deletid(int id); 89 | 90 | int userinfo_p_selectall(userinfo_service_t users[]);//get all user infomation 91 | 92 | int creat_userdir(const char *username); //创建用户文件 93 | 94 | int server_name_check(char *username); 95 | 96 | int userinfo_p_selet(int id, userinfo_service_t *data); //获取用户信息 97 | 98 | int userinfo_p_seletname(const char *username, userinfo_service_t *data); //用户名获取用户信息 99 | 100 | int leave_p_insert(send_t *precv); 101 | 102 | //int message_p_select(const char *mesfilename); 103 | 104 | inline long EntKey_Srv_CompNewKey(char entName[]); //根据传入的实体名entName,为新实体分配一个唯一的主键。函数返回值为取新实体的主键值 105 | 106 | inline long EntKey_Srv_CompNewKeys(char entName[], int count); //根据传入的实体名entName及实体个数count,为这个count个新实体分配一个长度为count的主键值区间,使得每个新实体在该区间内都可以分配到 唯一的 主键。返回值为该主键区间的最小值 107 | 108 | inline void EntKey_Srv_Add2List(entkey_list_t keyList, long key); //将主键key保存到主键链表keyList中 109 | 110 | inline int EntKey_Srv_CheckExist(entkey_list_t keyList, long key); //在主键链表keyList中检查key是否存在,返回1存在,否则0 111 | 112 | long EntKey_Perst_GetNewKeys(char entName[], int count); //根据传入的实体名entName及实体个数count,为这个count个新实体分配一个长度为count的主键值区间,使得每个新实体在该区间内都可以分配到 唯一的 主键。返回值为该主键区间的最小值 113 | 114 | int file_upload(send_t *precv); 115 | 116 | int file_download(send_t *precv); 117 | 118 | #endif 119 | -------------------------------------------------------------------------------- /service/service.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yifeiwang-ep/CHATROOM_C/892b82522f8dd491720e9a139bd6f4a7c8f224e9/service/service.o -------------------------------------------------------------------------------- /service/tags: -------------------------------------------------------------------------------- 1 | !_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ 2 | !_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ 3 | !_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ 4 | !_TAG_PROGRAM_NAME Exuberant Ctags // 5 | !_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ 6 | !_TAG_PROGRAM_VERSION 5.8 // 7 | ACCOUNTSIZE common/struct.h 10;" d 8 | BUFSIZE common/struct.h 12;" d 9 | CC Makefile /^CC = gcc$/;" m 10 | COMMON_H_ common/common.h 9;" d 11 | C_FLAGS Makefile /^C_FLAGS = -c.$/;" m 12 | C_OBJECTS Makefile /^C_OBJECTS = $(patsubst %.c, %.o, $(C_SOURCES))$/;" m 13 | C_SOURCES Makefile /^C_SOURCES = $(shell find . -name "*.c")$/;" m 14 | DateNow common/common.c /^user_date_t DateNow() {$/;" f 15 | ENTITY_KEY_FILE EntityKey.c /^static const char ENTITY_KEY_FILE[] = ".\/EntityKey.dat";$/;" v file: 16 | EPOLL_MAX service.c 9;" d file: 17 | EntKey_Perst_GetNewKeys EntityKey.c /^long EntKey_Perst_GetNewKeys(char entName[], int count) {$/;" f 18 | EntKey_Srv_Add2List EntityKey.c /^inline void EntKey_Srv_Add2List(entkey_list_t keyList, long key) {$/;" f 19 | EntKey_Srv_CheckExist EntityKey.c /^inline int EntKey_Srv_CheckExist(entkey_list_t keyList, long key){$/;" f 20 | EntKey_Srv_CompNewKey EntityKey.c /^inline long EntKey_Srv_CompNewKey(char entName[]) {$/;" f 21 | EntKey_Srv_CompNewKeys EntityKey.c /^inline long EntKey_Srv_CompNewKeys(char entName[], int count) {$/;" f 22 | FRIENDSIZE common/struct.h 11;" d 23 | GROUPCOUNT common/struct.h 14;" d 24 | GROUPSIZE common/struct.h 13;" d 25 | INVALID_USERINFO common/struct.h 16;" d 26 | LISTENQ common/struct.h 70;" d 27 | LIST_H_ list.h 9;" d 28 | LOADOVER common/struct.h 63;" d 29 | LOADSTART common/struct.h 62;" d 30 | LOADWRONG common/struct.h 64;" d 31 | List_AddHead list.h 51;" d 32 | List_AddTail list.h 59;" d 33 | List_DelNode list.h 88;" d 34 | List_Destroy list.h 43;" d 35 | List_ForEach list.h 102;" d 36 | List_Free list.h 31;" d 37 | List_FreeNode list.h 95;" d 38 | List_Init list.h 25;" d 39 | List_InsertAfter list.h 75;" d 40 | List_InsertBefore list.h 67;" d 41 | List_IsEmpty list.h 83;" d 42 | List_Paging list.h 108;" d 43 | PROGRAM Makefile /^PROGRAM = Service$/;" m 44 | Pageing_CurPage list.h 163;" d 45 | Pageing_TotalPages list.h 165;" d 46 | Pagination_t list.h /^}Pagination_t;$/;" t typeref:struct:__anon13 47 | Paging_Locate_FirstPage list.h 121;" d 48 | Paging_Locate_LastPage list.h 126;" d 49 | Paging_Locate_OffsetPage list.h 138;" d 50 | SERVICE_H service.h 9;" d 51 | SERV_PORT common/struct.h 71;" d 52 | Str2Upper common/common.c /^char *Str2Upper(char *str) {$/;" f 53 | TimeNow common/common.c /^user_time_t TimeNow(){$/;" f 54 | USER_FILE account.c 11;" d file: 55 | VALID_USERINFO common/struct.h 17;" d 56 | _CHATGRO common/struct.h 57;" d 57 | _CHATONE common/struct.h 58;" d 58 | _DOWNLOAD common/struct.h 67;" d 59 | _FRIENDASK common/struct.h 35;" d 60 | _FRIENDDEL common/struct.h 37;" d 61 | _FRIENDFIND common/struct.h 34;" d 62 | _FRIENDLIST common/struct.h 33;" d 63 | _FRIENDSEND common/struct.h 36;" d 64 | _GROUPDEL common/struct.h 44;" d 65 | _GROUPDISMISS common/struct.h 45;" d 66 | _GROUPFIND common/struct.h 41;" d 67 | _GROUPLIST common/struct.h 40;" d 68 | _GROUPOWN common/struct.h 42;" d 69 | _GROUPSALL common/struct.h 46;" d 70 | _GROUPSEND common/struct.h 43;" d 71 | _INPUTFRIEND common/struct.h 54;" d 72 | _INPUTGROUP common/struct.h 55;" d 73 | _INPUTNAME common/struct.h 51;" d 74 | _INPUTPASSWD common/struct.h 52;" d 75 | _INPUTSEX common/struct.h 53;" d 76 | _LEAVEMES common/struct.h 28;" d 77 | _LEAVEMESS common/struct.h 59;" d 78 | _LOADERR common/struct.h 68;" d 79 | _MESSTIP common/struct.h 60;" d 80 | _NOMESSAGE common/struct.h 29;" d 81 | _OFFLINE common/struct.h 31;" d 82 | _ONLINE common/struct.h 30;" d 83 | _RECALL common/struct.h 48;" d 84 | _SYSTEMTIP common/struct.h 49;" d 85 | _UPLOAD common/struct.h 66;" d 86 | _USERADD common/struct.h 22;" d 87 | _USERCHA common/struct.h 23;" d 88 | _USERDEL common/struct.h 24;" d 89 | _USEROFFL common/struct.h 26;" d 90 | _USERONL common/struct.h 25;" d 91 | _USERSALL common/struct.h 38;" d 92 | buf common/struct.h /^ char buf[BUFSIZE];$/;" m struct:__anon10 93 | buf common/struct.h /^ char buf[BUFSIZE];$/;" m struct:__anon12 94 | check_answer common/struct.h /^ char check_answer;$/;" m struct:__anon12 95 | cl_stdin common/common.c /^void cl_stdin(){$/;" f 96 | command_type common/struct.h /^ int command_type;$/;" m struct:__anon12 97 | conn_fd common/struct.h /^ int conn_fd;$/;" m struct:__anon11 98 | conn_fd common/struct.h /^ int conn_fd;$/;" m struct:__anon12 99 | conn_fd common/struct.h /^ int conn_fd;$/;" m struct:__anon7 100 | count common/struct.h /^ int count;$/;" m struct:__anon6 101 | creat_userdir account.c /^int creat_userdir(const char *string){$/;" f 102 | curPos list.h /^ void *curPos;$/;" m struct:__anon13 103 | data common/struct.h /^ user_date_t data;$/;" m struct:__anon12 104 | data service.h /^ entity_key_t data;$/;" m struct:entity_key_node 105 | date common/struct.h /^ user_date_t date;$/;" m struct:__anon5 106 | date common/struct.h /^ user_date_t date;$/;" m struct:__anon8 107 | day common/struct.h /^ int day;$/;" m struct:__anon3 108 | entity_key_node service.h /^typedef struct entity_key_node{$/;" s 109 | entity_key_t service.h /^} entity_key_t;$/;" t typeref:struct:__anon14 110 | entkey_list_t service.h /^}entkey_node_t, *entkey_list_t;$/;" t typeref:struct:entity_key_node 111 | entkey_node_t service.h /^}entkey_node_t, *entkey_list_t;$/;" t typeref:struct:entity_key_node 112 | entyName service.h /^ char entyName[32];$/;" m struct:__anon14 113 | file common/struct.h /^ fileinfo_t file;$/;" m struct:__anon12 114 | file_download service.c /^int file_download(send_t* precv){$/;" f 115 | file_len common/common.c /^int file_len(int fd){$/;" f 116 | file_read common/common.c /^int file_read(int fd,int len,void* buf){$/;" f 117 | file_t common/struct.h /^}file_t;$/;" t typeref:struct:__anon10 118 | file_upload service.c /^int file_upload(send_t * precv){$/;" f 119 | file_write common/common.c /^int file_write(int fd, const void * buf, int len){$/;" f 120 | fileinfo_message_t common/struct.h /^}fileinfo_message_t;$/;" t typeref:struct:__anon6 121 | fileinfo_t common/struct.h /^}fileinfo_t;$/;" t typeref:struct:__anon9 122 | filelen common/struct.h /^ int filelen;$/;" m struct:__anon9 123 | filelen common/struct.h /^ int filelen;$/;" m struct:__anon10 124 | filename common/struct.h /^ char filename[32];$/;" m struct:__anon6 125 | filepath common/struct.h /^ char filepath[32];$/;" m struct:__anon10 126 | filepath common/struct.h /^ char filepath[BUFSIZE];$/;" m struct:__anon6 127 | flag_recv common/struct.h /^ int flag_recv;$/;" m struct:__anon7 128 | friend_info_t common/struct.h /^}friend_info_t;$/;" t typeref:struct:__anon1 129 | friendinfo common/struct.h /^ friend_info_t friendinfo;$/;" m struct:__anon12 130 | friendslist common/struct.h /^ int friendslist[FRIENDSIZE];$/;" m struct:__anon11 131 | from_name common/struct.h /^ char from_name[32];$/;" m struct:__anon5 132 | getch common/common.c /^int getch(void){$/;" f 133 | getpwd common/common.c /^void getpwd(int maxlen, char *pwd){$/;" f 134 | givelen common/struct.h /^ int givelen;$/;" m struct:__anon9 135 | givelen common/struct.h /^ int givelen;$/;" m struct:__anon10 136 | group_info common/struct.h /^ group_info_t group_info;$/;" m struct:__anon12 137 | group_info_t common/struct.h /^}group_info_t;$/;" t typeref:struct:__anon2 138 | groupid common/struct.h /^ int groupid;$/;" m struct:__anon2 139 | grouplist common/struct.h /^ int grouplist[GROUPCOUNT];$/;" m struct:__anon11 140 | groupname common/struct.h /^ char groupname[32];$/;" m struct:__anon2 141 | hour common/struct.h /^ int hour;$/;" m struct:__anon4 142 | input_check common/common.c /^int input_check(int input_len, char * input_buf){$/;" f 143 | input_check common/struct.h /^ int input_check;$/;" m struct:__anon12 144 | key service.h /^ long key;$/;" m struct:__anon14 145 | l_getc common/common.c /^char l_getc(){$/;" f 146 | leave_p_insert chat.c /^int leave_p_insert(send_t *precv){$/;" f 147 | leavemes_flag common/struct.h /^ char leavemes_flag;$/;" m struct:__anon11 148 | leftlen common/struct.h /^ int leftlen;$/;" m struct:__anon9 149 | leftlen common/struct.h /^ int leftlen;$/;" m struct:__anon10 150 | lenth common/struct.h /^ int lenth;$/;" m struct:__anon6 151 | main service.c /^int main(){$/;" f 152 | markname common/struct.h /^ char markname[32];$/;" m struct:__anon9 153 | markname common/struct.h /^ char markname[32];$/;" m struct:__anon10 154 | member common/struct.h /^ friend_info_t member[GROUPSIZE];$/;" m struct:__anon2 155 | message common/struct.h /^ message_t message;$/;" m struct:__anon12 156 | message_buf common/struct.h /^ char message_buf[BUFSIZE-36];$/;" m struct:__anon5 157 | message_p_insert chat.c /^int message_p_insert(message_t *mes){$/;" f 158 | message_p_update account.c /^int message_p_update(message_t * data) {$/;" f 159 | message_t common/struct.h /^}message_t;$/;" t typeref:struct:__anon5 160 | message_tip chat.c /^int message_tip(message_t *mes){$/;" f 161 | messageinfo common/struct.h /^ fileinfo_message_t messageinfo;$/;" m struct:__anon12 162 | minute common/struct.h /^ int minute;$/;" m struct:__anon4 163 | month common/struct.h /^ int month;$/;" m struct:__anon3 164 | my_err common/common.c /^void my_err(const char* err_string, int line){$/;" f 165 | next service.h /^ struct entity_key_node *next;$/;" m struct:entity_key_node typeref:struct:entity_key_node::entity_key_node 166 | offset list.h /^ int offset;$/;" m struct:__anon13 167 | offtime common/struct.h /^ user_logtime_t offtime;$/;" m struct:__anon11 168 | ontime common/struct.h /^ user_logtime_t ontime;$/;" m struct:__anon11 169 | owner common/struct.h /^ char owner[32];$/;" m struct:__anon2 170 | pageSize list.h /^ int pageSize;$/;" m struct:__anon13 171 | prev service.h /^ struct entity_key_node *prev;$/;" m struct:entity_key_node typeref:struct:entity_key_node::entity_key_node 172 | recv_data common/common.c /^int recv_data(int conn_fd, send_t *precv){$/;" f 173 | second common/struct.h /^ int second;$/;" m struct:__anon4 174 | send_data common/common.c /^int send_data(int conn_fd, send_t * psend){$/;" f 175 | send_name common/struct.h /^ char send_name[32];$/;" m struct:__anon5 176 | send_t common/struct.h /^}send_t;$/;" t typeref:struct:__anon12 177 | sendlen common/struct.h /^ int sendlen;$/;" m struct:__anon9 178 | sendlen common/struct.h /^ int sendlen;$/;" m struct:__anon10 179 | sendtimes common/struct.h /^ int sendtimes;$/;" m struct:__anon10 180 | ser_atoi common/common.c /^int ser_atoi(char * buf){ \/\/atoi$/;" f 181 | serve_user_delet account.c /^int serve_user_delet(send_t * precv){$/;" f 182 | serve_user_login account.c /^int serve_user_login(send_t *precv){$/;" f 183 | serve_user_logoff account.c /^int serve_user_logoff(send_t *precv){$/;" f 184 | serve_user_register account.c /^int serve_user_register(send_t *precv){$/;" f 185 | serve_user_update account.c /^int serve_user_update(send_t *precv){$/;" f 186 | server_chat_one chat.c /^int server_chat_one(send_t *precv, int type){$/;" f 187 | server_friend_add chat.c /^int server_friend_add(send_t *precv){$/;" f 188 | server_friend_combain chat.c /^int server_friend_combain(send_t *precv){$/;" f 189 | server_friend_delet chat.c /^int server_friend_delet(send_t * precv){$/;" f 190 | server_friend_find chat.c /^int server_friend_find(send_t *precv){$/;" f 191 | server_friend_send chat.c /^int server_friend_send(send_t *precv){$/;" f 192 | server_friend_show chat.c /^int server_friend_show(send_t *precv){$/;" f 193 | server_name_check account.c /^int server_name_check(char *username){ \/\/重名检测$/;" f 194 | service service.c /^int *service(send_t *precv){$/;" f 195 | size common/struct.h /^ int size;$/;" m struct:__anon2 196 | status common/struct.h /^ char status;$/;" m struct:__anon1 197 | status common/struct.h /^ char status;$/;" m struct:__anon11 198 | thread_t common/struct.h /^}thread_t;$/;" t typeref:struct:__anon7 199 | time common/struct.h /^ user_time_t time;$/;" m struct:__anon12 200 | time common/struct.h /^ user_time_t time;$/;" m struct:__anon5 201 | time common/struct.h /^ user_time_t time;$/;" m struct:__anon8 202 | totalRecords list.h /^ int totalRecords;$/;" m struct:__anon13 203 | user_date_t common/struct.h /^}user_date_t;$/;" t typeref:struct:__anon3 204 | user_logtime_t common/struct.h /^}user_logtime_t;$/;" t typeref:struct:__anon8 205 | user_time_t common/struct.h /^}user_time_t;$/;" t typeref:struct:__anon4 206 | userid common/struct.h /^ int userid;$/;" m struct:__anon11 207 | userinfo common/struct.h /^ userinfo_service_t userinfo;$/;" m struct:__anon12 208 | userinfo_p_checkfile account.c /^int userinfo_p_checkfile(const char *string){$/;" f 209 | userinfo_p_delet account.c /^int userinfo_p_delet(int id){$/;" f 210 | userinfo_p_deletid account.c /^int userinfo_p_deletid(int id) {$/;" f 211 | userinfo_p_insert account.c /^int userinfo_p_insert(const userinfo_service_t * data){$/;" f 212 | userinfo_p_selectall account.c /^int userinfo_p_selectall(userinfo_service_t * users){$/;" f 213 | userinfo_p_selet account.c /^int userinfo_p_selet(int id, userinfo_service_t * data){$/;" f 214 | userinfo_p_seletname account.c /^int userinfo_p_seletname(const char* username, userinfo_service_t * data){$/;" f 215 | userinfo_p_update account.c /^int userinfo_p_update(userinfo_service_t * data) {$/;" f 216 | userinfo_service_t common/struct.h /^}userinfo_service_t;$/;" t typeref:struct:__anon11 217 | username common/struct.h /^ char username[32];$/;" m struct:__anon1 218 | username common/struct.h /^ char username[32];$/;" m struct:__anon11 219 | userpasswd common/struct.h /^ char userpasswd[32];$/;" m struct:__anon11 220 | usersex common/struct.h /^ char usersex[10];$/;" m struct:__anon1 221 | usersex common/struct.h /^ char usersex[10];$/;" m struct:__anon11 222 | userstatus common/struct.h /^ char userstatus;$/;" m struct:__anon12 223 | year common/struct.h /^ int year;$/;" m struct:__anon3 224 | --------------------------------------------------------------------------------