Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 09:43, 28 May 2008.

LocalDeviceIpAddress

From Forum Nokia Wiki

This Function retrives the LocalIpAddress of the device. Tested on the S60 3rd Edition device Nokia E60.

int GetlocalIPAdressL()
{	
	RSocketServ socketServ;
	TInetAddr aAddr;
  	RSocket sock;
  	TBuf8<20> aIP8;
  	TBuf<20> aIP;
 
  	User::LeaveIfError(sock.Open(socketServ, KAfInet, KSockStream, KProtocolInetTcp));
  	
  	// Get the IAP id of the underlying interface of this RConnection
  	//TUint32 iapId = 0;
  	//User::LeaveIfError(conn.GetIntSetting(_L("IAP\\Id"), iapId));
  	
  	// Get IP information from the socket
  	TSoInetInterfaceInfo ifInfo;
  	TPckg<TSoInetInterfaceInfo> ifInfoPkg(ifInfo);
  	TSoInetIfQuery ifQuery;
  	TPckg<TSoInetIfQuery> ifQueryPkg(ifQuery);
  	// To find out which interfaces are using our current IAP, we must
  	// enumerate and go through all of them and make a query by name for each.
  	User::LeaveIfError(sock.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl));
  	
  	while(sock.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, ifInfoPkg) == KErrNone) {
 
    ifQuery.iName = ifInfo.iName;
    TInt err = sock.GetOpt(KSoInetIfQueryByName, KSolInetIfQuery, ifQueryPkg);
    	if( err == KErrNone   && ifQuery.iZone[1] == g_iapId) 
   		{ 	// IAP ID is index 1 of iZone
        	// We have found an interface using the IAP we are interested in.
        	//if( ifinfo.iAddress.Address() >0 ) 
        	if (!ifInfo.iAddress.IsUnspecified() && !ifInfo.iAddress.IsLoopback() && !ifInfo.iAddress.IsLinkLocal())
        	{
			       
            // found a IPv4 address
            aAddr = ifInfo.iAddress;
            aAddr.ConvertToV4();
	    aAddr.Output(aIP);
            
            aIP8.Copy(aIP);
           	
            strncpy(out,(char*)aIP8.Ptr(),aIP8.Length());
            
            
            sock.Close();
            return err; // stop & return KErrNone
        }
      }
      else if( err !=KErrNone ) {
        sock.Close();
        return err; // return with error
    }
  } // while
  sock.Close();
  return KErrNotFound; // return with KErrNotFound
	
		
}
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX