Cross-Platform C++

ot::net
class InetAddress

#include "ot/net/InetAddress.h"

ot::ManagedObject An abstract class representing an Internet host's IP address.

This class provides static methods for resolving host names into IP addresses. Presently, only IPv4 addresses are supported, but future versions of OpenTop will support IPv6 addresses also.

The following example resolves a hostname into an IP address using the GetByName() static member function:-

#include "ot/net/InetAddress.h"
#include "ot/io/Console.h"

using namespace ot;
using namespace ot::io;
using namespace ot::net;

int main(int argc, char* argv[])
{
    try
    {
        // Get the IP address for 'www.elcel.com'
        RefPtr<InetAddress> rpAddr = InetAddress::GetByName(OT_T("www.elcel.com"));

        Console::cout() << OT_T("The IP Address for ") << rpAddr->getHostName() 
                        << OT_T(" is ") << rpAddr->getHostAddress() << endl;
    }
    catch(Exception& e)
    {
        Console::cerr() << e.toString() << endl;
    }
    return 0;
}




Constructor/Destructor Summary
InetAddress()
         Protected default constructor.
InetAddress(const InetAddress& rhs)
         Copy constructor.
~InetAddress()
         Destructor.

Method Summary
 virtual bool equals(const InetAddress& rhs) const
         Tests if two InetAddress instances refer to the same IP address.
static RefPtr< InetAddress > FromNetworkAddress(const void* pAddr, size_t addrlen)
         Creates an InetAddress instance from a sockaddr structure.
 virtual const Byte* getAddress() const
         Returns a pointer to a byte array containing the internal IP address.
 virtual size_t getAddressLength() const
         Returns the length of the byte array returned when calling getAddress().
static RefPtr< InetAddress > GetAnyHost()
         Returns a special InetAddress that does not pertain to any particular Internet host.
static RefPtr< InetAddress > GetByName(const String& host)
         Returns an InetAddress by performing a look-up of a host's name.
 virtual String getHostAddress() const
         Returns the IP address in dotted decimal notation.
 virtual String getHostName() const
         Returns the host name for this InetAddress.
static RefPtr< InetAddress > GetLocalBroadcast()
         Returns a special InetAddress that can be used to broadcast UDP datagrams to all hosts on the local subnet.
static RefPtr< InetAddress > GetLocalHost()
         Returns an InetAddress instance pertaining to the local host.
 bool operator!=(const InetAddress& rhs) const
         Tests if two InetAddress instances refer to different IP addresses.
 InetAddress& operator=(const InetAddress& rhs)
         Assignment operator.
 bool operator==(const InetAddress& rhs) const
         Tests if two InetAddress instances refer to the same IP address.
 virtual String toString() const
         Returns a String representation of this InetAddress.
 virtual String toStringLite() const
         Returns a String representation of this InetAddress without performing any additional name look-ups.

Methods inherited from class ot::ManagedObject
addRef(), getRefCount(), onFinalRelease(), operator=(const ManagedObject&), release()

Constructor/Destructor Detail

InetAddress

protected  InetAddress()
Protected default constructor. Used to prevent the accidental creation of invalid InetAddress instances.


InetAddress

 InetAddress(const InetAddress& rhs)
Copy constructor. A compiler-generated copy constructor would be unsuitable because this class contains pointer members.

Parameters:
rhs - InetAddress being copied.

~InetAddress

 ~InetAddress()
Destructor. Frees resources held by this object.


Method Detail

equals

virtual bool equals(const InetAddress& rhs) const
Tests if two InetAddress instances refer to the same IP address.

Parameters:
rhs - an InetAddress to compare against.
Returns:
true if they are equal; false otherwise.

FromNetworkAddress

static RefPtr< InetAddress > FromNetworkAddress(const void* pAddr,
                                                size_t addrlen)
Creates an InetAddress instance from a sockaddr structure.

Parameters:
pAddr - pointer to a sockaddr structure containing a valid Internet address.
addrlen - size of the passed sockaddr structure.
Returns:
An InetAddress instance initialized with the passed IP address.
Exceptions:
NullPointerException - if pAddr is null.
IllegalArgumentException - if pAddr points to a sockaddr whose sa_family member is not supported.

getAddress

virtual const BytegetAddress() const
Returns a pointer to a byte array containing the internal IP address.


getAddressLength

virtual size_t getAddressLength() const
Returns the length of the byte array returned when calling getAddress().

See also:
getAddress()
Since:
OpenTop 1.3

GetAnyHost

static RefPtr< InetAddress > GetAnyHost()
Returns a special InetAddress that does not pertain to any particular Internet host. This is often used when binding server or UDP sockets to all local interfaces.


GetByName

static RefPtr< InetAddress > GetByName(const String& host)
Returns an InetAddress by performing a look-up of a host's name. The host name can either be a machine name, such as "www.elcel.com", or a string representing its IP address in dotted decimal notation, such as "192.168.10.1".

Parameters:
host - the name of the Internet host
Returns:
An InetAddress instance for the specified host.
Exceptions:
UnknownHostException - if the host name cannot be resolved into an IP address.

getHostAddress

virtual String getHostAddress() const
Returns the IP address in dotted decimal notation. For example: "192.168.0.1"

See also:
getHostName()

getHostName

virtual String getHostName() const
Returns the host name for this InetAddress. Unless the host name has already been established (perhaps during construction) this routine uses the operating system's resolver library to locate the host name using a reverse DNS look-up.

No exceptions are thrown. If the reverse look-up fails, the IP address is returned (in dotted decimal notation) instead.

See also:
getHostAddress()

GetLocalBroadcast

static RefPtr< InetAddress > GetLocalBroadcast()
Returns a special InetAddress that can be used to broadcast UDP datagrams to all hosts on the local subnet.


GetLocalHost

static RefPtr< InetAddress > GetLocalHost()
Returns an InetAddress instance pertaining to the local host. On a multi-homed machine (a machine with multiple IP addresses), just one of the available addresses will be selected.


operator!=

bool operator!=(const InetAddress& rhs) const
Tests if two InetAddress instances refer to different IP addresses.

Parameters:
rhs - an InetAddress to compare against.
Returns:
true if they are not equal; false otherwise.

operator=

InetAddress& operator=(const InetAddress& rhs)
Assignment operator. A compiler-generated assignment operator would be unsuitable because this class contains pointer members.

Parameters:
rhs - InetAddress being copied.

operator==

bool operator==(const InetAddress& rhs) const
Tests if two InetAddress instances refer to the same IP address.

Parameters:
rhs - an InetAddress to compare against.
Returns:
true if they are equal; false otherwise.

toString

virtual String toString() const
Returns a String representation of this InetAddress. The returned string is of the form:-

hostname/host address

Note that the hostname component is obtained by invoking getHostname() and may therefore be delayed while a DNS reverse look-up is performed on the IP address. To avoid the potential for delay, consider using toStringLite() instead.

See also:
toStringLite()

toStringLite

virtual String toStringLite() const
Returns a String representation of this InetAddress without performing any additional name look-ups. The returned string takes the form:- hostname/host address where hostname is equal to 'unresolved' when the hostname is not known and a reverse DNS look-up would therefore be required to obtain it.

This function will not attempt to resolve unknown hostnames and is therefore a better choice that toString() in situations where a potential delay is unacceptable.

See also:
toString()
Since:
OpenTop 1.4


Cross-Platform C++

Found a bug or missing feature? Please email us at support@elcel.com

Copyright © 2000-2005 ElCel Technology   Trademark Acknowledgements