-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini_serv.c
224 lines (194 loc) · 3.51 KB
/
mini_serv.c
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
typedef struct s_client
{
int fd;
int id;
struct s_client *next;
} t_client;
t_client *clients = NULL;
fd_set write_set, read_set, current;
int sockfd, g_id;
char msg[200000], buf[200040];
void eprint(char *msg)
{
write(2, msg, strlen(msg));
}
void fatal()
{
eprint("Fatal error\n");
close(sockfd);
exit(1);
}
int get_max_fd()
{
int maxfd = sockfd;
t_client *tmp = clients;
while (tmp)
{
if (tmp->fd > maxfd)
maxfd = tmp->fd;
tmp = tmp->next;
}
return (maxfd);
}
int get_id(int fd)
{
t_client *tmp = clients;
while (tmp)
{
if (tmp->fd == fd)
return (tmp->id);
tmp = tmp->next;
}
return (-10);
}
void send_to_all(int from)
{
t_client *tmp = clients;
while (tmp)
{
int to = tmp->fd;
if (from != to && FD_ISSET(to, &write_set))
{
if (send(to, buf, strlen(buf), 0) == -1)
fatal();
}
tmp = tmp->next;
}
}
void add_client()
{
t_client *tmp = clients;
t_client *new;
int client;
struct sockaddr_in clientaddr;
socklen_t len = sizeof(clientaddr);
client = accept(sockfd, (struct sockaddr *)&clientaddr, &len);
if (client == -1)
fatal();
FD_ZERO(&buf);
sprintf(buf, "server: client %d just arrived\n", g_id);
send_to_all(client);
FD_SET(client, ¤t);
new = malloc(sizeof(t_client));
if (new == NULL)
fatal();
new->fd = client;
new->id = g_id++;
new->next = NULL;
if (clients == NULL)
clients = new;
else
{
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}
}
void rm_client(int fd)
{
t_client *to_del = NULL;
t_client *tmp = clients;
FD_ZERO(&buf);
sprintf(buf, "server: client %d just left\n", get_id(fd));
send_to_all(fd);
FD_CLR(fd, ¤t);
if (clients && clients->fd == fd)
{
to_del = clients;
clients = clients->next;
}
else
{
while (tmp && tmp->next && tmp->next->fd != fd)
tmp = tmp->next;
if (tmp && tmp->next && tmp->next->fd == fd)
{
to_del = tmp->next;
tmp->next = tmp->next->next;
}
}
free(to_del);
close(fd);
}
void extract_msg(int fd)
{
char tmp[200000];
int i = -1;
int j = -1;
FD_ZERO(&tmp);
while (msg[++i])
{
tmp[++j] = msg[i];
if (msg[i] == '\n')
{
FD_ZERO(&buf);
sprintf(buf, "client %d: %s", get_id(fd), tmp);
send_to_all(fd);
FD_ZERO(&tmp);
j = -1;
}
}
FD_ZERO(&msg);
}
int main(int argc, char **argv)
{
if (argc != 2)
{
eprint("Wrong number of arguments\n");
exit(1);
}
struct sockaddr_in servaddr;
FD_ZERO(&servaddr);
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(2130706433);
servaddr.sin_port = htons(atoi(argv[1]));
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1)
fatal();
if (bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
fatal();
if (listen(sockfd, 100) == -1)
fatal();
FD_ZERO(¤t);
FD_SET(sockfd, ¤t);
FD_ZERO(&msg);
while (1)
{
read_set = write_set = current;
if (select(get_max_fd() + 1, &read_set, &write_set, NULL, NULL) == -1)
continue ;
for (int fd = 0; fd < get_max_fd() + 1; ++fd)
{
if (FD_ISSET(fd, &read_set))
{
if (fd == sockfd)
{
add_client();
break ;
}
int ret = 1;
while (ret == 1 && msg[strlen(msg) - 1] != '\n')
{
ret = recv(fd, msg + strlen(msg), 1, 0);
if (ret <= 0)
break ;
}
if (ret <= 0)
{
rm_client(fd);
break ;
}
extract_msg(fd);
}
}
}
return (0);
}