1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <strings.h> #include <fcntl.h> #include <sys/socket.h> #include <sys/file.h> #include <netinet/in.h> #include <arpa/inet.h> #include <unistd.h> #include <time.h> #include <pthread.h>
#define MAXLINE 511 #define MAX_SOCK 1024
char *EXIT_STRING = "exit"; char *START_STRING = "Connected to chat_server \n";
int maxfdp1; int num_user = 0; int num_chat = 0; int clisock_list[MAX_SOCK]; char ip_list[MAX_SOCK][20]; int listen_sock;
void addClient(int s, struct sockaddr_in *newcliaddr); int getmax(); void removeClient(int s); int tcp_listen(int host, int port, int backlog); void errquit(char *mesg) { perror(mesg); exit(1); }
time_t ct; struct tm tm;
void *thread_function(void *arg) { int i; printf("명령어 목록 : help, num_user, num_chat, ip_list\n"); while (1) { char bufmsg[MAXLINE + 1]; fprintf(stderr, "\033[1;32m"); printf("server>"); fgets(bufmsg, MAXLINE, stdin); if (!strcmp(bufmsg, "\n")) continue; else if (!strcmp(bufmsg, "help\n")) printf("help, num_user, num_chat, ip_list\n"); else if (!strcmp(bufmsg, "num_user\n")) printf("현재 참가자 수 = %d\n", num_user); else if (!strcmp(bufmsg, "num_chat\n")) printf("지금까지 오간 대화의 수 = %d\n", num_chat); else if (!strcmp(bufmsg, "ip_list\n")) for (i = 0; i < num_user; i++) printf("%s\n", ip_list[i]); else printf("해당 명령어가 없습니다.help를 참조하세요.\n"); } }
int main(int argc, char *argv[]) { struct sockaddr_in cliaddr; char buf[MAXLINE + 1]; int i, j, nbyte, accp_sock, addrlen = sizeof(struct sockaddr_in); fd_set read_fds; pthread_t a_thread;
if (argc != 2) { printf("사용법 :%s port\n", argv[0]); exit(0); }
listen_sock = tcp_listen(INADDR_ANY, atoi(argv[1]), 5); pthread_create(&a_thread, NULL, thread_function, (void *)NULL); while (1) { FD_ZERO(&read_fds); FD_SET(listen_sock, &read_fds); for (i = 0; i < num_user; i++) FD_SET(clisock_list[i], &read_fds);
maxfdp1 = getmax() + 1; if (select(maxfdp1, &read_fds, NULL, NULL, NULL) < 0) errquit("select fail");
if (FD_ISSET(listen_sock, &read_fds)) { accp_sock = accept(listen_sock, (struct sockaddr*)&cliaddr, &addrlen); if (accp_sock == -1) errquit("accept fail"); addClient(accp_sock, &cliaddr); send(accp_sock, START_STRING, strlen(START_STRING), 0); ct = time(NULL); tm = *localtime(&ct); write(1, "\033[0G", 4); printf("[%02d:%02d:%02d]", tm.tm_hour, tm.tm_min, tm.tm_sec); fprintf(stderr, "\033[33m"); printf("사용자 1명 추가. 현재 참가자 수 = %d\n", num_user); fprintf(stderr, "\033[32m"); fprintf(stderr, "server>"); }
for (i = 0; i < num_user; i++) { if (FD_ISSET(clisock_list[i], &read_fds)) { num_chat++; nbyte = recv(clisock_list[i], buf, MAXLINE, 0); if (nbyte <= 0) { removeClient(i); continue; } buf[nbyte] = 0; if (strstr(buf, EXIT_STRING) != NULL) { removeClient(i); continue; } for (j = 0; j < num_user; j++) send(clisock_list[j], buf, nbyte, 0); printf("\033[0G"); fprintf(stderr, "\033[97m"); printf("%s", buf); fprintf(stderr, "\033[32m"); fprintf(stderr, "server>"); } }
}
return 0; }
void addClient(int s, struct sockaddr_in *newcliaddr) { char buf[20]; inet_ntop(AF_INET, &newcliaddr->sin_addr, buf, sizeof(buf)); write(1, "\033[0G", 4); fprintf(stderr, "\033[33m"); printf("new client: %s\n", buf); clisock_list[num_user] = s; strcpy(ip_list[num_user], buf); num_user++; }
void removeClient(int s) { close(clisock_list[s]); if (s != num_user - 1) { clisock_list[s] = clisock_list[num_user - 1]; strcpy(ip_list[s], ip_list[num_user - 1]); } num_user--; ct = time(NULL); tm = *localtime(&ct); write(1, "\033[0G", 4); fprintf(stderr, "\033[33m"); printf("[%02d:%02d:%02d]", tm.tm_hour, tm.tm_min, tm.tm_sec); printf("채팅 참가자 1명 탈퇴. 현재 참가자 수 = %d\n", num_user); fprintf(stderr, "\033[32m"); fprintf(stderr, "server>"); }
int getmax() { int max = listen_sock; int i; for (i = 0; i < num_user; i++) if (clisock_list[i] > max) max = clisock_list[i]; return max; }
int tcp_listen(int host, int port, int backlog) { int sd; struct sockaddr_in servaddr;
sd = socket(AF_INET, SOCK_STREAM, 0); if (sd == -1) { perror("socket fail"); exit(1); } bzero((char *)&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(host); servaddr.sin_port = htons(port); if (bind(sd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { perror("bind fail"); exit(1); } listen(sd, backlog); return sd; }
|