SFML - Simple and Fast Multimedia Library

SFML 3.0.2
Loading...
Searching...
No Matches
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 Member Functions
| IpAddress (std::uint8_t byte0, std::uint8_t byte1, std::uint8_t byte2, std::uint8_t byte3)
| Construct the address from 4 bytes.
| IpAddress (std::uint32_t address)
| Construct the address from a 32-bits integer.
std::string | toString () const
| Get a string representation of the address.
std::uint32_t | toInteger () const
| Get an integer representation of the 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 > | getLocalAddress ()
| Get the computer's local address.
static std::optional< IpAddress > | getPublicAddress (Time timeout=Time::Zero)
| Get the computer's public address.

## Static Public Attributes
static const IpAddress | Any
| Value representing any address (0.0.0.0)
static const IpAddress | LocalHost
| The "localhost" address (for connecting a computer to itself locally)
static const IpAddress | Broadcast
| The "broadcast" address (for sending UDP messages to everyone on a local network)

## 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 a2 = sf::IpAddress::resolve("127.0.0.1"); // the local host address
auto a3 = sf::IpAddress::Broadcast; // the broadcast address
sf::IpAddress a4(192, 168, 1, 56); // a local address
auto a5 = sf::IpAddress::resolve("my_computer"); // a local address created from a network name
auto a6 = sf::IpAddress::resolve("89.54.1.169"); // a distant address
auto a7 = sf::IpAddress::resolve("www.google.com"); // a distant address created from a network name
auto a8 = sf::IpAddress::getLocalAddress(); // my address on the local network
auto a9 = sf::IpAddress::getPublicAddress(); // my address on the internet
sf::IpAddress
Encapsulate an IPv4 network address.
Definition IpAddress.hpp:49
sf::IpAddress::getLocalAddress
static std::optional< IpAddress > getLocalAddress()
Get the computer's local address.
sf::IpAddress::getPublicAddress
static std::optional< IpAddress > getPublicAddress(Time timeout=Time::Zero)
Get the computer's public address.
sf::IpAddress::resolve
static std::optional< IpAddress > resolve(std::string_view address)
Construct the address from a null-terminated string view.
sf::IpAddress::Broadcast
static const IpAddress Broadcast
The "broadcast" address (for sending UDP messages to everyone on a local network)
Definition IpAddress.hpp:170

Note that sf::IpAddress currently doesn't support IPv6 nor other types of network addresses.

Definition at line 48 of file IpAddress.hpp.

## Constructor & Destructor Documentation

## ◆ IpAddress() [1/2]

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

Construct the 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/2]

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

Construct the address from a 32-bits 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-bits integer

See also
  toInteger

## Member Function Documentation

## ◆ getLocalAddress()

std::optional< IpAddress > sf::IpAddress::getLocalAddress | ( |  | ) |
| 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.

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 | ) |
| staticnodiscard

Get the computer's public address.

The public address is the address of the computer from the internet point of view, i.e. something like 89.54.1.169. It is necessary for communications over the world wide web. The only way to get a public address is to ask it to a distant website; as a consequence, this function depends on both your network connection and the server, and may be very slow. You should use it as few as possible. Because this function depends on the network connection and on a distant server, you may use a time limit if you don't want your program to be possibly stuck waiting in case there is a problem; this limit is deactivated by default.

Parameters

  timeout | Maximum time to wait

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

See also
  getLocalAddress

## ◆ resolve()

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

Construct the address from a null-terminated string view.

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

Parameters

  address | IP address or network name

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

## ◆ toInteger()

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

Get an integer representation of the address.

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"), even if it was constructed from a host name.

Returns
  String representation of the address

See also
  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

Value representing any address (0.0.0.0)

Definition at line 168 of file IpAddress.hpp.

## ◆ Broadcast

const IpAddress sf::IpAddress::Broadcast
| static

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

Definition at line 170 of file IpAddress.hpp.

## ◆ LocalHost

const IpAddress sf::IpAddress::LocalHost
| static

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

Definition at line 169 of file IpAddress.hpp.

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

- IpAddress.hpp