Open C Networking and Sockets |
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.
For information about this, refer to Multihoming and Socket communication.
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; }
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; }
©Nokia 2007 |