![]() |
jsocketpp 1.0
A cross-platform C++20 socket library.
|
A C++ socket library providing Java-style networking interfaces. More...
Namespaces | |
| namespace | internal |
| Implementation-only utilities and platform abstractions for jsocketpp. | |
| namespace | net |
| Endianness utilities for network byte order conversion. | |
| namespace | detail |
| Implementation details and type traits for jsocketpp buffer management. | |
Classes | |
| struct | BufferView |
| Represents a raw writable memory region for scatter/gather I/O. More... | |
| class | DatagramPacket |
| Represents a UDP datagram packet, encapsulating both payload and addressing information. More... | |
| struct | DatagramReadOptions |
| Options controlling a single UDP receive operation. More... | |
| struct | DatagramReadResult |
| Telemetry data about a single UDP datagram receive operation. More... | |
| struct | ReadExactOptions |
| Policy for enforcing an exact-byte receive on a single UDP datagram. More... | |
| class | DatagramSocket |
| Cross-platform UDP socket class with Java-style interface. More... | |
| class | MulticastSocket |
| Cross-platform multicast UDP socket class (IPv4/IPv6). More... | |
| class | ServerSocket |
| TCP server socket abstraction for cross-platform C++ networking. More... | |
| class | Socket |
| TCP client connection abstraction (Java-like interface). More... | |
| class | SocketException |
| Represents socket-related errors in the jsocketpp library. More... | |
| class | SocketInitializer |
| Helper class to initialize and cleanup sockets (RAII). More... | |
| class | SocketOptions |
| Public base class for raw socket option access via setsockopt() and getsockopt(). More... | |
| class | SocketTimeoutException |
| Exception class for socket operations that time out. More... | |
| class | UnixSocket |
| Cross-platform abstraction for Unix domain sockets. More... | |
Typedefs | |
| typedef long | ssize_t |
| typedef int | SOCKET |
| typedef sockaddr_un | SOCKADDR_UN |
| using | Port = std::uint16_t |
| Type alias representing a TCP or UDP port number (1–65535). | |
Enumerations | |
| enum class | ShutdownMode { Read , Write , Both } |
| Enum for socket shutdown modes. More... | |
| enum class | DatagramReceiveMode : std::uint8_t { DatagramReceiveMode::NoPreflight = 0 , DatagramReceiveMode::PreflightSize = 1 , DatagramReceiveMode::PreflightMax = 2 } |
| Receive-time sizing policy for UDP datagrams. More... | |
| enum class | Direction : std::uint8_t { Direction::Read , Direction::Write , Direction::ReadWrite } |
| I/O readiness selector used by waitReady(). More... | |
Functions | |
| int | InitSockets () |
| int | CleanupSockets () |
| int | GetSocketError () |
| int | CloseSocket (SOCKET fd) |
| const char * | inet_ntop_aux (int af, const void *src, char *dst, socklen_t size) |
| int | ioctlsocket (const SOCKET fd, const long cmd, u_long *argp) |
| std::vector< std::string > | getHostAddr () |
| Get all local network interface addresses as strings. | |
| std::string | SocketErrorMessage (int error, bool gaiStrerror=false) |
| Convert a socket-related error code to a human-readable message. | |
| bool | isIPv4MappedIPv6 (const sockaddr_in6 *addr6) |
| Checks if a given sockaddr_in6 represents an IPv4-mapped IPv6 address. | |
| sockaddr_in | convertIPv4MappedIPv6ToIPv4 (const sockaddr_in6 &addr6) |
| Converts an IPv4-mapped IPv6 address to a pure IPv4 sockaddr_in. | |
| std::string | ipFromSockaddr (const sockaddr *addr, bool convertIPv4Mapped=true) |
| Extracts a human-readable IP address from a socket address structure. | |
| Port | portFromSockaddr (const sockaddr *addr) |
| Extracts the port number from a socket address structure. | |
| std::string | addressToString (const sockaddr_storage &addr) |
| Converts a socket address to a human-readable "IP:port" string. | |
| void | stringToAddress (const std::string &str, sockaddr_storage &addr) |
| Parses an "IP:port" string into a sockaddr_storage structure. | |
Variables | |
| constexpr SOCKET | INVALID_SOCKET = -1 |
| constexpr SOCKET | SOCKET_ERROR = -1 |
| constexpr std::size_t | DefaultBufferSize = 4096 |
| Default internal buffer size (in bytes) for socket read operations. | |
| constexpr std::size_t | DefaultDatagramReceiveSize = 8192 |
| Fallback receive size (in bytes) for UDP datagrams when the exact size is unknown. | |
| constexpr std::size_t | MaxDatagramPayloadSafe = 65507 |
| Maximum UDP payload size (in bytes) that is safely valid across common stacks. | |
| constexpr std::size_t | MaxUdpPayloadIPv4 = 65507 |
| Maximum UDP payload size (in bytes) over IPv4. | |
| constexpr std::size_t | MaxUdpPayloadIPv6 = 65527 |
| Theoretical maximum UDP payload size (in bytes) over IPv6. | |
A C++ socket library providing Java-style networking interfaces.
The jsocketpp namespace contains classes and utilities for network programming with an API design inspired by Java's networking classes. It provides:
Core Socket Classes:
Features:
Design Philosophy:
| typedef sockaddr_un jsocketpp::SOCKADDR_UN |
| typedef int jsocketpp::SOCKET |
| typedef long jsocketpp::ssize_t |
|
strong |
|
inlineconstexpr |
|
inline |
| std::vector< std::string > jsocketpp::getHostAddr | ( | ) |
Get all local network interface addresses as strings.
|
inline |
| const char * jsocketpp::inet_ntop_aux | ( | int | af, |
| const void * | src, | ||
| char * | dst, | ||
| socklen_t | size ) |
Redefine because not available on Windows XP http://stackoverflow.com/questions/13731243/what-is-the-windows-xp-equivalent-of-inet-pton-or-inetpton
|
inlineconstexpr |
|
inline |
| std::string jsocketpp::ipFromSockaddr | ( | const sockaddr * | addr, |
| bool | convertIPv4Mapped = true ) |
Extracts a human-readable IP address from a socket address structure.
This function returns the numeric IP address (IPv4 or IPv6) contained in the given sockaddr. If the address is an IPv4-mapped IPv6 (::ffff:a.b.c.d) and convertIPv4Mapped is true, the result is converted to the original IPv4 form (e.g., "192.168.1.10").
| addr | Pointer to a sockaddr structure (must be AF_INET or AF_INET6). |
| convertIPv4Mapped | Whether to convert IPv4-mapped IPv6 addresses to plain IPv4. |
| SocketException | if the address family is unsupported or conversion fails. |
| Port jsocketpp::portFromSockaddr | ( | const sockaddr * | addr | ) |
Extracts the port number from a socket address structure.
Retrieves the port from a sockaddr containing either an IPv4 or IPv6 address, converting it from network byte order to host byte order.
| addr | Pointer to a sockaddr structure (must be AF_INET or AF_INET6). |
| SocketException | if the address family is unsupported. |
| std::string jsocketpp::SocketErrorMessage | ( | int | error, |
| bool | gaiStrerror = false ) |
Convert a socket-related error code to a human-readable message.
This helper produces best-effort, descriptive text for errors originating from:
Usage rules:
gaiStrerror set to false.gaiStrerror set to true. Do not call WSAGetLastError() for getaddrinfo/getnameinfo, as those functions return their error code.Platform behavior:
Thread-safety notes:
| [in] | error | Numeric error code. For errno/WSA* paths, pass errno or GetSocketError() (WSAGetLastError()). For getaddrinfo/getnameinfo, pass the function's non-zero return value (an EAI_* code). |
| [in] | gaiStrerror | Set to true if error is an address-resolution error from getaddrinfo/getnameinfo (EAI_* domain). Set to false for traditional socket/Winsock errors (errno/WSA* domain). Default is false. |
error is zero.| This | function is designed not to throw. In failure cases, it returns a conservative string such as "Unknown error <code>". |
|
constexpr |
|
constexpr |