Server example
A simple server which listens on port 8000 and echoes whatever is sent to it:
#include <stdlib.h>
#include "dyad.h"
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");
}
int main(void) {
dyad_init();
dyad_Stream *serv = dyad_newStream();
dyad_addListener(serv, DYAD_EVENT_ACCEPT, onAccept, NULL);
dyad_listen(serv, 8000);
while (dyad_getStreamCount() > 0) {
dyad_update();
}
dyad_shutdown();
return 0;
}
Client example
A simple example program which connects to a daytime server and prints the response:
#include <stdio.h>
#include "dyad.h"
static void onConnect(dyad_Event *e) {
printf("connected: %s\n", e->msg);
}
static void onData(dyad_Event *e) {
printf("%s", e->data);
}
int main(void) {
dyad_init();
dyad_Stream *s = dyad_newStream();
dyad_addListener(s, DYAD_EVENT_CONNECT, onConnect, NULL);
dyad_addListener(s, DYAD_EVENT_DATA, onData, NULL);
dyad_connect(s, "time-nw.nist.gov", 13);
while (dyad_getStreamCount() > 0) {
dyad_update();
}
dyad_shutdown();
return 0;
}