Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
basic_usage.cpp
Go to the documentation of this file.
1
11#include <iostream>
12#include <memory>
13#include <thread>
14#include <chrono>
15#include <vector>
16
17using namespace kcenon::network;
18
19int main() {
20 std::cout << "=== Network System - Basic Usage Example ===" << std::endl;
21
22 // 1. TCP Server Setup
23 std::cout << "\n1. TCP Server Setup:" << std::endl;
24
25 // Create TCP facade and server using factory method
27 const unsigned short port = 8080;
28
29 auto server = tcp.create_server({
30 .port = port,
31 .server_id = "BasicServer"
32 });
33 std::cout << "Server created with ID: BasicServer" << std::endl;
34
35 // Start server (port already set in config)
36 auto server_result = server->start(port);
37
38 if (server_result.is_err()) {
39 std::cerr << "✗ Failed to start server" << std::endl;
40 return -1;
41 }
42
43 std::cout << "✓ Server started on port " << port << std::endl;
44
45 // Give server time to start
46 std::this_thread::sleep_for(std::chrono::milliseconds(100));
47
48 // 2. TCP Client Setup
49 std::cout << "\n2. TCP Client Setup:" << std::endl;
50
51 // Create TCP client using facade factory method
52 auto client = tcp.create_client({
53 .host = "localhost",
54 .port = port,
55 .client_id = "BasicClient"
56 });
57 std::cout << "Client created with ID: BasicClient" << std::endl;
58
59 // Connect to server
60 auto client_result = client->start("localhost", port);
61
62 if (client_result.is_err()) {
63 std::cerr << "✗ Failed to connect client" << std::endl;
64 server->stop();
65 return -1;
66 }
67
68 std::cout << "✓ Client connected to localhost:" << port << std::endl;
69
70 // Wait for connection to establish
71 std::this_thread::sleep_for(std::chrono::milliseconds(100));
72
73 // 3. Data Transmission
74 std::cout << "\n3. Data Transmission:" << std::endl;
75
76 // Send text message
77 std::string message = "Hello from Network System!";
78 std::vector<uint8_t> data(message.begin(), message.end());
79
80 std::cout << "Sending message: \"" << message << "\"" << std::endl;
81
82 // Use std::move for zero-copy efficiency
83 auto send_result = client->send(std::move(data));
84
85 if (send_result.is_err()) {
86 std::cerr << "✗ Failed to send message" << std::endl;
87 } else {
88 std::cout << "✓ Message sent successfully" << std::endl;
89 }
90
91 // 4. Connection Status
92 std::cout << "\n4. Connection Status:" << std::endl;
93
94 if (client->is_connected()) {
95 std::cout << "✓ Client is connected" << std::endl;
96 } else {
97 std::cout << "✗ Client is disconnected" << std::endl;
98 }
99
100 // 5. Binary Data Transmission
101 std::cout << "\n5. Binary Data Transmission:" << std::endl;
102
103 std::vector<uint8_t> binary_data = {0x01, 0x02, 0x03, 0x04, 0x05, 0xFF, 0xFE, 0xFD};
104 std::cout << "Sending binary data (" << binary_data.size() << " bytes)" << std::endl;
105
106 auto binary_result = client->send(std::move(binary_data));
107
108 if (binary_result.is_err()) {
109 std::cerr << "✗ Failed to send binary data" << std::endl;
110 } else {
111 std::cout << "✓ Binary data sent successfully" << std::endl;
112 }
113
114 // 6. Cleanup
115 std::cout << "\n6. Cleanup:" << std::endl;
116
117 // Stop client
118 auto client_stop_result = client->stop();
119 if (client_stop_result.is_err()) {
120 std::cerr << "✗ Failed to stop client" << std::endl;
121 } else {
122 std::cout << "✓ Client stopped" << std::endl;
123 }
124
125 // Stop server
126 auto server_stop_result = server->stop();
127 if (server_stop_result.is_err()) {
128 std::cerr << "✗ Failed to stop server" << std::endl;
129 } else {
130 std::cout << "✓ Server stopped" << std::endl;
131 }
132
133 // Summary
134 std::cout << "\n=== Summary ===" << std::endl;
135 std::cout << "✓ Server/Client creation" << std::endl;
136 std::cout << "✓ TCP connection establishment" << std::endl;
137 std::cout << "✓ Text and binary data transmission" << std::endl;
138 std::cout << "✓ Result<T> based error handling" << std::endl;
139 std::cout << "✓ Zero-copy data transfer with std::move" << std::endl;
140 std::cout << "✓ Clean shutdown" << std::endl;
141
142 std::cout << "\n=== Basic Usage Example completed ===" << std::endl;
143 return 0;
144}
int main()
Simplified facade for creating TCP clients and servers.
Definition tcp_facade.h:95
Main namespace for all Network System components.
Simplified facade for creating TCP clients and servers.