Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
tcp_client.cpp
Go to the documentation of this file.
1/*****************************************************************************
2BSD 3-Clause License
3
4Copyright (c) 2024, Network System Project
5All rights reserved.
6*****************************************************************************/
7
33
34#include <chrono>
35#include <iostream>
36#include <string>
37#include <thread>
38#include <vector>
39
40using namespace kcenon::network;
41
42int main() {
43 std::cout << "=== TCP Client Example ===" << std::endl;
44
45 // Create TCP facade and client
47 constexpr uint16_t port = 9000;
48 const std::string host = "127.0.0.1";
49
50 auto client = tcp.create_client({
51 .host = host,
52 .port = port,
53 .client_id = "ExampleClient",
54 });
55
56 // Set up receive callback to print server responses
57 client->set_receive_callback([](const std::vector<uint8_t>& data) {
58 std::string message(data.begin(), data.end());
59 std::cout << "[Client] Received: " << message << std::endl;
60 });
61
62 // Set up connection and disconnection callbacks
63 client->set_connected_callback([]() {
64 std::cout << "[Client] Connected to server." << std::endl;
65 });
66
67 client->set_disconnected_callback([]() {
68 std::cout << "[Client] Disconnected from server." << std::endl;
69 });
70
71 // Set up error callback
72 client->set_error_callback([](std::error_code ec) {
73 std::cerr << "[Client] Error: " << ec.message() << std::endl;
74 });
75
76 // Connect to the server
77 std::cout << "[Client] Connecting to " << host << ":" << port << "..." << std::endl;
78
79 auto connect_result = client->start(host, port);
80 if (connect_result.is_err()) {
81 std::cerr << "[Client] Connection failed: " << connect_result.error().message
82 << std::endl;
83 return 1;
84 }
85
86 // Wait for connection to establish
87 std::this_thread::sleep_for(std::chrono::milliseconds(200));
88
89 // Check connection status
90 if (!client->is_connected()) {
91 std::cerr << "[Client] Not connected." << std::endl;
92 client->stop();
93 return 1;
94 }
95
96 // Send text messages
97 std::vector<std::string> messages = {
98 "Hello, server!",
99 "This is a TCP client example.",
100 "Goodbye!",
101 };
102
103 for (const auto& msg : messages) {
104 std::vector<uint8_t> data(msg.begin(), msg.end());
105 std::cout << "[Client] Sending: " << msg << std::endl;
106
107 auto send_result = client->send(std::move(data));
108 if (send_result.is_err()) {
109 std::cerr << "[Client] Send failed: " << send_result.error().message
110 << std::endl;
111 }
112
113 // Wait for echo response
114 std::this_thread::sleep_for(std::chrono::milliseconds(500));
115 }
116
117 // Send binary data
118 std::cout << "[Client] Sending binary data..." << std::endl;
119 std::vector<uint8_t> binary_data = {0x01, 0x02, 0x03, 0x04, 0xFF};
120 auto binary_result = client->send(std::move(binary_data));
121 if (binary_result.is_err()) {
122 std::cerr << "[Client] Binary send failed: " << binary_result.error().message
123 << std::endl;
124 }
125
126 // Wait for response
127 std::this_thread::sleep_for(std::chrono::milliseconds(500));
128
129 // Disconnect
130 std::cout << "[Client] Disconnecting..." << std::endl;
131 auto stop_result = client->stop();
132 if (stop_result.is_err()) {
133 std::cerr << "[Client] Stop error: " << stop_result.error().message << std::endl;
134 }
135
136 std::cout << "[Client] Done." << std::endl;
137 return 0;
138}
Simplified facade for creating TCP clients and servers.
Definition tcp_facade.h:95
Main namespace for all Network System components.
int main()
Simplified facade for creating TCP clients and servers.