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

Public utility functions for working with socket addresses, conversions, and formatting. More...

Collaboration diagram for Utility Functions:

Functions

std::string jsocketpp::addressToString (const sockaddr_storage &addr)
 Converts a socket address to a human-readable "IP:port" string.
void jsocketpp::stringToAddress (const std::string &str, sockaddr_storage &addr)
 Parses an "IP:port" string into a sockaddr_storage structure.

Detailed Description

Public utility functions for working with socket addresses, conversions, and formatting.

This module includes general-purpose helpers that simplify common socket-related tasks such as:

  • Converting socket addresses to strings
  • Parsing string representations into sockaddr structures
  • Formatting or inspecting address data across socket types

These functions are protocol-agnostic and can be used with TCP, UDP, and Unix domain sockets.

See also
addressToString()
stringToAddress()

Function Documentation

◆ addressToString()

std::string jsocketpp::addressToString ( const sockaddr_storage & addr)

Converts a socket address to a human-readable "IP:port" string.

This utility function transforms a sockaddr_storage structure into a string representation using getnameinfo(), suitable for logging, diagnostics, or display. It supports both IPv4 (AF_INET) and IPv6 (AF_INET6) addresses and outputs the address in the form:

  • "192.168.1.10:8080" for IPv4
  • "[::1]:12345" for IPv6 (note: bracket wrapping is not added automatically)

For unknown or unsupported address families, the function returns "unknown".

Parameters
[in]addrA fully populated sockaddr_storage containing the IP address and port.
Returns
A string in the format "IP:port" (e.g., "127.0.0.1:8080" or "::1:53").
Exceptions
SocketExceptionIf getnameinfo() fails to resolve the IP or port for the provided address.
Note
This function does not add square brackets around IPv6 addresses. If you need bracketed formatting (e.g., for URL embedding), you must post-process the result.
sockaddr_storage addr = ...;
std::string str = addressToString(addr);
std::cout << "Connected to " << str << "\n";
std::string addressToString(const sockaddr_storage &addr)
Converts a socket address to a human-readable "IP:port" string.
Definition common.cpp:423

◆ stringToAddress()

void jsocketpp::stringToAddress ( const std::string & str,
sockaddr_storage & addr )

Parses an "IP:port" string into a sockaddr_storage structure.

This utility function takes a string of the form "host:port" and resolves it into a platform-compatible sockaddr_storage structure using getaddrinfo(). It supports both IPv4 and IPv6 addresses, including:

  • "127.0.0.1:8080"
  • "::1:1234"
  • IPv6 addresses without square brackets (e.g., "2001:db8::1:443")

The resulting structure can be passed directly to socket functions like connect(), bind(), or sendto().

Parameters
[in]strThe string to parse, in the format "ip:port". Must not include brackets around IPv6 addresses.
[out]addrThe output sockaddr_storage structure to populate.
Exceptions
SocketException
  • If the string is missing a : separator
  • If the port cannot be parsed
  • If getaddrinfo() fails to resolve the address
Note
This function assumes numeric host and port. No DNS resolution is performed.
sockaddr_storage addr;
stringToAddress("192.168.0.10:9000", addr);
connect(sockFd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr));
void stringToAddress(const std::string &str, sockaddr_storage &addr)
Parses an "IP:port" string into a sockaddr_storage structure.
Definition common.cpp:460