forked from cozis/tinyio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample2.c
More file actions
58 lines (45 loc) · 1.26 KB
/
example2.c
File metadata and controls
58 lines (45 loc) · 1.26 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
#include <stdio.h>
#include <assert.h>
#include "io.h"
#define MAX_RES 100
#define MAX_OPS 100
int main(void)
{
int port = 8080;
fprintf(stderr, "port=%d\n", port);
struct io_operation ops[MAX_OPS];
struct io_resource res[MAX_RES];
struct io_context ioc;
if (!io_global_init()) {
fprintf(stderr, "Couldn't perform the global initialization\n");
return -1;
}
if (!io_init(&ioc, res, ops, MAX_RES, MAX_OPS)) {
fprintf(stderr, "Couldn't initialize I/O context\n");
return -1;
}
io_handle socket = io_start_server(&ioc, NULL, port);
if (socket == IO_INVALID) {
fprintf(stderr, "Couldn't start listening\n");
return -1;
}
if (!io_accept(&ioc, NULL, socket)) {
fprintf(stderr, "Couldn't start accept operation\n");
return -1;
}
for (;;) {
struct io_event ev;
io_wait(&ioc, &ev);
assert(ev.optype == IO_ACCEPT);
io_handle accepted = ev.accepted;
fprintf(stderr, "Accepted\n");
io_close(&ioc, accepted);
if (!io_accept(&ioc, NULL, socket)) {
fprintf(stderr, "Couldn't start accept operation\n");
return -1;
}
}
io_free(&ioc);
io_global_free();
return 0;
}