jsocketpp 1.0
A cross-platform C++20 socket library.
Loading...
Searching...
No Matches
jsocketpp Namespace Reference

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.

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...
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  SocketTimeoutException
 Exception class for socket operations that time out. More...
class  UnixSocket
 Cross-platform abstraction for Unix domain sockets. More...

Typedefs

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...

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)
 Returns a human-readable error message for a socket error code.
std::string SocketErrorMessageWrap (int error, bool gaiStrerror=false)
 Returns a human-readable error message for a socket error code, with exception safety.
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.
template<>
std::string DatagramSocket::read< std::string > ()
 Read a string from the socket (connected UDP).
template<>
std::string DatagramSocket::recvFrom< std::string > (std::string *senderAddr, Port *senderPort)
 Receive a datagram as a string (connectionless).

Variables

constexpr SOCKET INVALID_SOCKET = -1
constexpr SOCKET SOCKET_ERROR = -1
constexpr std::size_t DefaultBufferSize = 4096
 Default internal buffer size (in bytes) for TCP socket read operations.

Detailed Description

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:

  • Socket: TCP client socket implementation for stream-based communication
  • ServerSocket: TCP server socket for accepting client connections
  • DatagramSocket: UDP socket implementation for connectionless communication
  • MulticastSocket: Extended UDP socket supporting IP multicast operations
  • UnixSocket: Unix domain socket implementation for local IPC

Features:

  • Exception-based error handling (SocketException)
  • RAII-compliant resource management
  • Support for both IPv4 and IPv6
  • Cross-platform compatibility (Windows/Unix)
  • Modern C++ design with move semantics

Design Philosophy:

  • Familiar interface for Java developers
  • Modern C++ practices and idioms
  • Exception-based error handling
  • Resource safety through RAII
  • Explicit over implicit behavior
Note
All classes in this namespace are not thread-safe unless explicitly stated. Each socket should be used from a single thread at a time.

Typedef Documentation

◆ SOCKADDR_UN

typedef sockaddr_un jsocketpp::SOCKADDR_UN

◆ SOCKET

typedef int jsocketpp::SOCKET

Enumeration Type Documentation

◆ ShutdownMode

enum class jsocketpp::ShutdownMode
strong

Enum for socket shutdown modes.

Used to specify how to shutdown a socket (read, write, or both).

Enumerator
Read 

Shutdown read operations (SHUT_RD or SD_RECEIVE)

Write 

Shutdown write operations (SHUT_WR or SD_SEND)

Both 

Shutdown both read and write operations (SHUT_RDWR or SD_BOTH)

Function Documentation

◆ CleanupSockets()

int jsocketpp::CleanupSockets ( )
inlineconstexpr

◆ CloseSocket()

int jsocketpp::CloseSocket ( SOCKET fd)
inline

◆ DatagramSocket::read< std::string >()

template<>
std::string jsocketpp::DatagramSocket::read< std::string > ( )
inline

Read a string from the socket (connected UDP).

Returns
The received string.
Exceptions
SocketExceptionon error.

◆ DatagramSocket::recvFrom< std::string >()

template<>
std::string jsocketpp::DatagramSocket::recvFrom< std::string > ( std::string * senderAddr,
Port * senderPort )
inline

Receive a datagram as a string (connectionless).

Parameters
senderAddr[out] String to store sender's address.
senderPort[out] Variable to store sender's port.
Returns
The received message as a std::string.
Exceptions
SocketExceptionon error.

◆ getHostAddr()

std::vector< std::string > jsocketpp::getHostAddr ( )

Get all local network interface addresses as strings.

Returns
Vector of strings describing each interface and address.

◆ GetSocketError()

int jsocketpp::GetSocketError ( )
inline

◆ inet_ntop_aux()

const char * jsocketpp::inet_ntop_aux ( int af,
const void * src,
char * dst,
socklen_t size )

◆ InitSockets()

int jsocketpp::InitSockets ( )
inlineconstexpr

◆ ioctlsocket()

int jsocketpp::ioctlsocket ( const SOCKET fd,
const long cmd,
u_long * argp )
inline

◆ SocketErrorMessage()

std::string jsocketpp::SocketErrorMessage ( int error,
bool gaiStrerror = false )

Returns a human-readable error message for a socket error code.

On Windows:

  • For Winsock error codes (10000-11999), uses strerror (these include WSAETIMEDOUT).
  • For system error codes (from GetLastError()), uses FormatMessageA. On POSIX:
  • For getaddrinfo-related errors, uses gai_strerror if gaiStrerror is true.
  • Otherwise, uses strerror.
Parameters
errorThe error code (platform-specific).
gaiStrerrorIf true, interpret error as a getaddrinfo error code (POSIX only).
Returns
A string describing the error.

◆ SocketErrorMessageWrap()

std::string jsocketpp::SocketErrorMessageWrap ( int error,
bool gaiStrerror = false )

Returns a human-readable error message for a socket error code, with exception safety.

Like SocketErrorMessage, but guarantees not to throw exceptions (e.g., for use in destructors). If an error occurs while generating the message, returns a fallback string (on Windows, uses strerror for Winsock codes).

Parameters
errorThe error code (platform-specific).
gaiStrerrorIf true, interpret error as a getaddrinfo error code (POSIX only).
Returns
A string describing the error, or a fallback if an exception occurs.

Variable Documentation

◆ INVALID_SOCKET

SOCKET jsocketpp::INVALID_SOCKET = -1
constexpr

◆ SOCKET_ERROR

SOCKET jsocketpp::SOCKET_ERROR = -1
constexpr