SFML - Simple and Fast Multimedia Library

SFML 3.1.0
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Friends | List of all members
sf::IpAddress Class Reference
Network module

Encapsulate an IPv4 network address. More...

`#include <SFML/Network/IpAddress.hpp>`

## Public Types
enum class | Type { IpV4 , IpV6 }
| Type of IP address. More...

## Public Member Functions
| IpAddress (std::uint8_t byte0, std::uint8_t byte1, std::uint8_t byte2, std::uint8_t byte3)
| Construct an IPv4 address from 4 bytes.
| IpAddress (std::uint32_t address)
| Construct an IPv4 address from a 32-bit integer.
| IpAddress (std::array< std::uint8_t, 16 > bytes)
| Construct an IPv6 address from 16 bytes.
std::string | toString () const
| Get a string representation of the address.
std::uint32_t | toInteger () const
| Get an integer representation of the address.
std::array< std::uint8_t, 16 > | toBytes () const
| Get an array of bytes representing the address.
Type | getType () const
| Get the type of this IP address.
bool | isV4 () const
| Check if this IP address is an IPv4 address.
bool | isV6 () const
| Check if this IP address is an IPv6 address.

## Static Public Member Functions
static std::optional< IpAddress > | resolve (std::string_view address)
| Construct the address from a null-terminated string view.
static std::optional< IpAddress > | fromString (std::string_view address)
| Try to construct an address from its string representation.
static std::optional< IpAddress > | getLocalAddress (Type type=Type::IpV4)
| Get the computer's local address.
static std::optional< IpAddress > | getPublicAddress (Time timeout=Time::Zero, std::optional< Type > type=Type::IpV4, bool secure=false)
| Get the computer's public address.

## Static Public Attributes
static const IpAddress | Any
| The same as AnyV4.
static const IpAddress | LocalHost
| The same as LocalHostV4.
static const IpAddress | Broadcast
| The same as BroadcastV4.
static const IpAddress | AnyV4
| Value representing any IPv4 address (0.0.0.0).
static const IpAddress | LocalHostV4
| The "localhost" IPv4 address (for connecting a computer to itself locally).
static const IpAddress | BroadcastV4
| The "broadcast" IPv4 address (for sending UDP messages to everyone on a local network).
static const IpAddress | AnyV6
| Value representing any IPv6 address (::).
static const IpAddress | LocalHostV6
| The "localhost" IPv6 address (for connecting a computer to itself locally).

## Friends
bool | operator< (IpAddress left, IpAddress right)
| Overload of operator< to compare two IP addresses.

## Detailed Description

Encapsulate an IPv4 network address.

sf::IpAddress is a utility class for manipulating network addresses.

It provides a set a implicit constructors and conversion functions to easily build or transform an IP address from/to various representations.

Usage example:

auto a0 = sf::IpAddress::fromString("127.0.0.1"); // the local host IPv4 address
auto a1 = sf::IpAddress::fromString("::1"); // the local host IPv6 address
auto a2 = sf::IpAddress::Broadcast; // the broadcast address
sf::IpAddress a3(192, 168, 1, 56); // a local IPv4 address
sf::IpAddress a4({0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5d, 0x58, 0x84, 0xef, 0xc1, 0x34, 0xfd}); // a local IPv6 address
auto a5 = sf::IpAddress::fromString("89.54.1.169"); // a distant IPv4 address
auto a6 = sf::IpAddress::fromString("2606:4700:4700::1111"); // a distant IPv6 address
auto a7 = sf::IpAddress::getLocalAddress(sf::IpAddress::Type::IpV4); // my IPv4 address on the local network
auto a8 = sf::IpAddress::getLocalAddress(sf::IpAddress::Type::IpV6); // my IPv6 address on the local network
auto a9 = sf::IpAddress::getPublicAddress(sf::Time::Zero, sf::IpAddress::Type::IpV4); // my IPv4 address on the internet
auto a10 = sf::IpAddress::getPublicAddress(sf::Time::Zero, sf::IpAddress::Type::IpV6); // my IPv6 address on the internet
sf::IpAddress
Encapsulate an IPv4 network address.
Definition IpAddress.hpp:51
sf::IpAddress::getPublicAddress
static std::optional< IpAddress > getPublicAddress(Time timeout=Time::Zero, std::optional< Type > type=Type::IpV4, bool secure=false)
Get the computer's public address.
sf::IpAddress::Broadcast
static const IpAddress Broadcast
The same as BroadcastV4.
Definition IpAddress.hpp:290
sf::IpAddress::fromString
static std::optional< IpAddress > fromString(std::string_view address)
Try to construct an address from its string representation.
sf::IpAddress::Type::IpV4
@ IpV4
IPv4 address.
Definition IpAddress.hpp:59
sf::IpAddress::Type::IpV6
@ IpV6
IPv6 address.
Definition IpAddress.hpp:60
sf::IpAddress::getLocalAddress
static std::optional< IpAddress > getLocalAddress(Type type=Type::IpV4)
Get the computer's local address.
sf::Time::Zero
static const Time Zero
Predefined "zero" time value.
Definition Time.hpp:110

To resolve hostnames to IP addresses, use the sf::Dns::resolve() function.

Definition at line 50 of file IpAddress.hpp.

## Member Enumeration Documentation

## ◆ Type

enum class sf::IpAddress::Type
| strong

Type of IP address.

Enumerator
IpV4 | IPv4 address.
IpV6 | IPv6 address.

Definition at line 57 of file IpAddress.hpp.

## Constructor & Destructor Documentation

## ◆ IpAddress() [1/3]

sf::IpAddress::IpAddress | ( | std::uint8_t | byte0,
|  | std::uint8_t | byte1,
|  | std::uint8_t | byte2,
|  | std::uint8_t | byte3 )

Construct an IPv4 address from 4 bytes.

Calling IpAddress(a, b, c, d) is equivalent to calling IpAddress::resolve("a.b.c.d"), but safer as it doesn't have to parse a string to get the address components.

Parameters

  byte0 | First byte of the address
  byte1 | Second byte of the address
  byte2 | Third byte of the address
  byte3 | Fourth byte of the address

## ◆ IpAddress() [2/3]

sf::IpAddress::IpAddress | ( | std::uint32_t | address | ) |
| explicit

Construct an IPv4 address from a 32-bit integer.

This constructor uses the internal representation of the address directly. It should be used for optimization purposes, and only if you got that representation from IpAddress::toInteger().

Parameters

  address | 4 bytes of the address packed into a 32-bit integer

See also
  toInteger

## ◆ IpAddress() [3/3]

sf::IpAddress::IpAddress | ( | std::array< std::uint8_t, 16 > | bytes | ) |

Construct an IPv6 address from 16 bytes.

Parameters

  bytes | Array of 16 bytes containing the address

## Member Function Documentation

## ◆ fromString()

std::optional< IpAddress > sf::IpAddress::fromString | ( | std::string_view | address | ) |
| staticnodiscard

Try to construct an address from its string representation.

The string should contain either a valid representation of an IPv4 address in dotted-decimal notation or a valid representation of an IPv6 address in internet standard notation.

Examples:

- 192.168.1.56
- FEDC:BA98:7654:3210:FEDC:BA98:7654:3210
- fedc:ba98:7654:3210:fedc:ba98:7654:3210
- 1080:0:0:0:8:800:200C:417A
- 1080::8:800:200C:417A
- FF01::101
- ::1
- ::
- 0:0:0:0:0:0:13.1.68.3
- ::13.1.68.3
- 0:0:0:0:0:FFFF:129.144.52.38
- ::FFFF:129.144.52.38

Parameters

  address | String representation of the address

Returns
  Address if provided argument was a valid string represenation of an IP address, otherwise std::nullopt

See also
  toString

## ◆ getLocalAddress()

std::optional< IpAddress > sf::IpAddress::getLocalAddress | ( | Type | type = Type::IpV4 | ) |
| staticnodiscard

Get the computer's local address.

The local address is the address of the computer from the LAN point of view, i.e. something like 192.168.1.56. It is meaningful only for communications over the local network. Unlike getPublicAddress, this function is fast and may be used safely anywhere.

Parameters

  type | Type of local address

Returns
  Local IP address of the computer on success, std::nullopt otherwise

See also
  getPublicAddress

## ◆ getPublicAddress()

std::optional< IpAddress > sf::IpAddress::getPublicAddress | ( | Time | timeout = Time::Zero,
|  | std::optional< Type > | type = Type::IpV4,
|  | bool | secure = false )
| staticnodiscard

Get the computer's public address.

The public address is the address of the computer from the point of view of the internet, i.e. something like 89.54.1.169 or 2600:1901:0:13e0::1 as opposed to a private or local address like 192.168.1.56 or fe80::1234:5678:9abc. It is necessary for communication with hosts outside of the local network.

The only way to reliably get the public address is to send data to a host on the internet and see what the origin address is; as a consequence, this function depends on both your network connection and the server, and may be very slow. You should try to use it as little as possible. Because this function depends on the network connection and on a distant server, you can specify a time limit if you don't want your program to get stuck waiting in case there is a problem; this limit is deactivated by default.

If tamper resistance is required, setting secure to true will make use of verified HTTPS connections to get the address.

Parameters

  timeout | Maximum time to wait
  type | The type of public address to get, std::nullopt to specify no preference
  secure | true to retrieve the public address via a secure HTTPS connection, false to retrieve via DNS or an insecure connection

Returns
  Public IP address of the computer on success, std::nullopt otherwise

See also
  getLocalAddress

## ◆ getType()

Type sf::IpAddress::getType | ( |  | ) | const
| nodiscard

Get the type of this IP address.

Returns
  The type of this IP address (IPv4 or IPv6)

## ◆ isV4()

bool sf::IpAddress::isV4 | ( |  | ) | const
| nodiscard

Check if this IP address is an IPv4 address.

Equivalent to getType() == Type::IPv4

Returns
  true if this is an IPv4 address, false otherwise

## ◆ isV6()

bool sf::IpAddress::isV6 | ( |  | ) | const
| nodiscard

Check if this IP address is an IPv6 address.

Equivalent to getType() == Type::IPv6

Returns
  true if this is an IPv6 address, false otherwise

## ◆ resolve()

std::optional< IpAddress > sf::IpAddress::resolve | ( | std::string_view | address | ) |
| staticnodiscard

Construct the address from a null-terminated string view.

Deprecated
  Use sf::Dns::resolve() instead.

Here address can be either a decimal address (ex: "192.168.1.56") or a network name (ex: "localhost").

This function will only resolve to an IPv4 address. Use Dns::resolve() to resolve to IPv6 addresses as well.

Parameters

  address | IP address or network name

Returns
  Address if provided argument was valid, otherwise std::nullopt

## ◆ toBytes()

std::array< std::uint8_t, 16 > sf::IpAddress::toBytes | ( |  | ) | const
| nodiscard

Get an array of bytes representing the address.

This function can only be called if this is an IPv6 address. Check with isV6() before calling this function.

The returned array is the internal representation of the address, and should be used for optimization purposes only (like sending the address through a socket). The array produced by this function can then be converted back to a sf::IpAddress with the proper constructor.

Returns
  16-byte array representation of the address

See also
  toString

## ◆ toInteger()

std::uint32_t sf::IpAddress::toInteger | ( |  | ) | const
| nodiscard

Get an integer representation of the address.

This function can only be called if this is an IPv4 address. Check with isV4() before calling this function.

The returned number is the internal representation of the address, and should be used for optimization purposes only (like sending the address through a socket). The integer produced by this function can then be converted back to a sf::IpAddress with the proper constructor.

Returns
  32-bits unsigned integer representation of the address

See also
  toString

## ◆ toString()

std::string sf::IpAddress::toString | ( |  | ) | const
| nodiscard

Get a string representation of the address.

The returned string is the decimal representation of the IP address (like "192.168.1.56" or "FF01::101"), even if it was constructed from a host name.

Returns
  String representation of the address

See also
  fromString, toInteger

## Friends And Related Symbol Documentation

## ◆ operator<

bool operator< | ( | IpAddress | left,
|  | IpAddress | right )
| friend

Overload of operator< to compare two IP addresses.

Parameters

  left | Left operand (a IP address)
  right | Right operand (a IP address)

Returns
  true if left is lesser than right

## Member Data Documentation

## ◆ Any

const IpAddress sf::IpAddress::Any
| static

The same as AnyV4.

Definition at line 288 of file IpAddress.hpp.

## ◆ AnyV4

const IpAddress sf::IpAddress::AnyV4
| static

Value representing any IPv4 address (0.0.0.0).

Definition at line 291 of file IpAddress.hpp.

## ◆ AnyV6

const IpAddress sf::IpAddress::AnyV6
| static

Value representing any IPv6 address (::).

Definition at line 294 of file IpAddress.hpp.

## ◆ Broadcast

const IpAddress sf::IpAddress::Broadcast
| static

The same as BroadcastV4.

Definition at line 290 of file IpAddress.hpp.

## ◆ BroadcastV4

const IpAddress sf::IpAddress::BroadcastV4
| static

The "broadcast" IPv4 address (for sending UDP messages to everyone on a local network).

Definition at line 293 of file IpAddress.hpp.

## ◆ LocalHost

const IpAddress sf::IpAddress::LocalHost
| static

The same as LocalHostV4.

Definition at line 289 of file IpAddress.hpp.

## ◆ LocalHostV4

const IpAddress sf::IpAddress::LocalHostV4
| static

The "localhost" IPv4 address (for connecting a computer to itself locally).

Definition at line 292 of file IpAddress.hpp.

## ◆ LocalHostV6

const IpAddress sf::IpAddress::LocalHostV6
| static

The "localhost" IPv6 address (for connecting a computer to itself locally).

Definition at line 295 of file IpAddress.hpp.

The documentation for this class was generated from the following file:

- IpAddress.hpp