forked from rxi/dyad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathechoserv.c
More file actions
35 lines (27 loc) · 758 Bytes
/
echoserv.c
File metadata and controls
35 lines (27 loc) · 758 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dyad.h"
/* An echo server: Echos any data received by a client back to the client */
static void onData(dyad_Event *e) {
dyad_write(e->stream, e->data, e->size);
}
static void onAccept(dyad_Event *e) {
dyad_addListener(e->remote, DYAD_EVENT_DATA, onData, NULL);
dyad_writef(e->remote, "echo server\r\n");
}
static void onError(dyad_Event *e) {
printf("server error: %s\n", e->msg);
}
int main(void) {
dyad_Stream *s;
dyad_init();
s = dyad_newStream();
dyad_addListener(s, DYAD_EVENT_ERROR, onError, NULL);
dyad_addListener(s, DYAD_EVENT_ACCEPT, onAccept, NULL);
dyad_listen(s, 8000);
while (dyad_getStreamCount() > 0) {
dyad_update();
}
return 0;
}