Using RSocket::LocalName(TInetAddr&)
| Thu, 2007-02-22 07:54 | |
|
|
Greetings,
I have the following code snippet: Code: TInetAddr aAddr(KInetAddrAny, 0); ... User::LeaveIfError( iListenSock.Open(iServ, KAfInet, KSockStream, KProtocolInetTcp, iConn) ); User::LeaveIfError( iListenSock.Bind(aAddr) ); User::LeaveIfError( iListenSock.Listen(QUEUE_SIZE) ); ... TInetAddr addr; iListenSock.LocalName(addr); RSocket::LocalName gets the allocated port, but doesn't retrieve the interface IP address this socket is bound to (address returned is always of the form 0.0.0.0:<port>).
Thanks in advance. |






Forum posts: 9
No problem with Listen
/*----------------Finds out local IP address of the device----------------------*/
TInt CSocketsEngine::GetLocalIPAddressL( RConnection& aConnection, TInetAddr& aAddr )
{
RSocket sock;
User::LeaveIfError(sock.Open(iSocketServ, KAfInet, KSockStream, KProtocolInetTcp));
// Get the IAP id of the underlying interface of this RConnection
TUint32 iapId = 0;
User::LeaveIfError(aConnection.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] == 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 ) {
// found a IPv4 address
aAddr = ifinfo.iAddress;
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
}
Forum posts: 131