11# ip-sockets-cpp-lite 🔌
22
3+ [ ![ Build examples] ( https://github.com/biaks/ip-sockets-cpp-lite/actions/workflows/cmake.yml/badge.svg )] ( https://github.com/biaks/ip-sockets-cpp-lite/actions/workflows/cmake.yml )
4+
35** Simplicity. Lightweight. Cross-platform.**
46
5- ip-sockets-cpp-lite is a fast, header-only, dependency-free, cross-platform C++ library that makes programming UDP/TCP sockets and working with IPv4/IPv6 addresses as easy as possible.
7+ It's a fast, header-only, dependency-free, cross-platform C++ library that makes programming UDP/TCP sockets and working with IPv4/IPv6 addresses as easy as possible.
68Forget about endless ` getaddrinfo ` calls, confusion with ` sockaddr_in ` /` sockaddr_in6 ` , and platform-specific workarounds.
79
810``` cpp
@@ -22,6 +24,73 @@ sock.send("Hello!", 6);
2224- ** 🔒 Predictable** - full control over everything
2325- ** 📋 Human-readable errors** - clear error codes instead of cryptic errno values
2426
27+ ## Smart IP Address Handling 🌐
28+
29+ Working with IP addresses has never been easier:
30+
31+ ### Create from anything
32+ ``` cpp
33+ // IPv4 addresses - all the common formats
34+ ip4_t ip0 = " 192.168.1.1" ; // from string
35+ ip4_t ip1 = 0xc0a80101 ; // from uint32
36+ ip4_t ip2 = {192, 168, 1, 1}; // from bytes
37+ ip4_t ip3 = "0xc0a80101"; // from hex string
38+ ip4_t ip4 = "3232235777"; // from decimal string
39+ ip4_t ip5 = "127.1"; // 127.0.0.1 (missing bytes are zero)
40+ // IPv6 addresses - all the common formats
41+ ip6_t ip6 = "fe80::"; // link-local address
42+ ip6_t ip7 = "::1"; // loopback
43+ ip6_t ip8 = "64:ff9b::10.0.0.7"; // IPv4-translated (NAT64)
44+ ip6_t ip9 = "11.0.0.1"; // auto IPv4-mapped to ::ffff:11.0.0.1
45+ ```
46+
47+ ### Zero-copy overlay on existing memory
48+ ```cpp
49+ uint8_t packet[14] = {0x45, 0x00, 0x00, 0x54, 0xc0, 0xa8, 0x01, 0x01, ...};
50+ // Treat memory region as ip4_t without copying!
51+ ip4_t& src_ip = *reinterpret_cast<ip4_t*>(&packet[12]);
52+ ```
53+
54+ ### Network operations made simple
55+ ``` cpp
56+ ip4_t ip = " 192.168.1.100" ;
57+ ip4_t mask (24); // 255.255.255.0 from prefix
58+ ip4_t network = ip & mask; // 192.168.1.0
59+
60+ ip6_t ipv6 = "2001:db8::1";
61+ ip6_t ipv6_from_v4 = ip4_t("10.0.0.1"); // ::ffff:10.0.0.1 (IPv4-mapped)
62+ ```
63+
64+ ### Address + port as one type
65+ ```cpp
66+ addr4_t server = "192.168.1.100:8080"; // IP and port together
67+ addr6_t server6 = "[2001:db8::1]:8080";
68+ std::cout << server; // prints "192.168.1.100:8080"
69+ ```
70+
71+ ## Two Ways to Use IP and Address Types 🔧
72+
73+ ``` cpp
74+ // Direct types (when IP version is known)
75+ ip4_t ip4 = " 192.168.1.1" ; // IPv4
76+ ip6_t ip6 = " 2001:db8::1" ; // IPv6
77+ addr4_t addr4 = " 192.168.1.1:8080" ; // IPv4 + port
78+ addr6_t addr6 = " [::1]:8080" ; // IPv6 + port
79+
80+ // Generic types (template-friendly)
81+ ip_t <v4> ip4 = " 192.168.1.1" ; // same as ip4_t
82+ ip_t <v6> ip6 = " 2001:db8::1" ; // same as ip6_t
83+ addr_t <v4> addr4 = " 192.168.1.1:8080" ; // same as addr4_t
84+ addr_t <v6> addr6 = " [::1]:8080" ; // same as addr6_t
85+
86+ // Perfect for templates!
87+ template <ip_type_e Type>
88+ void print_endpoint (const addr_t<Type >& endpoint) {
89+ std::cout << "Connecting to: " << endpoint << '\n';
90+ }
91+ ```
92+ **Choose what fits your code:** use concrete types for simplicity, or generic types for maximum flexibility in templates!
93+
2594## Quick Start 🏁
2695
2796### 1. Installation
@@ -31,7 +100,7 @@ Simply copy three files into your project:
31100- `udp_socket.h` — UDP sockets
32101- `tcp_socket.h` — TCP sockets
33102
34- Then include one of what you need:
103+ Then include what you need:
35104```cpp
36105#include "ip_address.h" // work only with ipv4/ipv6 addresses
37106#include "udp_socket.h" // work with UDP ipv4/ipv6 client/server sockets
@@ -78,7 +147,7 @@ int main() {
78147 // Create a UDP server on port 8080
79148 udp_socket_t<v4, socket_type_e::server> server;
80149
81- if (server.open("0.0.0.0:8080") ! = no_error) {
150+ if (server.open("0.0.0.0:8080") = = no_error) {
82151
83152 std::cout << "Server listening on port 8080\n";
84153
@@ -110,7 +179,7 @@ int main() {
110179 // Create a TCP server
111180 tcp_socket_t<v4, socket_type_e::server> server;
112181
113- if (server.open("0.0.0.0:8080") ! = no_error) {
182+ if (server.open("0.0.0.0:8080") = = no_error) {
114183
115184 std::cout << "TCP server listening on port 8080\n";
116185
@@ -161,6 +230,9 @@ sock.send("Hello", 5);
161230- Network prefixes and masks
162231- IPv4-over-IPv6 mapping
163232- Address + port as a single unit
233+ - Zero-copy overlay on existing memory buffers
234+ - Rich constructors from strings, numbers, bytes
235+ - Flexible parsing of various formats (hex, decimal, dotted)
164236
165237### UDP Sockets (` udp_socket.h ` )
166238- Client and server modes
0 commit comments