Categories: Symbian C++ | Networking | TCP/IP | Code Examples | How To
This page was last modified 15:17, 7 December 2007.
Getting the host address in Symbian C++
From Forum Nokia Wiki
The below code demonstrates retrieving of the host address in Symbian C++.
Headers
#define MAX_IP_ADDR_LEN 16 #include <stdio.h> #include <string.h> #include <unistd.h> #include <in_sock.h>
Getting Host address
/* * ret buf :- on return contains the Host Address * ret TInt :- Error codes KErrNone if success system wide error codes */ TInt GetHostAddress(char *buf) { RSocketServ iSocketServer; RSocket iSockClient; TRequestStatus iStatus; TBuf<(MAX_IP_ADDR_LEN)> buffer; TBuf8<(MAX_IP_ADDR_LEN*2)> buf_8bit; char ip_addr_l[MAX_IP_ADDR_LEN]; TRequestStatus protostartStatus; TBool ip_found_l = EFalse; TInt Err; Err = iSocketServer.Connect(); if (Err != KErrNone) { return (Err); } iSocketServer.StartProtocol(KAfInet,KSockStream,KProtocolInetTcp,protostartStatus); User::WaitForRequest(protostartStatus); if (protostartStatus.Int()!=KErrNone) { iSocketServer.Close(); return (protostartStatus.Int()); } Err = iSockClient.Open(iSocketServer,KAfInet,KSockStream,KProtocolInetTcp); if (Err != KErrNone) { iSocketServer.Close(); return (Err); } TSoInetInterfaceInfo* inf = new TSoInetInterfaceInfo; if(inf ==NULL) { iSockClient.Close(); iSocketServer.Close(); return KErrNoMemory; } TPckg<TSoInetInterfaceInfo> opt(*inf); /* Set SocketOption For enumerating interfaces */ Err = iSockClient.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl); if (Err != KErrNone) { iSockClient.Close(); iSocketServer.Close(); delete inf; return Err; } /* Start Enumerating the Interfaces */ while (iSockClient.GetOpt(KSoInetNextInterface, KSolInetIfCtrl,opt) == KErrNone) { inf->iAddress.Output(buffer); buf_8bit.Copy(buffer); memset(ip_addr_l, 0, MAX_IP_ADDR_LEN); strcpy(ip_addr_l, (char *)buf_8bit.PtrZ()); if (inf->iAddress.IsUnspecified()) { // Display UnSpecified } else if (inf->iAddress.IsLoopback()) { // display LoopBack } else if(inf->iAddress.IsLinkLocal()) { // Display LinkLocal address } else { strcpy(buf, ip_addr_l); ip_found_l = ETRUE; break; } }//end of while /* Free the resources */ iSockClient.Close(); iSocketServer.Close(); delete inf; if (ip_found_l == ETRUE) { return KErrNone; } else { return KErrNotFound; } }
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Maximum valu of address in Symbian | gloria799 | General Symbian C++ | 3 | 2008-04-10 00:52 |
| "+" prefix in Origination Address | jayess | General Messaging | 0 | 2003-07-29 06:25 |
| emulator problem | rudycandrasantoso | Symbian Networking & Messaging | 0 | 2005-05-22 06:35 |
| Printing via USB | thefrozenrose | Mobile Java General | 2 | 2007-01-30 12:13 |
| How to get IP Address | chintankanani11 | Mobile Java Networking & Messaging & Security | 1 | 2005-10-06 11:08 |
