代码:
#ifndef _i_h #define _i_h #include <math.h> #include <stdio.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include <inttypes.h> #include <time.h> #include <sys/ioctl.h> #include <net/if.h> #include <signal.h> #include <ncurses.h> #include <math.h> #define sevr_ip "127.0.0.1" #define sevr_port 8081 #define cntnt_len 150 #define msg_len sizeof(struct msg) #define addr_len sizeof(struct sockaddr) #define usr_len sizeof(struct user) #define prt_len 8 #define hstr_len sizeof(struct chat_history) /* declare global variables */ int mainfd;/* used as chat histroy file handle*/ int sockfd;/* used as socket local handle */ int count; struct sockaddr_in server; /* msg is used for communicating message */ struct msg { int flag; /* flag meaning:1,ordinary; 2,log msg; 3,reg msg, other,file*/ int id_from; int id_to; char content[cntnt_len]; char append[10]; }; /* user is used information list */ struct user { int id; char name[10]; char password[10]; char *p_chatlog; struct sockaddr user_addr; }; /* chat_history used for reading chat history */ struct chat_history { char content[cntnt_len]; char time[25]; int to; int from; int count; }; /* i_functions below is funtions needed by both client and sever */ extern int i_saveto_chat(struct msg *pmsg); int i_clean_stdin () { while ('n' == getchar()) { continue; } return(0); } int i_print(char *pmsg, int size) { int i = 1; for (i; i<= size; i++) { if (*pmsg != 'n') { printf("%c", *pmsg); pmsg ++; } else { return(0); } } return(0); } int i_input(char *p_input) { char c = '