/* -*- coding: utf-8 -*- @Time : 2021/9/21 14:25 -*- @Author : lingz -*- @Software: CLion */ #include #include #include #include #include #include #include #include #include #include #include #include #define PORT 1115 #define BACKLOG 1 #define Max 10 #define MAXSIZE 1024 struct user { char name[20]; int socket_fd; }; struct message { char from_name[20]; char to_name[20]; char mess[1024]; char time_now[25]; int flag; }; struct message mess_buf; struct user user_array[Max]; int user_num=0; char inf[1024]; char private[1024]="Private:"; char sign[2]=":"; char sendbuf[1024]; int SendToClient(int fd,int Size); // Process timestamps. char *get_time(){ time_t time_now; time(&time_now); return(ctime(&time_now)); } void *pthread_service(void* sfd){ int fd=*(int *)sfd; while(1){ char temp_name[30]; char temp_sign[2]; strcpy(temp_sign,sign); int numbytes; int i; numbytes=recv(fd,&mess_buf,sizeof(struct message),0); if(numbytes>0){ if(mess_buf.flag==0){ // If you do not send a message, the server displays all current users. for(i=0;i=Max){ printf("Warning:Reach the maximum of client number.\n"); close(fd); } for(int i=0;i