Public Member Functions | Static Public Member Functions | Static Public Attributes | List of all members
cpp3ds::IpAddress Class Reference

Encapsulate an IPv4 network address. More...

#include <IpAddress.hpp>

Public Member Functions

 IpAddress ()
 Default constructor. More...
 
 IpAddress (const std::string &address)
 Construct the address from a string. More...
 
 IpAddress (const char *address)
 Construct the address from a string. More...
 
 IpAddress (Uint8 byte0, Uint8 byte1, Uint8 byte2, Uint8 byte3)
 Construct the address from 4 bytes. More...
 
 IpAddress (Uint32 address)
 Construct the address from a 32-bits integer. More...
 
std::string toString () const
 Get a string representation of the address. More...
 
Uint32 toInteger () const
 Get an integer representation of the address. More...
 

Static Public Member Functions

static IpAddress getLocalAddress ()
 Get the computer's local address. More...
 
static IpAddress getPublicAddress (Time timeout=Time::Zero)
 Get the computer's public address. More...
 

Static Public Attributes

static const IpAddress None
 Value representing an empty/invalid address. More...
 
static const IpAddress LocalHost
 The "localhost" address (for connecting a computer to itself locally) More...
 
static const IpAddress Broadcast
 The "broadcast" address (for sending UDP messages to everyone on a local network) More...
 

Detailed Description

Encapsulate an IPv4 network address.

cpp3ds::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:

cpp3ds::IpAddress a0; // an invalid address
cpp3ds::IpAddress a1 = cpp3ds::IpAddress::None; // an invalid address (same as a0)
cpp3ds::IpAddress a2("127.0.0.1"); // the local host address
cpp3ds::IpAddress a3 = cpp3ds::IpAddress::Broadcast; // the broadcast address
cpp3ds::IpAddress a4(192, 168, 1, 56); // a local address
cpp3ds::IpAddress a5("my_computer"); // a local address created from a network name
cpp3ds::IpAddress a6("89.54.1.169"); // a distant address
cpp3ds::IpAddress a7("www.google.com"); // a distant address created from a network name
cpp3ds::IpAddress a8 = cpp3ds::IpAddress::getLocalAddress(); // my address on the local network
cpp3ds::IpAddress a9 = cpp3ds::IpAddress::getPublicAddress(); // my address on the internet

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

Definition at line 43 of file IpAddress.hpp.

Constructor & Destructor Documentation

cpp3ds::IpAddress::IpAddress ( )

Default constructor.

This constructor creates an empty (invalid) address

cpp3ds::IpAddress::IpAddress ( const std::string &  address)

Construct the address from a string.

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

Parameters
addressIP address or network name
cpp3ds::IpAddress::IpAddress ( const char *  address)

Construct the address from a string.

Here address can be either a decimal address (ex: "192.168.1.56") or a network name (ex: "localhost"). This is equivalent to the constructor taking a std::string parameter, it is defined for convenience so that the implicit conversions from literal strings to IpAddress work.

Parameters
addressIP address or network name
cpp3ds::IpAddress::IpAddress ( Uint8  byte0,
Uint8  byte1,
Uint8  byte2,
Uint8  byte3 
)

Construct the address from 4 bytes.

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

Parameters
byte0First byte of the address
byte1Second byte of the address
byte2Third byte of the address
byte3Fourth byte of the address
cpp3ds::IpAddress::IpAddress ( Uint32  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
address4 bytes of the address packed into a 32-bits integer
See also
toInteger

Member Function Documentation

static IpAddress cpp3ds::IpAddress::getLocalAddress ( )
static

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
See also
getPublicAddress
static IpAddress cpp3ds::IpAddress::getPublicAddress ( Time  timeout = Time::Zero)
static

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
timeoutMaximum time to wait
Returns
Public IP address of the computer
See also
getLocalAddress
Uint32 cpp3ds::IpAddress::toInteger ( ) const

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 cpp3ds::IpAddress with the proper constructor.

Returns
32-bits unsigned integer representation of the address
See also
toString
std::string cpp3ds::IpAddress::toString ( ) const

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

Member Data Documentation

const IpAddress cpp3ds::IpAddress::Broadcast
static

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

Definition at line 185 of file IpAddress.hpp.

const IpAddress cpp3ds::IpAddress::LocalHost
static

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

Definition at line 184 of file IpAddress.hpp.

const IpAddress cpp3ds::IpAddress::None
static

Value representing an empty/invalid address.

Definition at line 183 of file IpAddress.hpp.


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