@@ -10,8 +10,8 @@ A header-only C++20 networking library providing cross-platform TCP and UDP mult
1010## Features
1111
1212- ** Cross-platform** : Windows and Unix/Linux support
13- - ** Header-only** : No separate compilation required
14- - ** Modern C++** : C++20 template-based design with CRTP
13+ - ** Header-only** : No separate compilation required (Windows requires linking slick_socket.lib)
14+ - ** Modern C++** : C++20 design with CRTP for most components
1515- ** Asynchronous** : Non-blocking socket operations with timeout handling
1616- ** TCP Communication** : Client and server implementations
1717- ** UDP Multicast** : One-to-many communication support
@@ -151,6 +151,126 @@ int main()
151151}
152152```
153153
154+ ### Creating a TCP Client
155+
156+ ```cpp
157+ #include <slick/socket/tcp_client.h>
158+
159+ class MyClient : public slick::socket::TCPClientBase<MyClient>
160+ {
161+ public:
162+ MyClient(const slick::socket::TCPClientConfig& config)
163+ : TCPClientBase("MyClient", config) {}
164+
165+ void onConnected()
166+ {
167+ std::cout << "Connected to server" << std::endl;
168+ }
169+
170+ void onDisconnected()
171+ {
172+ std::cout << "Disconnected from server" << std::endl;
173+ }
174+
175+ void onData(const uint8_t* data, size_t length)
176+ {
177+ std::string received_data((const char*)data, length);
178+ std::cout << "Received: " << received_data << std::endl;
179+ }
180+ };
181+
182+ int main()
183+ {
184+ slick::socket::TCPClientConfig config;
185+ config.server_address = "127.0.0.1";
186+ config.server_port = 5000;
187+
188+ MyClient client(config);
189+ client.connect();
190+
191+ if (client.is_connected())
192+ {
193+ client.send_data("Hello Server!");
194+ // ... process responses
195+ client.disconnect();
196+ }
197+
198+ return 0;
199+ }
200+ ```
201+
202+ ### Creating a Multicast Sender
203+
204+ ``` cpp
205+ #include < slick/socket/multicast_sender.h>
206+
207+ int main ()
208+ {
209+ slick::socket::MulticastSenderConfig config;
210+ config.multicast_address = "224.0.0.100";
211+ config.port = 12345;
212+ config.ttl = 1; // Local network only
213+
214+ slick::socket::MulticastSender sender("MySender", config);
215+
216+ if (!sender.start())
217+ {
218+ std::cerr << "Failed to start sender" << std::endl;
219+ return -1;
220+ }
221+
222+ // Send data to multicast group
223+ sender.send_data("Hello Multicast World!");
224+
225+ // Check statistics
226+ std::cout << "Packets sent: " << sender.get_packets_sent() << std::endl;
227+
228+ sender.stop();
229+ return 0;
230+ }
231+ ```
232+
233+ ### Creating a Multicast Receiver
234+
235+ ``` cpp
236+ #include < slick/socket/multicast_receiver.h>
237+
238+ class MyReceiver : public slick ::socket::MulticastReceiverBase<MyReceiver >
239+ {
240+ public:
241+ MyReceiver(const slick::socket::MulticastReceiverConfig& config)
242+ : MulticastReceiverBase("MyReceiver", config) {}
243+
244+ void handle_multicast_data(const std::vector<uint8_t>& data, const std::string& sender_address)
245+ {
246+ std::string message(data.begin(), data.end());
247+ std::cout << "Received from " << sender_address << ": " << message << std::endl;
248+ }
249+ };
250+
251+ int main ()
252+ {
253+ slick::socket::MulticastReceiverConfig config;
254+ config.multicast_address = "224.0.0.100";
255+ config.port = 12345;
256+ config.reuse_address = true; // Allow multiple receivers
257+
258+ MyReceiver receiver(config);
259+
260+ if (!receiver.start())
261+ {
262+ std::cerr << "Failed to start receiver" << std::endl;
263+ return -1;
264+ }
265+
266+ // ... receiver runs in background thread
267+ std::this_thread::sleep_for (std::chrono::seconds (30));
268+
269+ receiver.stop();
270+ return 0;
271+ }
272+ ```
273+
154274For more examples, see the [ examples/] ( examples/ ) directory.
155275
156276## Testing
@@ -213,11 +333,11 @@ slick_socket/
213333
214334The library uses a three-file pattern for cross-platform support:
215335
216- - ` component.h ` - Base template class with platform-independent interface
336+ - ` component.h ` - Base class with platform-independent interface
217337- ` component_win32.h ` - Windows implementation
218338- ` component_unix.h ` - Unix/Linux implementation
219339
220- This design uses CRTP (Curiously Recurring Template Pattern) for compile-time polymorphism without virtual function overhead.
340+ Most components use CRTP (Curiously Recurring Template Pattern) for compile-time polymorphism without virtual function overhead. ` MulticastSender ` is implemented as a regular class without CRTP for simpler usage .
221341
222342## License
223343
0 commit comments