算是课后作业吧…

功能:聊天室服务器,可用telnet直接连接到服务器,发送消息则在聊天室内的所有用户都能看见。使用select实现

功能:聊天室服务器

可用telnet直接连接到服务器,发送消息则在聊天室内的所有用户都能看见。

使用select实现

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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include <sys/select.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

#define max(a, b) ((a) > (b) ? (a) : (b))

struct filedescriptor_node
{
int fd;
struct filedescriptor_node* next;
};

struct filedescriptor_set
{
size_t len;
struct filedescriptor_node* head;
struct filedescriptor_node* iter;
int max_fd;

fd_set fds;
};

int fds_init(struct filedescriptor_set* _this)
{
bzero(_this, sizeof(struct filedescriptor_set));

FD_ZERO(&_this->fds);
return 0;
}

struct filedescriptor_set*
fds_create()
{
struct filedescriptor_set* _this;
_this = malloc(sizeof(struct filedescriptor_set));
if (_this == NULL)
return NULL;

if (fds_init(_this) == -1)
{
free(_this);
return NULL;
}
return _this;
}

int fds_add(struct filedescriptor_set* _this, int fd)
{
struct filedescriptor_node* p;
p = malloc(sizeof(struct filedescriptor_node));
if (p == NULL)
return -1;
p->fd = fd;
p->next = _this->head;
_this->head = p; /* 在head前插入新节点 */
++_this->len;

if (fd > _this->max_fd)
_this->max_fd = fd;

FD_SET(fd, &_this->fds);

return 0;
}

int fds_remove(struct filedescriptor_set* _this, int fd)
{
struct filedescriptor_node* p;
if (_this->head->fd == fd)
{
p = _this->head;
_this->head = p->next;
free(p);
}
else
{
for (p = _this->head; p->next != NULL; p = p->next)
if (p->next->fd == fd)
break; /* 令p指向fd的前一个 */
if (p->next == NULL)
return -1;
}
--_this->len;

if (fd == _this->max_fd)
{
_this->max_fd = 0; /* _this->max_fd = _this->head->fd不正确,
this->head可能为NULL */
for (p = _this->head; p != NULL; p = p->next)
if (p->fd > _this->max_fd)
_this->max_fd = p->fd;
}

FD_CLR(fd, &_this->fds);

return 0;
}

fd_set fds_getfdset(struct filedescriptor_set* _this)
{
return _this->fds;
}

int fds_getmaxfd(struct filedescriptor_set* _this)
{
return _this->max_fd;
}

void fds_iter_init(struct filedescriptor_set* _this)
{
_this->iter = _this->head;
}

int fds_iter_next(struct filedescriptor_set* _this)
{
if (_this->iter != NULL)
{
int fd = _this->iter->fd;
_this->iter = _this->iter->next;
return fd;
}
else
return -1;
}

void fds_destroy(struct filedescriptor_set* _this)
{
struct filedescriptor_node *p, *q;
p = _this->head;
q = p->next;
while (p != NULL)
{
free(p);
p = q;
if (q != NULL)
q = q->next;
}
}

void fds_delete(struct filedescriptor_set* _this)
{
fds_destroy(_this);
free(_this);
}

int writen(int fd, char* buf, size_t len)
{
ssize_t n;
size_t left = len;

while (left > 0)
{
if ((n = write(fd, buf, len)) <= 0)
{
if (errno == EINTR)
continue;
else
return -1;
}
left -= n;
buf += n;
}
return len; /* 无论如何也要写完,否则就是错误 */
}

int readn(int fd, char* buf, size_t len)
{
ssize_t n;
size_t left = len;

while (left > 0)
{
if ((n = read(fd, buf, len)) < 0)
{
if (errno == EINTR)
continue;
else
return -1;
}
else if (n == 0) /* 到达EOF */
{
break;
}
left -= n;
buf += n;
}
return len - left; /* 可能提前到达EOF */
}

#define PORT 10086
#define BUF_SIZE 4096

typedef struct sockaddr SA;
int main(int argc, char* argv[])
{
struct filedescriptor_set fds;
int svrsock;
struct sockaddr_in svraddr;

svrsock = socket(AF_INET, SOCK_STREAM, 0);
if (svrsock == -1)
{
perror("socket");
exit(errno);
}
bzero(&svraddr, sizeof(svraddr));
svraddr.sin_family = AF_INET;
svraddr.sin_port = htons(PORT);
svraddr.sin_addr.s_addr = htonl(INADDR_ANY);

if (bind(svrsock, (SA*)&svraddr, sizeof(svraddr)) == -1)
{
perror("bind");
exit(errno);
}
if (listen(svrsock, 64) == -1)
{
perror("listen");
exit(errno);
}

if (fds_init(&fds) == -1)
{
perror("fds_init");
exit(errno);
}

while (1)
{
int fd;
fd_set fdset;
char buf[BUF_SIZE];
size_t buflen;

fdset = fds_getfdset(&fds);
FD_SET(svrsock, &fdset);
if (select(max(fds_getmaxfd(&fds), svrsock) + 1, &fdset, NULL, NULL, NULL) == -1)
{
perror("select");
exit(errno);
}
if (FD_ISSET(svrsock, &fdset))
{
int cltsock;
struct sockaddr_in cltaddr;
socklen_t cltaddrlen = sizeof(cltaddr);
char cltaddrstr[INET_ADDRSTRLEN];

if ((cltsock = accept(svrsock, (SA*)&cltaddr, &cltaddrlen)) == -1)
{
perror("accept");
exit(errno);
}
if (inet_ntop(AF_INET, &cltaddr.sin_addr, cltaddrstr, INET_ADDRSTRLEN) == NULL)
{
perror("inet_ntop");
exit(errno);
}
/* 通知新用户到达 */
printf("incoming connection from %s:%d\n", cltaddrstr, ntohs(cltaddr.sin_port));
snprintf(buf, BUF_SIZE, "user %s:%d joined\n", cltaddrstr, ntohs(cltaddr.sin_port));
buflen = strlen(buf);
for (fds_iter_init(&fds); (fd = fds_iter_next(&fds)) >= 0;)
{
if (writen(fd, buf, buflen) == -1)
{
perror("writen");
exit(errno);
}
}
strncpy(buf, "Welcom\n", BUF_SIZE);
buflen = strlen(buf);
if (writen(cltsock, buf, buflen) == -1)
{
perror("writen");
exit(errno);
}

if (fds_add(&fds, cltsock) == -1)
{
perror("fds_add");
exit(errno);
}

}
for (fds_iter_init(&fds); (fd = fds_iter_next(&fds)) >= 0;)
{
if (FD_ISSET(fd, &fdset))
{
int n;
struct filedescriptor_node* old;
if ((n = readn(fd, buf, BUF_SIZE)) == -1)
{
perror("readn");
exit(errno);
}
else if (n == 0)
{
/*struct sockaddr_in cltaddr;
socklen_t cltaddrlen = sizeof(cltaddr);
char cltaddrstr[INET_ADDRSTRLEN];
if (getpeername(fd, (SA*)&cltaddr, &cltaddrlen) == -1)
{
perror("getpeername");
exit(errno);
}
if (inet_ntop(AF_INET, &cltaddr.sin_addr, cltaddrstr, INET_ADDRSTRLEN) == NULL)
{
perror("inet_ntop");
exit(errno);
}

printf("%s disconnected the connection\n", cltaddrstr);
snprintf(buf, BUF_SIZE, "user %s exited\n", cltaddrstr);
buflen = strlen(buf);
for (fds_iter_init(&fds); (fd = fds_iter_next(&fds)) >= 0;)
{
if (writen(fd, buf, buflen) == -1)
{
perror("writen");
exit(errno);
}
}*/

fds_remove(&fds, fd);
continue;
}
old = fds.iter;
for (fds_iter_init(&fds); (fd = fds_iter_next(&fds)) >= 0;)
{
if (writen(fd, buf, n) == -1)
{
perror("writen");
exit(errno);
}
}
fds.iter = old;
}
}

}

fds_destroy(&fds);
return 0;
}

Proudly powered by Hexo and Theme by Hacker
© 2021 wastecat