Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
Network System Examples

Focused, minimal examples demonstrating common network_system usage patterns. Each example is self-contained and can be built and run independently.

For more comprehensive demonstrations (HTTP, gRPC, QUIC, memory profiling), see the samples/ directory.

Building

Examples are built when BUILD_EXAMPLES=ON is set (off by default):

cmake -B build -DBUILD_EXAMPLES=ON
cmake --build build

Binaries are placed in build/bin/examples/.

Examples

tcp_echo_server

A minimal TCP server that echoes received data back to the client. Shows server lifecycle, session tracking, and callback-based event handling.

./build/bin/examples/example_tcp_echo_server

tcp_client

A TCP client that connects to a server, sends text and binary messages, and prints responses. Pair with tcp_echo_server.

# Terminal 1
./build/bin/examples/example_tcp_echo_server
# Terminal 2
./build/bin/examples/example_tcp_client

websocket_chat

A self-contained WebSocket chat demo. Starts a server and two clients in separate threads, demonstrating broadcast messaging over WebSocket.

Requires BUILD_WEBSOCKET_SUPPORT=ON (enabled by default).

./build/bin/examples/example_websocket_chat

connection_pool

Demonstrates TCP connection pooling with the tcp_facade. Covers pool initialization, single-threaded acquire/release, and multi-threaded concurrent access with throughput measurement.

./build/bin/examples/example_connection_pool

udp_echo

A UDP echo server and client running in a single program. Shows datagram-based communication with message boundary preservation.

./build/bin/examples/example_udp_echo

observer_pattern

Demonstrates the connection_observer interface, null_connection_observer, and callback_adapter for handling client events. Compares the observer pattern with callback-based approaches.

./build/bin/examples/example_observer_pattern

Key Concepts

Concept Examples
TCP server/client tcp_echo_server, tcp_client
UDP communication udp_echo
WebSocket websocket_chat
Connection pooling connection_pool
Observer pattern observer_pattern
Result<T> error handling All examples
Session management tcp_echo_server, websocket_chat
Facade API All examples

API Quick Reference

All examples use the facade API (tcp_facade, udp_facade, websocket_facade) which provides a simplified interface for creating clients and servers. The facade returns i_protocol_client and i_protocol_server interfaces that offer a consistent API across all protocols.

Common Pattern

using namespace kcenon::network;
// Server
auto server = tcp.create_server({ .port = 9000 });
server->set_receive_callback([](std::string_view session_id, const std::vector<uint8_t>& data) {
// handle data
});
server->start(9000);
// Client
auto client = tcp.create_client({ .host = "127.0.0.1", .port = 9000 });
client->set_receive_callback([](const std::vector<uint8_t>& data) {
// handle data
});
client->start("127.0.0.1", 9000);
client->send(std::vector<uint8_t>{'h', 'i'});
Simplified facade for creating TCP clients and servers.
Definition tcp_facade.h:95
@ server
Server-side handling of a request.
Main namespace for all Network System components.
Simplified facade for creating TCP clients and servers.