fnet.c

Simple C networking library
git clone git://git.finwo.net/lib/fnet.c
Log | Files | Refs | README

commit c994f1bc0c421463ef08400fc93792a0729ee597
parent f755dcac74463f5a776b5f15d16729a97d9ef54f
Author: Yersa Nordman <yersa@finwo.nl>
Date:   Wed,  2 Aug 2023 01:01:46 +0200

Added client mode to test program

Diffstat:
Mtest.c | 32++++++++++++++++++++++++++------
1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/test.c b/test.c @@ -12,8 +12,11 @@ void onClose(struct fnet_ev *ev) { void onData(struct fnet_ev *ev) { printf("Data(%d): %.*s\n", ev->buffer->len, (int)(ev->buffer->len), ev->buffer->data); - // Simple echo - fnet_write(ev->connection, ev->buffer); + // Simple echo if it was an accepted connection + if (ev->connection->status & FNET_STATUS_ACCEPTED) { + fnet_write(ev->connection, ev->buffer); + } + } void onConnect(struct fnet_ev *ev) { @@ -24,6 +27,10 @@ void onConnect(struct fnet_ev *ev) { void onTick(struct fnet_ev *ev) { printf("tick\n"); + fnet_write(ev->connection, &((struct buf){ + .len = 12, + .data = "Hello World\n", + })); } int main(int argc, const char *argv[]) { @@ -88,16 +95,29 @@ int main(int argc, const char *argv[]) { if (mode == 1) { - const struct fnet_options_t opts = { + fnet_listen(addr, port, &((struct fnet_options_t){ .proto = FNET_PROTO_TCP, .flags = 0, .onConnect = onConnect, .onData = NULL, - .onTick = onTick, + .onTick = NULL, .onClose = NULL, .udata = NULL, - }; - fnet_listen(addr, port, &opts); + })); + fnet_main(); + return 0; + } + + if (mode == 2) { + fnet_connect(addr, port, &((struct fnet_options_t){ + .proto = FNET_PROTO_TCP, + .flags = 0, + .onConnect = onConnect, + .onData = onData, + .onTick = onTick, + .onClose = onClose, + .udata = NULL, + })); fnet_main(); return 0; }