S60 Open C
Open C Networking and Sockets

Open C Networking and Sockets

Table of Contents

Multihoming and network host entry functions
Querying available access points
gethostbyname
gethostbyaddr

 


Multihoming and network host entry functions

In Linux, network interfaces are identified by different IP addresses. In the mobile world an IP address is not valid at the moment. In most cases, the IP address is allotted dynamically by the service provider or by WLAN. Therefore, Symbian OS, introduces the idea of an interface via access points. Symbian OS stores access point information in system-wide tables. The programmer can choose any access point without prompting the user about the access point. This section shows how to create an access point and use it for network host entry functions such as gethostbyname, and gethostbyaddr.

 


Querying available access points

For information about this, refer to Multihoming and Socket communication.

 


gethostbyname

Note in the following example that the error checking code has been removed deliberately to improve the readability of the code. For release quality code, add the error checking statements in lines such as conn.Start(), conn.Open(), and so on.

int gethostbyname_iap (char *name, size_t size, int iap_id)
    {
    RSocketServ ss;
    struct hostent* retval=0;
    RConnection conn;
    TCommDbConnPref pref;
    RHostResolver r;
    
    ss.Connect();
    
    // TCommDbConnPref stores the conection preference of the user.
    pref.SetIapId(iap_id);
    pref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt); // Set not to prompt the user
    
    // RConnection creates a connection.
    conn.Open(ss, KAfInet);
    conn.Start(pref);
    
    r.Open(ss, KAfInet, KProtocolInetUdp, conn);
    
    TNameRecord record;
    TNameEntry entry(record);
#ifdef _UNICODE
    TPtrC8 ptr(reinterpret_cast<const TUint8*> (name));
    TBuf<0x40> hostname; // Assuming name is not more than 40 characters
    hostname.Copy(ptr);
#else
    TPtrC8 hostname(reinterpret_cast<const TUint8*> (name));
#endif /* _UNICODE */
    err = r.GetByName(hostname, entry);
    if (err == KErrNone)
    	{
    	record = entry();
    	retval = mapNameRecord(rp, record, sizeof(struct in_addr), AF_INET);
    	}
    r.Close();	
    conn.Close();
    ss.Close();
    
    return err;
    }

 


gethostbyaddr

struct hostent* gethostbyaddr_iap (struct _reent* rp, const char* addr, 
						int length, int format, int iap_id)
    {
    RSocketServ ss;
    struct hostent* retval = NULL;
    RConnection conn;
    TCommDbConnPref pref;
    RHostResolver r;
    
    ss.Connect();
    
    // TCommDbConnPref stores the conection preference of the user.
    pref.SetIapId(iap_id);
     // Set not to prompt the user
    pref.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
    
    // RConnection creates a connection.
    conn.Open(ss, KAfInet);
    conn.Start(pref);
    
    r.Open(ss, format, KProtocolInetUdp, conn);
    struct sockaddr buf;
    buf.sa_family = (unsigned short)format;
    memcpy(buf.sa_data, addr, length);
    TUSockAddr addr(&buf, length+4);
    TNameRecord record;
    TNameEntry entry(record);
    err = r.GetByAddress(addr, entry);
    if (err == KErrNone)
    	   {
    	   record = entry();
    	   retval = mapNameRecord(rp, record, length, format);
    	   }
    r.Close();
    conn.Close();
    ss.Close();
    return retval;
    }

Give feedback of this section


©Nokia 2007

Back to top


This material, including documentation and any related computer programs, is protected by copyright controlled by Nokia. All rights are reserved. Copying, including reproducing, storing, adapting or translating, any or all of this material requires the prior written consent of Nokia. This material also contains confidential information, which may not be disclosed to others without the prior written consent of Nokia.

Nokia is a registered trademark of Nokia Corporation. S60 and logo is a trademark of Nokia Corporation. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. Other company and product names mentioned herein may be trademarks or tradenames of their respective owners.