forked from kwongiho/SocketProgrammingClass
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_server.c
More file actions
136 lines (118 loc) · 4.33 KB
/
Copy pathchat_server.c
File metadata and controls
136 lines (118 loc) · 4.33 KB
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
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/file.h>
#include <netinet/in.h>
#define MAXLINE 1024
#define MAX_SOCK 512
char *escapechar = "exit\n";
int readline(int, char *, int);
int main(int argc, char *argv[]) {
char rline[MAXLINE], my_msg[MAXLINE];
char *start = "대화방에 오신걸 환영합니다...\n";
int i, j, n;
int s, client_fd, clilen;
int nfds; /* 최대 소켓번호 +1 */
fd_set read_fds; /* 읽기를 감지할 소켓번호 구조체 */
int num_chat = 0; /* 채팅 참가자 수 */
/* 채팅에 참가하는 클라이언트들의 소켓번호 리스트 */
int client_s[MAX_SOCK];
struct sockaddr_in client_addr, server_addr;
if(argc < 2) {
printf("실행방법 :%s [포트번호]\n",argv[0]);
return -1;
}
printf("대화방 서버 초기화 중....\n");
/* 초기소켓 생성 */
if((s = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
printf("Server: Can't open stream socket.");
return -1;
}
/* server_addr 구조체의 내용 세팅 */
bzero((char *)&server_addr, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(atoi(argv[1]));
if (bind(s,(struct sockaddr *)&server_addr,sizeof(server_addr)) < 0) {
printf("Server: Can't bind local address.(or port number)\n");
return -1;
}
printf("클라이언트로부터 연결요청을 기다림.");
/* 클라이언트로부터 연결요청을 기다림 */
listen(s, 5);
nfds = s + 1; /* 최대 소켓번호 +1 */
FD_ZERO(&read_fds);
while(1) {
/* (최대 소켓번호 +1) 값을 갱신 */
if((num_chat-1) >= 0) {
nfds = client_s[num_chat-1] + 1;
printf("num_chat %d",num_chat);
}
/* 읽기 변화를 감지할 소켓번호를 fd_set 구조체에 지정 */
FD_SET(s, &read_fds);
for(i=0; i<num_chat; i++)
FD_SET(client_s[i], &read_fds);
/*--------------------------------------- select() 호출 ----------------------------------------- */
if (select(nfds, &read_fds, (fd_set *)0, (fd_set *)0,(struct timeval *)0) < 0) {
printf("select error\n");
return -1;
}
/*------------------------------ 클라이언트 연결요청 처리 ------------------------------- */
if(FD_ISSET(s, &read_fds)) {
clilen = sizeof(client_addr);
client_fd = accept(s, (struct sockaddr *)&client_addr, &clilen);
if(client_fd != -1) {
/* 채팅 클라이언트 목록에 추가 */
client_s[num_chat] = client_fd;
num_chat++;
send(client_fd, start, strlen(start), 0);
printf("%d 번째 사용자 추가.\n",num_chat);
}
}
/*------ 임의의 클라이언트가 보낸 메시지를 모든 클라이언트에게 방송 ----- */
for(i = 0; i < num_chat; i++) {
if(FD_ISSET(client_s[i], &read_fds)) {
if((n = recv(client_s[i], rline, MAXLINE,0)) > 0) {
rline[n] = '\0';
/* 종료문자 입력시 채팅 탈퇴 처리 */
if (exitCheck(rline, escapechar, 5) == 1) {
shutdown(client_s[i], 2);
if(i != num_chat-1) client_s[i] = client_s[num_chat-1];
num_chat--;
continue;
}
/* 모든 채팅 참가자에게 메시지 방송 */
for (j = 0; j < num_chat; j++) send(client_s[j], rline, n, 0);
printf("%s", rline);
}
}
}
}
return 0;
}
/* ------------------------------- 종료문자 확인 함수 ----------------------------
exitCheck()는 다음의 세 개의 인자를 필요로 한다
rline: 클라이언트가 전송한 문자열 포인터
escapechar: 종료문자 포인터
len: 종료문자의 크기
---------------------------------------------------------------------------------------------*/
int exitCheck(rline, escapechar, len)
char *rline; /* 클라이언트가 전송한 메시지 */
char *escapechar; /* 종료문자 */
int len;
{
int i, max;
char *tmp;
max = strlen(rline);
tmp = rline;
for(i = 0; i<max; i++) {
if (*tmp == escapechar[0]) {
if(strncmp(tmp, escapechar, len) == 0)
return 1;
} else
tmp++;
}
return -1;
}