.. index:: pair: class; EE::Network::IpAddress .. _doxid-class_e_e_1_1_network_1_1_ip_address: class EE::Network::IpAddress ============================ .. toctree:: :hidden: Overview ~~~~~~~~ Encapsulate an IPv4 network address. :ref:`More...` .. ref-code-block:: cpp :class: doxyrest-overview-code-block #include class IpAddress { public: // fields static const IpAddress :ref:`None`; static const IpAddress :ref:`Any`; static const IpAddress :ref:`LocalHost`; static const IpAddress :ref:`Broadcast`; // construction :ref:`IpAddress`(); :ref:`IpAddress`(const std::string& address); :ref:`IpAddress`(const char* address); :ref:`IpAddress`(:ref:`Uint8` byte0, :ref:`Uint8` byte1, :ref:`Uint8` byte2, :ref:`Uint8` byte3); :ref:`IpAddress`(:ref:`Uint32` address); // methods std::string :ref:`toString`() const; :ref:`Uint32` :ref:`toInteger`() const; static IpAddress :ref:`getLocalAddress`(); static IpAddress :ref:`getPublicAddress`(:ref:`Time` timeout = Time::Zero); }; .. _details-class_e_e_1_1_network_1_1_ip_address: Detailed Documentation ~~~~~~~~~~~~~~~~~~~~~~ Encapsulate an IPv4 network address. :ref:`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: .. ref-code-block:: cpp IpAddress a0; // an invalid address IpAddress a1 = IpAddress::None; // an invalid address (same as a0) IpAddress a2("127.0.0.1"); // the local host address IpAddress a3 = IpAddress::Broadcast; // the broadcast address IpAddress a4(192, 168, 1, 56); // a local address IpAddress a5("my_computer"); // a local address created from a network name IpAddress a6("89.54.1.169"); // a distant address IpAddress a7("www.google.com"); // a distant address created from a network name IpAddress a8 = IpAddress::getLocalAddress(); // my address on the local network IpAddress a9 = IpAddress::getPublicAddress(); // my address on the internet Note that :ref:`IpAddress ` currently doesn't support IPv6 nor other types of network addresses. Fields ------ .. index:: pair: variable; None .. _doxid-class_e_e_1_1_network_1_1_ip_address_1ab4740b8a1b47ea8900995789e612e4dc: .. ref-code-block:: cpp :class: doxyrest-title-code-block static const IpAddress None Value representing an empty/invalid address. .. index:: pair: variable; Any .. _doxid-class_e_e_1_1_network_1_1_ip_address_1af86fbb465852602e7f307b55591fbfc5: .. ref-code-block:: cpp :class: doxyrest-title-code-block static const IpAddress Any Value representing any address (0.0.0.0) .. index:: pair: variable; LocalHost .. _doxid-class_e_e_1_1_network_1_1_ip_address_1a92bbf0978f00cda5c3eea391209fa34c: .. ref-code-block:: cpp :class: doxyrest-title-code-block static const IpAddress LocalHost The "localhost" address (for connecting a computer to itself locally) .. index:: pair: variable; Broadcast .. _doxid-class_e_e_1_1_network_1_1_ip_address_1abaccac709f7a69654ed4148fcf119e99: .. ref-code-block:: cpp :class: doxyrest-title-code-block static const IpAddress Broadcast The "broadcast" address (for sending UDP messages to everyone on a local network) Construction ------------ .. index:: pair: function; IpAddress .. _doxid-class_e_e_1_1_network_1_1_ip_address_1a23ed80730aac4613bcfc1913c3e867cc: .. ref-code-block:: cpp :class: doxyrest-title-code-block IpAddress() Default constructor This constructor creates an empty (invalid) address. .. index:: pair: function; IpAddress .. _doxid-class_e_e_1_1_network_1_1_ip_address_1ab16578af5b475d908c4ac34372e6be22: .. ref-code-block:: cpp :class: doxyrest-title-code-block 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"). .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - address - IP address or network name .. index:: pair: function; IpAddress .. _doxid-class_e_e_1_1_network_1_1_ip_address_1a5bad77197589d294411d3a216d8cdc68: .. ref-code-block:: cpp :class: doxyrest-title-code-block 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 :ref:`IpAddress ` work. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - address - IP address or network name .. index:: pair: function; IpAddress .. _doxid-class_e_e_1_1_network_1_1_ip_address_1a99650327c6a1fa9d28c2a69da5668d33: .. ref-code-block:: cpp :class: doxyrest-title-code-block IpAddress(:ref:`Uint8` byte0, :ref:`Uint8` byte1, :ref:`Uint8` byte2, :ref:`Uint8` byte3) Construct the address from 4 bytes Calling IpAddress(a, b, c, d) is equivalent to calling :ref:`IpAddress ` ("a.b.c.d"), but safer as it doesn't have to parse a string to get the address components. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - byte0 - First byte of the address * - byte1 - Second byte of the address * - byte2 - Third byte of the address * - byte3 - Fourth byte of the address .. index:: pair: function; IpAddress .. _doxid-class_e_e_1_1_network_1_1_ip_address_1af70de095f11f50a1dd6a562b2bd3fd3d: .. ref-code-block:: cpp :class: doxyrest-title-code-block IpAddress(:ref:`Uint32` address) 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 :ref:`IpAddress::toInteger() `. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - address - 4 bytes of the address packed into a 32-bits integer .. rubric:: See also: ToInteger Methods ------- .. index:: pair: function; toString .. _doxid-class_e_e_1_1_network_1_1_ip_address_1aa3bc4c6bbf950ccf80acedc3a5b1cdea: .. ref-code-block:: cpp :class: doxyrest-title-code-block std::string 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. .. rubric:: Returns: :ref:`String ` representation of the address .. rubric:: See also: ToInteger .. index:: pair: function; toInteger .. _doxid-class_e_e_1_1_network_1_1_ip_address_1a75196fbbdcb77550a3c052ee125b7365: .. ref-code-block:: cpp :class: doxyrest-title-code-block :ref:`Uint32` 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 :ref:`IpAddress ` with the proper constructor. .. rubric:: Returns: 32-bits unsigned integer representation of the address .. rubric:: See also: ToString .. index:: pair: function; getLocalAddress .. _doxid-class_e_e_1_1_network_1_1_ip_address_1a355604b4df6c43b6b4feaabce8c6199a: .. ref-code-block:: cpp :class: doxyrest-title-code-block static IpAddress getLocalAddress() 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. .. rubric:: Returns: Local IP address of the computer .. rubric:: See also: GetPublicAddress .. index:: pair: function; getPublicAddress .. _doxid-class_e_e_1_1_network_1_1_ip_address_1a9e3120296cfaaf23c323a8242b025801: .. ref-code-block:: cpp :class: doxyrest-title-code-block static IpAddress getPublicAddress(:ref:`Time` timeout = Time::Zero) 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. .. rubric:: Parameters: .. list-table:: :widths: 20 80 * - timeout - Maximum time to wait .. rubric:: Returns: Public IP address of the computer .. rubric:: See also: GetLocalAddress