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 struct sockaddr* 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.

Methods inherited from class ot::ManagedObject
addRef, getRefCount, onFinalRelease, operator=, 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 memebers.

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 struct sockaddr* 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:
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 memebers.

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.



Cross-Platform C++

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

Copyright © 2000-2003 ElCel Technology   Trademark Acknowledgements