kronknet
A low level network library in C.
Overview
KronkNet is a high-performance asynchronous TCP network library in C. It is built around the poll() system call and an event-driven architecture: a single thread manages many connections. An internal ring buffer keeps the OS from blocking during large transfers.
The server never freezes the game loop. Both TCP and UDP are supported, with IPv4 and IPv6 on a single socket. It builds on Linux and Windows (MSVC or MinGW).
Changelog
1.2.0
Performance
- poll() is replaced by epoll(), which handles a very large number of clients much better.
Project
- TCP timeout fix.
- The mkn/ folder, an old modular version that had become useless, is removed.
- Tests build from the root (-DBUILD_TESTS=ON) with kronklab, and the CI runs them.
- PR and issue templates.
Examples
#include <kronknet/server/server.h>
#include <kronknet/connection/connection.h>
#include <kronknet/macros/errdef.h>
#include <stdio.h>
static int on_connect(knServer *server, knConnection *conn)
{
(void)server;
printf("+ %s:%hu\n", knConnection_getIp(conn), knConnection_getPort(conn));
return KNEVTOK;
}
static int on_read(knConnection *conn, const void *data, size_t size)
{
return knConnection_send(conn, data, size); // echo it back
}
static int on_disconnect(knServer *server, knConnection *conn)
{
(void)server;
printf("- %s:%hu\n", knConnection_getIp(conn), knConnection_getPort(conn));
return KNEVTOK;
}
int main(void)
{
knServer *server = knServer_create(4242, knTCP);
if (!server)
return 1;
knServer_setOnConnect(server, on_connect);
knServer_setOnRead(server, on_read);
knServer_setOnDisconnect(server, on_disconnect);
knServer_run(server); // blocks until knServer_stop()
knServer_destroy(server);
return 0;
}