-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathecho-server.c
80 lines (60 loc) · 1.4 KB
/
echo-server.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
/*
* echo-server.c
*/
#include <uv.h>
#include <stdio.h>
#include <stdlib.h>
uv_tcp_t server;
uv_loop_t *loop;
void
alloc_buffer(uv_handle_t *handle, size_t size, uv_buf_t *buf)
{
buf->base = malloc(size);
buf->len = size;
}
void
on_read(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf)
{
uv_write_t *req = (uv_write_t *)malloc(sizeof(uv_write_t));
if (nread == -1) {
/* if (uv_last_error(loop).code != UV_EOF) { */
/* } */
uv_close((uv_handle_t *)stream, NULL);
}
int r = uv_write(req, stream, buf, 1, NULL);
if (r) {
/* error */
}
free(buf->base);
}
void
on_connection(uv_stream_t *server, int status)
{
uv_tcp_t *client = malloc(sizeof(uv_tcp_t));
if (status == -1) {
/* error */
}
uv_tcp_init(loop, client);
if (uv_accept(server, (uv_stream_t *)client) == 0) {
int r = uv_read_start((uv_stream_t *)client, alloc_buffer, on_read);
if (r) {
/* error */
}
} else {
uv_close((uv_handle_t *)client, NULL);
}
}
int
main(int argc, char **argv)
{
loop = uv_default_loop();
struct sockaddr_in addr;
uv_ip4_addr("0.0.0.0", 3000, &addr);
uv_tcp_init(loop, &server);
uv_tcp_bind(&server, (const struct sockaddr*)&addr, 0);
int r = uv_listen((uv_stream_t *)&server, 128, on_connection);
if (r) {
/* error */
}
return uv_run(loop, UV_RUN_DEFAULT);
}