PrevNextUpHome SophiaFramework UNIVERSE 5.3

16.1. Network Address Management

There are two types of classes for managing a network address(a domain name, an IP address and a port number).

Table 16.1. Network Address Management Classes

Class Name Description
SFXInetAddress Class for managing a domain name and/or an IP address. The function to resolve a domain name is provided.
SFXSocketAddress Class for managing a domain name and/or an IP address and a port number. This class inherits from the SFXInetAddress class.
[Note] Note

In the SFXTCPSocket / SFXSSLSocket / SFXUDPSocket class, the domain name resolution will be performed automatically.

To resolve a domain name manually, call the SFXInetAddress::Resolve function.

[Caution] Setting of the MIF file

To use network function, the network privilege option must be turned on in the MIF file's setting.

16.1.1. Setting of Domain Name, IP Address and Port Number

A domain name, an IP address and a port number are set with the SFXInetAddress::Set / SFXSocketAddress::Set function or the SFXInetAddress::SFXInetAddress / SFXSocketAddress::SFXSocketAddress constructor.

An address of the SFXAnsiString type must be specified in the format below.

<Protocol> :// <Domain name> : <Port number> / <Path>  

or

<Account> @ <Domain name>
[Note] Address format

In case of the SFXInetAddress class, strings other than the domain name can be omitted. Only the domain name will be extracted from the address and set.

In case of the SFXInetAddress class, strings other than the domain name can be omitted. The domain name and the port number(default: "0") will be extracted from the address and set.

Example 16.1. Define the IP address, domain, and port number

SFXSocketAddress address1("127.0.0.1");           // IP address (when port number is omitted, it is "0")
SFXSocketAddress address2("127.0.0.1:80");        // IP address and port number
SFXSocketAddress address3("www.example.com:80");  // Domain name and port number
SFXSocketAddress address4("www.example.com", 80); // Domain name and port number

Example 16.2. Set the address

SFXSocketAddress address;

address.Set("www.example.com:80"); 
address.Set("http://www.example.com:80/dir1/dir2/index.html");
address.Set("http://user@127.0.0.1:80/dir1/");

16.1.2. Domain Name Resolution

16.1.2.1. Automatic Domain Name Resolution

In the SFXTCPSocket / SFXSSLSocket / SFXUDPSocket class, domain name resolution will be automatically performed only by passing the address of the SFXSocketAddress type.

Example 16.3. Method to resolve the domain name automatically

// set socket address(domain name and port number)
SFXSocketAddress address("www.example.com:80");

SFXTCPSocket _socket;

// open socket
_socket.Open();

// connect to the server(domain name is automatically resolved by the internal processing of the SFXTCPSocket class)
_socket.Connect(address, XALLBACK_INTERNAL(OnConnect));

16.1.2.2. Manual Domain Name Resolution

[Caution] Caution

When the SFXTCPSocket / SFXSSLSocket / SFXUDPSocket class is used, domain name resolution will be automatically performed only by passing the address of the SFXSocketAddress type. Therefore, in general, you don't have to resolve the domain name manually as the below.

To resolve the domain name manually, call the SFXInetAddress::Resolve function. The result of the domain name resolution will be notified to the callback function specified in the argument of this function. The domain name resolution can be canceled by calling the SFXInetAddress::Cancel function before the callback function is called.

As a result of the domain name resolution, IP addresses can be obtained by using the SFXInetAddress::GetIP / SFXInetAddress::AsINAddr function.

When a domain name is converted into a multiple of IP addresses, each IP address can be obtained by specifying its index number in the argument of the SFXInetAddress::GetIP / SFXInetAddress::AsINAddr function. The number of IP addresses that this object contains can be obtained by calling the SFXInetAddress::GetCount function.

[Note] When an IP address is set

If an IP address is set or a domain name is set in the "nnn.nnn.nnn.nnn"(nnn: number) format, there is no need to resolve the domain name by calling the SFXInetAddress::Resolve function.

Example 16.4. Method to resolve the domain name manually

// SFXSocketAddress instance is defined as class member variable for the callback function
class MyClass {
private:
    SFXSocketAddress _address;
public:
    Void Start(Void);
    XALLBACK_DECLARE_SFXSOCKETADDRESS(ResolveCallback);
};

Void MyClass::Start(Void)
{
    SFCError error;

    // set domain name and port number
    if ((error = _address.Set("www.example.com:80")) == SFERR_NO_ERROR) {
        // resolve domain ( result of domain name resolution will be notified to ResolveCallback function )
        error = _address.Resolve(XALLBACK_INTERNAL(ResolveCallback));
    }
    if (error != SFERR_NO_ERROR) {

        // if an error occurs, the ResolveCallback function will not be called

        // error handling
        ...
    }
}

// callback funtion notified of result of domain name resolution
XALLBACK_IMPLEMENT_SFXSOCKETADDRESS(MyClass, ResolveCallback, error)
{
    SFXAnsiString ip;
    SInt32 i;

    if (error == SFERR_NO_ERROR) {

        // display domain name and port number on BREW Output Window
        TRACE("%s", _address.Get().GetCString());      // domain name and port number
        TRACE("%s", _address.GetHost().GetCString());  // domain name
        TRACE("%s", _address.GetPort().GetCString());  // port number

        // repeat the number of IP addresses
        for (i = 0; i < _address.GetCount(); ++i) {

        // display IP address on BREW Output Window
            ip = _address.GetIP(i); 
            TRACE("%s", ip.GetCString());
        }
    } else {

        // error handling
        ...
    }
}

When the domain name may be converted into a multiple of IP addresses after the domain name resolution, the specific IP address can be obtained by specifying its index number.

[Caution] SFXSocketAddress Instance

If the SFXSocketAddress instance is released before the callback function is called, program will not work correctly.

[Note] First argument of the callback function

The SFXInetAddress::Resolve function internally calls the the BREW API INETMGR_GetHostByName function. The return value(error) of the BREW API INETMGR_GetHostByName function will be passed to the first argument of the callback function: