-
Notifications
You must be signed in to change notification settings - Fork 0
/
check6a
176 lines (146 loc) · 4.4 KB
/
check6a
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
Script started on 2020년 11월 30일 (월) 오전 11시 34분 39초
[?1034hnetpa15@~/ 997$ cat prog/tcp_chatserv_nonb.c
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/file.h>
#include <errno.h>
#include <strings.h>
#include <fcntl.h>
#include <sys/file.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MAXLINE 511
#define MAX_SOCK 1024
char *EXIT_STRING = "exit";
char *START_STRING = "Connected to chat_server \n";
int maxfdp1;
int num_chat = 0;
int clisock_list[MAX_SOCK];
int listen_sock;
void addClient(int s, struct sockaddr_in *newcliaddr);
void removeClient(int s);
int tcp_listen(int host, int port, int backlog);
void errquit(char *msg) { perror(msg); exit(1); }
int set_nonblock(int sockfd);
int is_nonblock(int sockfd);
int main(int argc, char *argv[]) {
struct sockaddr_in cliaddr;
int i, j, nbyte, count;
int addrlen;
int accp_sock, clilen;
char buf[MAXLINE];
if(argc != 2) {
printf("Usage : %s port\n", argv[0]);
exit(0);
}
listen_sock = tcp_listen(INADDR_ANY, atoi(argv[1]), 5);
if(listen_sock==1)
errquit("tcp_listen fail");
if(set_nonblock(listen_sock)==-1)
errquit("set_nonblock fail");
for(count=0; ; count++){
if (count==1000000){
putchar('.');
fflush(stdout);
count=0;
}
addrlen=sizeof(cliaddr);
accp_sock=accept(listen_sock, (struct sockaddr*)&cliaddr, &clilen);
if(accp_sock==-1 && errno!=EWOULDBLOCK)
errquit("accept fail");
else if (accp_sock>0){
clisock_list[num_chat]=accp_sock;
if(is_nonblock(accp_sock)!=0 &&set_nonblock(accp_sock)<0)
errquit("set_nonblock fail");
addClient(accp_sock,&cliaddr);
send(accp_sock,START_STRING,strlen(START_STRING),0);
printf("%d번째 사용자 추가.\n",num_chat);
}
for(i=0; i<num_chat; i++){
errno=0;
nbyte=recv(clisock_list[i],buf,MAXLINE,0);
if(nbyte==0){
removeClient(i);
continue;
}
else if (nbyte==-1&&errno==EWOULDBLOCK)
continue;
if(strstr(buf,EXIT_STRING)!=NULL){
removeClient(i);
continue;
}
buf[nbyte]=0;
for (j=0; j<num_chat; j++)
send(clisock_list[j],buf,nbyte,0);
printf("%s\n",buf);
}
}
}
void addClient(int s, struct sockaddr_in *newcliaddr) {
char buf[20];
inet_ntop(AF_INET, &newcliaddr->sin_addr, buf, sizeof(buf));
printf("new client : %s\n", buf);
clisock_list[num_chat] = s;
num_chat++;
}
void removeClient(int s) {
close(clisock_list[s]);
if(s != num_chat-1)
clisock_list[s] = clisock_list[num_chat-1];
num_chat--;
printf("참가자 1명 탈퇴. 현재 참가자 수 = %d\n", num_chat);
}
int is_nonblock(int sockfd){
int val;
val=fcntl(sockfd,F_GETFL,0);
if(val&O_NONBLOCK)
return 0;
return -1;
}
int set_nonblock(int sockfd){
int val;
val=fcntl(sockfd,F_GETFL,0);
if(fcntl(sockfd,F_SETFL,val | O_NONBLOCK)==-1)
return -1;
return 0;
}
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;
}
netpa15@~/ 998$ prog/tcp_chatserv_nonv[Kb.c[K[K 8015
...........................new client : 0.0.0.0
1번째 사용자 추가.
..............new client : 117.17.142.162
2번째 사용자 추가.
...[b15] : netpa15
...[c15] : Ok receive
....[b15] : Ok confirm
...참가자 1명 탈퇴. 현재 참가자 수 = 1
.^C
netpa15@~/ 999$ ls -l prog/tcp_chatserv_non*
-rwxr-xr-x. 1 netpa15 student 14120 11월 30 11:31 prog/tcp_chatserv_nonb
-rw-r--r--. 1 netpa15 student 3599 11월 30 11:30 prog/tcp_chatserv_nonb.c
netpa15@~/ 1000$ exit
Script done on 2020년 11월 30일 (월) 오전 11시 37분 14초